Skip to content

Commit 34bc44f

Browse files
committed
Rollup merge of #25335 - nikomatsakis:updates-to-reference-manual, r=steveklabnik
I did a read through of the manual. This commit corrects various small points and expands some sections, while avoiding too much detail. r? @steveklabnik
2 parents b3c92d9 + 393a37e commit 34bc44f

File tree

1 file changed

+151
-52
lines changed

1 file changed

+151
-52
lines changed

src/doc/reference.md

+151-52
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ x;
426426
x::y::z;
427427
```
428428

429-
Path components are usually [identifiers](#identifiers), but the trailing
430-
component of a path may be an angle-bracket-enclosed list of type arguments. In
431-
[expression](#expressions) context, the type argument list is given after a
432-
final (`::`) namespace qualifier in order to disambiguate it from a relational
433-
expression involving the less-than symbol (`<`). In type expression context,
434-
the final namespace qualifier is omitted.
429+
Path components are usually [identifiers](#identifiers), but they may
430+
also include angle-bracket-enclosed lists of type arguments. In
431+
[expression](#expressions) context, the type argument list is given
432+
after a `::` namespace qualifier in order to disambiguate it from a
433+
relational expression involving the less-than symbol (`<`). In type
434+
expression context, the final namespace qualifier is omitted.
435435

436436
Two examples of paths with type arguments:
437437

@@ -497,8 +497,9 @@ names, and invoked through a consistent syntax: `some_extension!(...)`.
497497

498498
Users of `rustc` can define new syntax extensions in two ways:
499499

500-
* [Compiler plugins][plugin] can include arbitrary
501-
Rust code that manipulates syntax trees at compile time.
500+
* [Compiler plugins][plugin] can include arbitrary Rust code that
501+
manipulates syntax trees at compile time. Note that the interface
502+
for compiler plugins is considered highly unstable.
502503

503504
* [Macros](book/macros.html) define new syntax in a higher-level,
504505
declarative way.
@@ -560,14 +561,18 @@ Nested repetitions are allowed.
560561
The parser used by the macro system is reasonably powerful, but the parsing of
561562
Rust syntax is restricted in two ways:
562563

563-
1. The parser will always parse as much as possible. If it attempts to match
564-
`$i:expr [ , ]` against `8 [ , ]`, it will attempt to parse `i` as an array
565-
index operation and fail. Adding a separator can solve this problem.
564+
1. Macro definitions are required to include suitable separators after parsing
565+
expressions and other bits of the Rust grammar. This implies that
566+
a macro definition like `$i:expr [ , ]` is not legal, because `[` could be part
567+
of an expression. A macro definition like `$i:expr,` or `$i:expr;` would be legal,
568+
however, because `,` and `;` are legal separators. See [RFC 550] for more information.
566569
2. The parser must have eliminated all ambiguity by the time it reaches a `$`
567570
_name_ `:` _designator_. This requirement most often affects name-designator
568571
pairs when they occur at the beginning of, or immediately after, a `$(...)*`;
569572
requiring a distinctive token in front can solve the problem.
570573

574+
[RFC 550]: https://github.com/rust-lang/rfcs/blob/master/text/0550-macro-future-proofing.md
575+
571576
# Crates and source files
572577

573578
Although Rust, like any other language, can be implemented by an interpreter as
@@ -686,7 +691,8 @@ type arguments as a list of comma-separated types enclosed within angle
686691
brackets, in order to refer to the type-parameterized item. In practice, the
687692
type-inference system can usually infer such argument types from context. There
688693
are no general type-parametric types, only type-parametric items. That is, Rust
689-
has no notion of type abstraction: there are no first-class "forall" types.
694+
has no notion of type abstraction: there are no higher-ranked (or "forall") types
695+
abstracted over other types, though higher-ranked types do exist for lifetimes.
690696

691697
### Modules
692698

@@ -732,6 +738,7 @@ mod vec;
732738
733739
mod thread {
734740
// Load the `local_data` module from `thread/local_data.rs`
741+
// or `thread/local_data/mod.rs`.
735742
mod local_data;
736743
}
737744
```
@@ -1004,7 +1011,8 @@ the guarantee that these issues are never caused by safe code.
10041011
* `&mut` and `&` follow LLVM’s scoped [noalias] model, except if the `&T`
10051012
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
10061013
guarantees.
1007-
* Mutating an immutable value/reference without `UnsafeCell<U>`
1014+
* Mutating non-mutable data (that is, data reached through a shared reference or
1015+
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
10081016
* Invoking undefined behavior via compiler intrinsics:
10091017
* Indexing outside of the bounds of an object with `std::ptr::offset`
10101018
(`offset` intrinsic), with
@@ -1034,9 +1042,13 @@ be undesired.
10341042
* Exiting without calling destructors
10351043
* Sending signals
10361044
* Accessing/modifying the file system
1037-
* Unsigned integer overflow (well-defined as wrapping)
1038-
* Signed integer overflow (well-defined as two’s complement representation
1039-
wrapping)
1045+
* Integer overflow
1046+
- Overflow is considered "unexpected" behavior and is always user-error,
1047+
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
1048+
will insert debug checks that panic on overflow, but in optimized builds overflow
1049+
instead results in wrapped values. See [RFC 560] for the rationale and more details.
1050+
1051+
[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
10401052

10411053
#### Diverging functions
10421054

@@ -1310,11 +1322,26 @@ type of the value is not required to ascribe to `Sync`.
13101322

13111323
### Traits
13121324

1313-
A _trait_ describes a set of method types.
1325+
A _trait_ describes an abstract interface that types can
1326+
implement. This interface consists of associated items, which come in
1327+
three varieties:
1328+
1329+
- functions
1330+
- constants
1331+
- types
1332+
1333+
Associated functions whose first parameter is named `self` are called
1334+
methods and may be invoked using `.` notation (e.g., `x.foo()`).
1335+
1336+
All traits define an implicit type parameter `Self` that refers to
1337+
"the type that is implementing this interface". Traits may also
1338+
contain additional type parameters. These type parameters (including
1339+
`Self`) may be constrained by other traits and so forth as usual.
13141340

1315-
Traits can include default implementations of methods, written in terms of some
1316-
unknown [`self` type](#self-types); the `self` type may either be completely
1317-
unspecified, or constrained by some other trait.
1341+
Trait bounds on `Self` are considered "supertraits". These are
1342+
required to be acyclic. Supertraits are somewhat different from other
1343+
constraints in that they affect what methods are available in the
1344+
vtable when the trait is used as a [trait object](#trait-objects).
13181345

13191346
Traits are implemented for specific types through separate
13201347
[implementations](#implementations).
@@ -1359,15 +1386,18 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
13591386
}
13601387
```
13611388

1362-
Traits also define an [trait object](#trait-objects) with the same name as the
1363-
trait. Values of this type are created by [casting](#type-cast-expressions)
1364-
pointer values (pointing to a type for which an implementation of the given
1365-
trait is in scope) to pointers to the trait name, used as a type.
1389+
Traits also define an [trait object](#trait-objects) with the same
1390+
name as the trait. Values of this type are created by coercing from a
1391+
pointer of some specific type to a pointer of trait type. For example,
1392+
`&T` could be coerced to `&Shape` if `T: Shape` holds (and similarly
1393+
for `Box<T>`). This coercion can either be implicit or
1394+
[explicit](#type-cast-expressions). Here is an example of an explicit
1395+
coercion:
13661396

13671397
```
1368-
# trait Shape { fn dummy(&self) { } }
1369-
# impl Shape for i32 { }
1370-
# let mycircle = 0i32;
1398+
trait Shape { }
1399+
impl Shape for i32 { }
1400+
let mycircle = 0i32;
13711401
let myshape: Box<Shape> = Box::new(mycircle) as Box<Shape>;
13721402
```
13731403

@@ -2041,7 +2071,8 @@ The name `str_eq` has a special meaning to the Rust compiler, and the presence
20412071
of this definition means that it will use this definition when generating calls
20422072
to the string equality function.
20432073

2044-
A complete list of the built-in language items will be added in the future.
2074+
The set of language items is currently considered unstable. A complete
2075+
list of the built-in language items will be added in the future.
20452076

20462077
### Inline attributes
20472078

@@ -2053,11 +2084,6 @@ The compiler automatically inlines functions based on internal heuristics.
20532084
Incorrectly inlining functions can actually make the program slower, so it
20542085
should be used with care.
20552086

2056-
Immutable statics are always considered inlineable unless marked with
2057-
`#[inline(never)]`. It is undefined whether two different inlineable statics
2058-
have the same memory address. In other words, the compiler is free to collapse
2059-
duplicate inlineable statics together.
2060-
20612087
`#[inline]` and `#[inline(always)]` always cause the function to be serialized
20622088
into the crate metadata to allow cross-crate inlining.
20632089

@@ -2259,10 +2285,6 @@ The currently implemented features of the reference compiler are:
22592285
* `unboxed_closures` - Rust's new closure design, which is currently a work in
22602286
progress feature with many known bugs.
22612287

2262-
* `unsafe_destructor` - Allows use of the `#[unsafe_destructor]` attribute,
2263-
which is considered wildly unsafe and will be
2264-
obsoleted by language improvements.
2265-
22662288
* `unsafe_no_drop_flag` - Allows use of the `#[unsafe_no_drop_flag]` attribute,
22672289
which removes hidden flag added to a type that
22682290
implements the `Drop` trait. The design for the
@@ -2382,18 +2404,54 @@ expressions](#index-expressions) (`expr[expr]`), and [field
23822404
references](#field-expressions) (`expr.f`). All other expressions are rvalues.
23832405

23842406
The left operand of an [assignment](#assignment-expressions) or
2385-
[compound-assignment](#compound-assignment-expressions) expression is an lvalue
2386-
context, as is the single operand of a unary
2387-
[borrow](#unary-operator-expressions). All other expression contexts are
2388-
rvalue contexts.
2407+
[compound-assignment](#compound-assignment-expressions) expression is
2408+
an lvalue context, as is the single operand of a unary
2409+
[borrow](#unary-operator-expressions). The discriminant or subject of
2410+
a [match expression](#match-expressions) may be an lvalue context, if
2411+
ref bindings are made, but is otherwise an rvalue context. All other
2412+
expression contexts are rvalue contexts.
23892413

23902414
When an lvalue is evaluated in an _lvalue context_, it denotes a memory
23912415
location; when evaluated in an _rvalue context_, it denotes the value held _in_
23922416
that memory location.
23932417

2394-
When an rvalue is used in an lvalue context, a temporary un-named lvalue is
2395-
created and used instead. A temporary's lifetime equals the largest lifetime
2396-
of any reference that points to it.
2418+
##### Temporary lifetimes
2419+
2420+
When an rvalue is used in an lvalue context, a temporary un-named
2421+
lvalue is created and used instead. The lifetime of temporary values
2422+
is typically the innermost enclosing statement; the tail expression of
2423+
a block is considered part of the statement that encloses the block.
2424+
2425+
When a temporary rvalue is being created that is assigned into a `let`
2426+
declaration, however, the temporary is created with the lifetime of
2427+
the enclosing block instead, as using the enclosing statement (the
2428+
`let` declaration) would be a guaranteed error (since a pointer to the
2429+
temporary would be stored into a variable, but the temporary would be
2430+
freed before the variable could be used). The compiler uses simple
2431+
syntactic rules to decide which values are being assigned into a `let`
2432+
binding, and therefore deserve a longer temporary lifetime.
2433+
2434+
Here are some examples:
2435+
2436+
- `let x = foo(&temp())`. The expression `temp()` is an rvalue. As it
2437+
is being borrowed, a temporary is created which will be freed after
2438+
the innermost enclosing statement (the `let` declaration, in this case).
2439+
- `let x = temp().foo()`. This is the same as the previous example,
2440+
except that the value of `temp()` is being borrowed via autoref on a
2441+
method-call. Here we are assuming that `foo()` is an `&self` method
2442+
defined in some trait, say `Foo`. In other words, the expression
2443+
`temp().foo()` is equivalent to `Foo::foo(&temp())`.
2444+
- `let x = &temp()`. Here, the same temporary is being assigned into
2445+
`x`, rather than being passed as a parameter, and hence the
2446+
temporary's lifetime is considered to be the enclosing block.
2447+
- `let x = SomeStruct { foo: &temp() }`. As in the previous case, the
2448+
temporary is assigned into a struct which is then assigned into a
2449+
binding, and hence it is given the lifetime of the enclosing block.
2450+
- `let x = [ &temp() ]`. As in the previous case, the
2451+
temporary is assigned into an array which is then assigned into a
2452+
binding, and hence it is given the lifetime of the enclosing block.
2453+
- `let ref x = temp()`. In this case, the temporary is created using a ref binding,
2454+
but the result is the same: the lifetime is extended to the enclosing block.
23972455

23982456
#### Moved and copied types
23992457

@@ -2535,8 +2593,10 @@ A field access is an [lvalue](#lvalues,-rvalues-and-temporaries) referring to
25352593
the value of that field. When the type providing the field inherits mutability,
25362594
it can be [assigned](#assignment-expressions) to.
25372595

2538-
Also, if the type of the expression to the left of the dot is a pointer, it is
2539-
automatically dereferenced to make the field access possible.
2596+
Also, if the type of the expression to the left of the dot is a
2597+
pointer, it is automatically dereferenced as many times as necessary
2598+
to make the field access possible. In cases of ambiguity, we prefer
2599+
fewer autoderefs to more.
25402600

25412601
### Array expressions
25422602

@@ -2577,6 +2637,11 @@ let arr = ["a", "b"];
25772637
arr[10]; // panics
25782638
```
25792639

2640+
Also, if the type of the expression to the left of the brackets is a
2641+
pointer, it is automatically dereferenced as many times as necessary
2642+
to make the indexing possible. In cases of ambiguity, we prefer fewer
2643+
autoderefs to more.
2644+
25802645
### Range expressions
25812646

25822647
The `..` operator will construct an object of one of the `std::ops::Range` variants.
@@ -2599,7 +2664,7 @@ assert_eq!(x,y);
25992664

26002665
### Unary operator expressions
26012666

2602-
Rust defines three unary operators. They are all written as prefix operators,
2667+
Rust defines the following unary operators. They are all written as prefix operators,
26032668
before the expression they apply to.
26042669

26052670
* `-`
@@ -2613,11 +2678,20 @@ before the expression they apply to.
26132678
implemented by the type and required for an outer expression that will or
26142679
could mutate the dereference), and produces the result of dereferencing the
26152680
`&` or `&mut` borrowed pointer returned from the overload method.
2616-
26172681
* `!`
26182682
: Logical negation. On the boolean type, this flips between `true` and
26192683
`false`. On integer types, this inverts the individual bits in the
26202684
two's complement representation of the value.
2685+
* `&` and `&mut`
2686+
: Borrowing. When applied to an lvalue, these operators produce a
2687+
reference (pointer) to the lvalue. The lvalue is also placed into
2688+
a borrowed state for the duration of the reference. For a shared
2689+
borrow (`&`), this implies that the lvalue may not be mutated, but
2690+
it may be read or shared again. For a mutable borrow (`&mut`), the
2691+
lvalue may not be accessed in any way until the borrow expires.
2692+
If the `&` or `&mut` operators are applied to an rvalue, a
2693+
temporary value is created; the lifetime of this temporary value
2694+
is defined by [syntactic rules](#temporary-lifetimes).
26212695

26222696
### Binary operator expressions
26232697

@@ -2727,6 +2801,13 @@ fn avg(v: &[f64]) -> f64 {
27272801
}
27282802
```
27292803

2804+
Some of the conversions which can be done through the `as` operator
2805+
can also be done implicitly at various points in the program, such as
2806+
argument passing and assignment to a `let` binding with an explicit
2807+
type. Implicit conversions are limited to "harmless" conversions that
2808+
do not lose information and which have minimal or no risk of
2809+
surprising side-effects on the dynamic execution semantics.
2810+
27302811
#### Assignment expressions
27312812

27322813
An _assignment expression_ consists of an
@@ -3348,6 +3429,22 @@ let bo: Binop = add;
33483429
x = bo(5,7);
33493430
```
33503431

3432+
#### Function types for specific items
3433+
3434+
Internally to the compiler, there are also function types that are specific to a particular
3435+
function item. In the following snippet, for example, the internal types of the functions
3436+
`foo` and `bar` are different, despite the fact that they have the same signature:
3437+
3438+
```
3439+
fn foo() { }
3440+
fn bar() { }
3441+
```
3442+
3443+
The types of `foo` and `bar` can both be implicitly coerced to the fn
3444+
pointer type `fn()`. There is currently no syntax for unique fn types,
3445+
though the compiler will emit a type like `fn() {foo}` in error
3446+
messages to indicate "the unique fn type for the function `foo`".
3447+
33513448
### Closure types
33523449

33533450
A [lambda expression](#lambda-expressions) produces a closure value with
@@ -3432,8 +3529,9 @@ has type `Vec<A>`, a vector with element type `A`.
34323529

34333530
### Self types
34343531

3435-
The special type `self` has a meaning within methods inside an impl item. It
3436-
refers to the type of the implicit `self` argument. For example, in:
3532+
The special type `Self` has a meaning within traits and impls. In a trait definition, it refers
3533+
to an implicit type parameter representing the "implementing" type. In an impl,
3534+
it is an alias for the implementing type. For example, in:
34373535

34383536
```
34393537
trait Printable {
@@ -3447,8 +3545,9 @@ impl Printable for String {
34473545
}
34483546
```
34493547

3450-
`self` refers to the value of type `String` that is the receiver for a call to
3451-
the method `make_string`.
3548+
The notation `&self` is a shorthand for `self: &Self`. In this case,
3549+
in the impl, `Self` refers to the value of type `String` that is the
3550+
receiver for a call to the method `make_string`.
34523551

34533552
# Special traits
34543553

0 commit comments

Comments
 (0)