Skip to content

Commit 2eccb1d

Browse files
committed
manual: many fixes
Mostly around attributes and language items.
1 parent a410833 commit 2eccb1d

File tree

1 file changed

+172
-82
lines changed

1 file changed

+172
-82
lines changed

src/doc/rust.md

Lines changed: 172 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ Attributes on the anonymous crate module define important metadata that influenc
611611
the behavior of the compiler.
612612

613613
~~~~
614-
// Package ID
614+
// Crate ID
615615
#[ crate_id = "projx#2.5" ];
616616
617617
// Additional metadata attributes
@@ -792,7 +792,7 @@ extern crate std; // equivalent to: extern crate std = "std";
792792
793793
extern crate ruststd = "std"; // linking to 'std' under another name
794794
795-
extern crate foo = "some/where/rust-foo#foo:1.0"; // a full package ID for external tools
795+
extern crate foo = "some/where/rust-foo#foo:1.0"; // a full crate ID for external tools
796796
~~~~
797797

798798
##### Use declarations
@@ -1505,11 +1505,9 @@ specified name.
15051505
extern { }
15061506
~~~~
15071507

1508-
The type of a function
1509-
declared in an extern block
1510-
is `extern "abi" fn(A1, ..., An) -> R`,
1511-
where `A1...An` are the declared types of its arguments
1512-
and `R` is the decalred return type.
1508+
The type of a function declared in an extern block is `extern "abi" fn(A1,
1509+
..., An) -> R`, where `A1...An` are the declared types of its arguments and
1510+
`R` is the declared return type.
15131511

15141512
## Visibility and Privacy
15151513

@@ -1680,41 +1678,43 @@ import public items from their destination, not private items.
16801678
## Attributes
16811679

16821680
~~~~ {.notrust .ebnf .gram}
1683-
attribute : '#' '[' attr_list ']' ;
1684-
attr_list : attr [ ',' attr_list ]* ;
1681+
attribute : '#' '!' ? '[' attr_list ']'
1682+
attr_list : attr [ ',' attr_list ]*
16851683
attr : ident [ '=' literal
1686-
| '(' attr_list ')' ] ? ;
1684+
| '(' attr_list ')' ] ?
16871685
~~~~
16881686

16891687
Static entities in Rust -- crates, modules and items -- may have _attributes_
1690-
applied to them. ^[Attributes in Rust are modeled on Attributes in ECMA-335,
1691-
C#]
1692-
An attribute is a general, free-form metadatum that is interpreted according to name, convention, and language and compiler version.
1693-
Attributes may appear as any of
1688+
applied to them. Attributes in Rust are modeled on Attributes in ECMA-335, C#.
1689+
An attribute is a general, free-form metadatum that is interpreted according
1690+
to name, convention, and language and compiler version. Attributes may appear
1691+
as any of:
16941692

16951693
* A single identifier, the attribute name
1696-
* An identifier followed by the equals sign '=' and a literal, providing a key/value pair
1694+
* An identifier followed by the equals sign '=' and a literal, providing a
1695+
key/value pair
16971696
* An identifier followed by a parenthesized list of sub-attribute arguments
16981697

1699-
Attributes terminated by a semi-colon apply to the entity that the attribute is declared
1700-
within. Attributes that are not terminated by a semi-colon apply to the next entity.
1698+
Attributes with a bang ("!") after the hash ("#") apply to the item that the
1699+
attribute is declared within. Attributes that do not have a bang after the
1700+
hash by a semi-colon apply to the next item.
17011701

17021702
An example of attributes:
17031703

1704-
~~~~ {.ignore}
1704+
~~~~
17051705
// General metadata applied to the enclosing module or crate.
1706-
#[license = "BSD"];
1706+
#![license = "BSD"]
17071707
17081708
// A function marked as a unit test
17091709
#[test]
17101710
fn test_foo() {
1711-
...
1711+
/* ... */
17121712
}
17131713
17141714
// A conditionally-compiled module
17151715
#[cfg(target_os="linux")]
17161716
mod bar {
1717-
...
1717+
/* ... */
17181718
}
17191719
17201720
// A lint attribute used to suppress a warning/error
@@ -1727,29 +1727,85 @@ pub type int8_t = i8;
17271727
> effectively no difference between an attribute handled by a loadable syntax
17281728
> extension and the compiler.
17291729
1730-
Some significant attributes include:
1731-
1732-
* The `doc` attribute, for documenting code in-place.
1733-
* The `cfg` attribute, for conditional-compilation by build-configuration (see
1734-
[Conditional compilation](#conditional-compilation)).
1735-
* The `crate_id` attribute, for describing the package ID of a crate.
1736-
* The `lang` attribute, for custom definitions of traits and functions that are
1737-
known to the Rust compiler (see [Language items](#language-items)).
1738-
* The `link` attribute, for describing linkage metadata for a extern blocks.
1739-
* The `test` attribute, for marking functions as unit tests.
1740-
* The `allow`, `warn`, `forbid`, and `deny` attributes, for
1741-
controlling lint checks (see [Lint check attributes](#lint-check-attributes)).
1742-
* The `deriving` attribute, for automatically generating implementations of
1743-
certain traits.
1744-
* The `inline` attribute, for expanding functions at caller location (see
1745-
[Inline attributes](#inline-attributes)).
1746-
* The `static_assert` attribute, for asserting that a static bool is true at
1747-
compiletime.
1748-
* The `thread_local` attribute, for defining a `static mut` as a thread-local.
1749-
Note that this is only a low-level building block, and is not local to a
1750-
*task*, nor does it provide safety.
1751-
1752-
Other attributes may be added or removed during development of the language.
1730+
### Crate-only attributes
1731+
1732+
- `crate_type` - see [linkage](#linkage).
1733+
- `feature` - see [compiler features](#compiler-features).
1734+
- `no_start` - disable linking to the `native` crate, which specifies the
1735+
"start" language item.
1736+
- `no_main` - disable emitting the `main` symbol. Useful when some
1737+
other object being linked to defines `main`.
1738+
- `no_std` - disable linking to the `std` crate.
1739+
- `crate_id` - specify the this crate's crate ID.
1740+
1741+
### Module-only attributes
1742+
1743+
- `path` - specifies the file to load the module from. `#[path="foo.rs"] mod
1744+
bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`
1745+
- `macro_escape` - macros defined in this module will be visible in the
1746+
module's parent, after this module has been included.
1747+
- `no_implicit_prelude` - disable injecting `use std::prelude::*` in this
1748+
module.
1749+
1750+
### Function-only attributes
1751+
1752+
- `start` - indicates that this function should be used as the entry point,
1753+
overriding the "start" language item. See the "start" [language
1754+
item](#language-items) for more details.
1755+
- `main` - indicates that this function should be passed to the entry point,
1756+
rather than the function in the crate root named `main`.
1757+
- `macro_registrar` - when using loadable syntax extensions, mark this
1758+
function as the registration point for the current crate's syntax
1759+
extensions.
1760+
1761+
### Static-only attributes
1762+
1763+
- `thread_local` - on a `static mut`, this signals that the value of this
1764+
static may change depending on the current thread. The exact consequences of
1765+
this are implementation-defined.
1766+
- `address_insignificant` - references to this static may alias with
1767+
references to other statics, potentially of unrelated type.
1768+
1769+
### FFI attributes
1770+
1771+
On an `extern` block, the following attributes are interpreted:
1772+
1773+
- `link` - indicate that a native library should be linked to for the
1774+
declarations in this block to be linked correctly. See [external
1775+
blocks](#external-blocks)
1776+
- `link_args` - specify arguments to the linker, rather than just the library
1777+
name and type. This is feature gated and the exact behavior is
1778+
implementation-defined (due to variety of linker invocation syntax).
1779+
1780+
On declarations inside an `extern` block, the following attributes are
1781+
interpreted:
1782+
1783+
- `link_name` - the name of the symbol that this function or static should be
1784+
imported as.
1785+
- `linkage` - on a static, this specifies the [linkage
1786+
type](http://llvm.org/docs/LangRef.html#linkage-types).
1787+
1788+
### Miscellaneous attributes
1789+
1790+
- `simd` - on certain tuple structs, derive the arithmetic operators, which
1791+
lower to the target's SIMD instructions, if any.
1792+
- `link_section` - on statics and functions, this specifies the section of the
1793+
object file that this item's contents will be placed into.
1794+
- `static_assert` - on statics whose type is `bool`, terminates compilation
1795+
with an error if it is not initialized to `true`.
1796+
- `repr` - on C-like enums, this sets the underlying type used for
1797+
representation. Useful for FFI.
1798+
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
1799+
symbol for this item to its identifier.
1800+
- `packed` - on structs or enums, eliminate any padding that would be used to
1801+
align fields.
1802+
- `unsafe_destructor` - allow implementations of the "drop" language item
1803+
where the type it is implemented for does not implement the "send" language
1804+
item.
1805+
- `unsafe_no_drop_flag` - on structs, remove the flag that prevents
1806+
destructors from being run twice. Destructors might be run multiple times on
1807+
the same object with this attribute.
1808+
- `macro_export` - export a macro for cross-crate usage.
17531809

17541810
### Conditional compilation
17551811

@@ -1791,9 +1847,7 @@ one of `foo` and `bar` to be defined (this resembles in the disjunctive normal
17911847
form). Additionally, one can reverse a condition by enclosing it in a
17921848
`not(...)`, like e. g. `#[cfg(not(target_os = "win32"))]`.
17931849

1794-
To pass a configuration option which triggers a `#[cfg(identifier)]` one can use
1795-
`rustc --cfg identifier`. In addition to that, the following configurations are
1796-
pre-defined by the compiler:
1850+
The following configurations must be defined by the implementation:
17971851

17981852
* `target_arch = "..."`. Target CPU architecture, such as `"x86"`, `"x86_64"`
17991853
`"mips"`, or `"arm"`.
@@ -1805,8 +1859,8 @@ pre-defined by the compiler:
18051859
* `target_os = "..."`. Operating system of the target, examples include
18061860
`"win32"`, `"macos"`, `"linux"`, `"android"` or `"freebsd"`.
18071861
* `target_word_size = "..."`. Target word size in bits. This is set to `"32"`
1808-
for 32-bit CPU targets, and likewise set to `"64"` for 64-bit CPU targets.
1809-
* `test`. Only set in test builds (`rustc --test`).
1862+
for targets with 32-bit pointers, and likewise set to `"64"` for 64-bit
1863+
pointers.
18101864
* `unix`. See `target_family`.
18111865
* `windows`. See `target_family`.
18121866

@@ -1823,7 +1877,7 @@ For any lint check `C`:
18231877
* `allow(C)` overrides the check for `C` so that violations will go
18241878
unreported,
18251879
* `forbid(C)` is the same as `deny(C)`, but also forbids uses of
1826-
`allow(C)` within the entity.
1880+
`allow(C)` within the attribute.
18271881

18281882
The lint checks supported by the compiler can be found via `rustc -W help`,
18291883
along with their default settings.
@@ -1881,11 +1935,11 @@ mod m3 {
18811935

18821936
### Language items
18831937

1884-
Some primitive Rust operations are defined in Rust code,
1885-
rather than being implemented directly in C or assembly language.
1886-
The definitions of these operations have to be easy for the compiler to find.
1887-
The `lang` attribute makes it possible to declare these operations.
1888-
For example, the `str` module in the Rust standard library defines the string equality function:
1938+
Some primitive Rust operations are defined in Rust code, rather than being
1939+
implemented directly in C or assembly language. The definitions of these
1940+
operations have to be easy for the compiler to find. The `lang` attribute
1941+
makes it possible to declare these operations. For example, the `str` module
1942+
in the Rust standard library defines the string equality function:
18891943

18901944
~~~~ {.ignore}
18911945
#[lang="str_eq"]
@@ -1900,16 +1954,23 @@ when generating calls to the string equality function.
19001954

19011955
A complete list of the built-in language items follows:
19021956

1903-
#### Traits
1957+
#### Built-in Traits
19041958

1905-
`const`
1906-
: Cannot be mutated.
1907-
`owned`
1908-
: Are uniquely owned.
1909-
`durable`
1910-
: Contain references.
1959+
`send`
1960+
: Able to be sent across task boundaries.
1961+
`sized`
1962+
: Has a size known at compile time.
1963+
`copy`
1964+
: Types that do not move ownership when used by-value.
1965+
`share`
1966+
: Able to be safely shared between tasks when aliased.
19111967
`drop`
1912-
: Have finalizers.
1968+
: Have destructors.
1969+
1970+
#### Operators
1971+
1972+
These language items are traits:
1973+
19131974
`add`
19141975
: Elements can be added (for example, integers and floats).
19151976
`sub`
@@ -1940,17 +2001,54 @@ A complete list of the built-in language items follows:
19402001
: Elements can be compared for equality.
19412002
`ord`
19422003
: Elements have a partial ordering.
2004+
`deref`
2005+
: `*` can be applied, yielding a reference to another type
2006+
`deref_mut`
2007+
: `*` can be applied, yielding a mutable reference to another type
19432008

1944-
#### Operations
2009+
2010+
These are functions:
19452011

19462012
`str_eq`
1947-
: Compare two strings for equality.
2013+
: Compare two strings (`&str`) for equality.
19482014
`uniq_str_eq`
1949-
: Compare two owned strings for equality.
1950-
`annihilate`
1951-
: Destroy a box before freeing it.
1952-
`log_type`
1953-
: Generically print a string representation of any type.
2015+
: Compare two owned strings (`~str`) for equality.
2016+
`strdup_uniq`
2017+
: Return a new unique string
2018+
containing a copy of the contents of a unique string.
2019+
2020+
#### Types
2021+
2022+
`unsafe`
2023+
: A type whose contents can be mutated through an immutable reference
2024+
`type_id`
2025+
: The type returned by the `type_id` intrinsic.
2026+
2027+
#### Marker types
2028+
2029+
These types help drive the compiler's analysis
2030+
2031+
`covariant_type`
2032+
: The type parameter should be considered covariant
2033+
`contravariant_type`
2034+
: The type parameter should be considered contravariant
2035+
`invariant_type`
2036+
: The type parameter should be considered invariant
2037+
`covariant_lifetime`
2038+
: The lifetime parameter should be considered covariant
2039+
`contravariant_lifetime`
2040+
: The lifetime parameter should be considered contravariant
2041+
`invariant_lifetime`
2042+
: The lifetime parameter should be considered invariant
2043+
`no_send_bound`
2044+
: This type does not implement "send", even if eligible
2045+
`no_copy_bound`
2046+
: This type does not implement "copy", even if eligible
2047+
`no_share_bound`
2048+
: This type does not implement "share", even if eligible
2049+
`managed_bound`
2050+
: This type implements "managed"
2051+
19542052
`fail_`
19552053
: Abort the program with an error.
19562054
`fail_bounds_check`
@@ -1963,15 +2061,6 @@ A complete list of the built-in language items follows:
19632061
: Allocate memory on the managed heap.
19642062
`free`
19652063
: Free memory that was allocated on the managed heap.
1966-
`borrow_as_imm`
1967-
: Create an immutable reference to a mutable value.
1968-
`return_to_mut`
1969-
: Release a reference created with `return_to_mut`
1970-
`check_not_borrowed`
1971-
: Fail if a value has existing references to it.
1972-
`strdup_uniq`
1973-
: Return a new unique string
1974-
containing a copy of the contents of a unique string.
19752064

19762065
> **Note:** This list is likely to become out of date. We should auto-generate it
19772066
> from `librustc/middle/lang_items.rs`.
@@ -2039,6 +2128,7 @@ Supported traits for `deriving` are:
20392128
* `Show`, to format a value using the `{}` formatter.
20402129

20412130
### Stability
2131+
20422132
One can indicate the stability of an API using the following attributes:
20432133

20442134
* `deprecated`: This item should no longer be used, e.g. it has been
@@ -2101,18 +2191,18 @@ necessarily ready for every-day use. These features are often of "prototype
21012191
quality" or "almost production ready", but may not be stable enough to be
21022192
considered a full-fleged language feature.
21032193

2104-
For this reason, rust recognizes a special crate-level attribute of the form:
2194+
For this reason, Rust recognizes a special crate-level attribute of the form:
21052195

21062196
~~~~ {.ignore}
2107-
#[feature(feature1, feature2, feature3)]
2197+
#![feature(feature1, feature2, feature3)]
21082198
~~~~
21092199

21102200
This directive informs the compiler that the feature list: `feature1`,
21112201
`feature2`, and `feature3` should all be enabled. This is only recognized at a
21122202
crate-level, not at a module-level. Without this directive, all features are
21132203
considered off, and using the features will result in a compiler error.
21142204

2115-
The currently implemented features of the compiler are:
2205+
The currently implemented features of the reference compiler are:
21162206

21172207
* `macro_rules` - The definition of new macros. This does not encompass
21182208
macro-invocation, that is always enabled by default, this only

0 commit comments

Comments
 (0)