Skip to content

Commit 604e682

Browse files
authored
Merge pull request #1599 from chorman0773/spec-add-identifiers-items
Add identifier syntax to items.md and subchapters
2 parents 85c4a7a + 9e4d726 commit 604e682

16 files changed

+692
-50
lines changed

src/items.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Items
22

3+
r[items]
4+
5+
r[items.syntax]
36
> **<sup>Syntax:<sup>**\
47
> _Item_:\
58
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
@@ -28,15 +31,17 @@
2831
> &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
2932
> &nbsp;&nbsp; | [_MacroRulesDefinition_]
3033
31-
34+
r[items.intro]
3235
An _item_ is a component of a crate. Items are organized within a crate by a
3336
nested set of [modules]. Every crate has a single "outermost" anonymous module;
3437
all further items within the crate have [paths] within the module tree of the
3538
crate.
3639

40+
r[items.static-def]
3741
Items are entirely determined at compile-time, generally remain fixed during
3842
execution, and may reside in read-only memory.
3943

44+
r[items.kinds]
4045
There are several kinds of items:
4146

4247
* [modules]
@@ -53,11 +58,19 @@ There are several kinds of items:
5358
* [implementations]
5459
* [`extern` blocks]
5560

61+
r[items.locations]
5662
Items may be declared in the [root of the crate], a [module][modules], or a [block expression].
63+
64+
r[items.associated-locations]
5765
A subset of items, called [associated items], may be declared in [traits] and [implementations].
66+
67+
r[items.extern-locations]
5868
A subset of items, called external items, may be declared in [`extern` blocks].
5969

70+
r[items.decl-order]
6071
Items may be defined in any order, with the exception of [`macro_rules`] which has its own scoping behavior.
72+
73+
r[items.name-resolution]
6174
[Name resolution] of item names allows items to be defined before or after where the item is referred to in the module or block.
6275

6376
See [item scopes] for information on the scoping rules of items.

src/items/associated-items.md

+72-14
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,62 @@
11
# Associated Items
22

3+
r[items.associated]
4+
5+
r[items.associated.syntax]
36
> **<sup>Syntax</sup>**\
47
> _AssociatedItem_ :\
58
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
69
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
710
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_TypeAlias_] | [_ConstantItem_] | [_Function_] ) )\
811
> &nbsp;&nbsp; )
912
13+
r[items.associated.intro]
1014
*Associated Items* are the items declared in [traits] or defined in
1115
[implementations]. They are called this because they are defined on an associate
12-
type &mdash; the type in the implementation. They are a subset of the kinds of
13-
items you can declare in a module. Specifically, there are [associated
14-
functions] (including methods), [associated types], and [associated constants].
16+
type &mdash; the type in the implementation.
17+
18+
r[items.associated.kinds]
19+
They are a subset of the kinds of items you can declare in a module.
20+
Specifically, there are [associated functions] (including methods), [associated types], and [associated constants].
1521

1622
[associated functions]: #associated-functions-and-methods
1723
[associated types]: #associated-types
1824
[associated constants]: #associated-constants
1925

26+
r[items.associated.related]
2027
Associated items are useful when the associated item logically is related to the
2128
associating item. For example, the `is_some` method on `Option` is intrinsically
2229
related to Options, so should be associated.
2330

31+
r[items.associated.decl-def]
2432
Every associated item kind comes in two varieties: definitions that contain the
2533
actual implementation and declarations that declare signatures for
2634
definitions.
2735

36+
r[items.associated.trait-items]
2837
It is the declarations that make up the contract of traits and what is available
2938
on generic types.
3039

3140
## Associated functions and methods
3241

42+
r[items.associated.fn]
43+
44+
r[items.associated.fn.intro]
3345
*Associated functions* are [functions] associated with a type.
3446

47+
r[items.associated.fn.decl]
3548
An *associated function declaration* declares a signature for an associated
3649
function definition. It is written as a function item, except the
3750
function body is replaced with a `;`.
3851

39-
The identifier is the name of the function. The generics, parameter list,
40-
return type, and where clause of the associated function must be the same as the
52+
r[items.associated.name]
53+
The identifier is the name of the function.
54+
55+
r[items.associated.same-signature]
56+
The generics, parameter list, return type, and where clause of the associated function must be the same as the
4157
associated function declarations's.
4258

59+
r[items.associated.fn.def]
4360
An *associated function definition* defines a function associated with another
4461
type. It is written the same as a [function item].
4562

@@ -64,6 +81,7 @@ fn main () {
6481
}
6582
```
6683

84+
r[items.associated.fn.qualified-self]
6785
When the associated function is declared on a trait, the function can also be
6886
called with a [path] that is a path to the trait appended by the name of the
6987
trait. When this happens, it is substituted for `<_ as Trait>::function_name`.
@@ -86,10 +104,14 @@ let _: f64 = f64::from_i32(42);
86104

87105
### Methods
88106

107+
r[items.associated.fn.method]
108+
109+
r[items.associated.fn.method.intro]
89110
Associated functions whose first parameter is named `self` are called *methods*
90111
and may be invoked using the [method call operator], for example, `x.foo()`, as
91112
well as the usual function call notation.
92113

114+
r[items.associated.fn.method.self-ty]
93115
If the type of the `self` parameter is specified, it is limited to types resolving
94116
to one generated by the following grammar (where `'lt` denotes some arbitrary
95117
lifetime):
@@ -127,6 +149,7 @@ impl Example {
127149
}
128150
```
129151

152+
r[associated.fn.method.self-pat-shorthands]
130153
Shorthand syntax can be used without specifying a type, which have the
131154
following equivalents:
132155

@@ -138,6 +161,7 @@ Shorthand | Equivalent
138161

139162
> **Note**: Lifetimes can be, and usually are, elided with this shorthand.
140163
164+
r[associated.fn.method.self-pat-mut]
141165
If the `self` parameter is prefixed with `mut`, it becomes a mutable variable,
142166
similar to regular parameters using a `mut` [identifier pattern]. For example:
143167

@@ -189,21 +213,30 @@ let circle_shape = Circle::new();
189213
let bounding_box = circle_shape.bounding_box();
190214
```
191215

216+
r[items.associated.fn.params.edition2015]
192217
> **Edition differences**: In the 2015 edition, it is possible to declare trait
193218
> methods with anonymous parameters (e.g. `fn foo(u8)`). This is deprecated and
194219
> an error as of the 2018 edition. All parameters must have an argument name.
195220
196221
#### Attributes on method parameters
197222

223+
r[items.associated.fn.param-attributes]
224+
198225
Attributes on method parameters follow the same rules and restrictions as
199226
[regular function parameters].
200227

201228
## Associated Types
202229

203-
*Associated types* are [type aliases] associated with another type. Associated
204-
types cannot be defined in [inherent implementations] nor can they be given a
230+
r[items.associated.type]
231+
232+
r[items.associated.type.intro]
233+
*Associated types* are [type aliases] associated with another type.
234+
235+
r[items.associated.type.restrictions]
236+
Associated types cannot be defined in [inherent implementations] nor can they be given a
205237
default implementation in traits.
206238

239+
r[items.associated.type.decl]
207240
An *associated type declaration* declares a signature for associated type
208241
definitions. It is written in one of the following forms, where `Assoc` is the
209242
name of the associated type, `Params` is a comma-separated list of type,
@@ -221,13 +254,21 @@ type Assoc<Params> where WhereBounds;
221254
type Assoc<Params>: Bounds where WhereBounds;
222255
```
223256

224-
The identifier is the name of the declared type alias. The optional trait bounds
225-
must be fulfilled by the implementations of the type alias.
257+
r[items.associated.type.name]
258+
The identifier is the name of the declared type alias.
259+
260+
r[items.associated.type.impl-fulfillment]
261+
The optional trait bounds must be fulfilled by the implementations of the type alias.
262+
263+
r[items.associated.type.sized]
226264
There is an implicit [`Sized`] bound on associated types that can be relaxed using the special `?Sized` bound.
227265

266+
r[items.associated.type.def]
228267
An *associated type definition* defines a type alias for the implementation
229-
of a trait on a type. They are written similarly to an *associated type declaration*,
230-
but cannot contain `Bounds`, but instead must contain a `Type`:
268+
of a trait on a type
269+
270+
r[items.associated.type.def.restriction]
271+
They are written similarly to an *associated type declaration*, but cannot contain `Bounds`, but instead must contain a `Type`:
231272

232273
<!-- ignore: illustrative example forms -->
233274
```rust,ignore
@@ -237,11 +278,15 @@ type Assoc<Params> = Type where WhereBounds;
237278
type Assoc<Params> where WhereBounds = Type; // deprecated, prefer the form above
238279
```
239280

281+
r[items.associated.type.alias]
240282
If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
241283
`<Item as Trait>::Assoc` is a type that is an alias of the type specified in the
242-
associated type definition. Furthermore, if `Item` is a type parameter, then
243-
`Item::Assoc` can be used in type parameters.
284+
associated type definition
285+
286+
r[items.associated.type.param]
287+
Furthermore, if `Item` is a type parameter, then `Item::Assoc` can be used in type parameters.
244288

289+
r[items.associated.type.generic]
245290
Associated types may include [generic parameters] and [where clauses]; these are
246291
often referred to as *generic associated types*, or *GATs*. If the type `Thing`
247292
has an associated type `Item` from a trait `Trait` with the generics `<'a>` , the
@@ -300,7 +345,6 @@ fn borrow<'a, T: Lend>(array: &'a mut T) -> <T as Lend>::Lender<'a> {
300345
array.lend()
301346
}
302347

303-
304348
fn main() {
305349
let mut array = [0usize; 16];
306350
let lender = borrow(&mut array);
@@ -352,11 +396,15 @@ Given a reference to the associated type like `<X as Example>::Output<Y>`, the a
352396

353397
### Required where clauses on generic associated types
354398

399+
r[items.associated.type.generic-where-clause]
400+
401+
r[items.associated.type.generic-where-clause.intro]
355402
Generic associated type declarations on traits currently may require a list of
356403
where clauses, dependent on functions in the trait and how the GAT is used. These
357404
rules may be loosened in the future; updates can be found [on the generic
358405
associated types initiative repository](https://rust-lang.github.io/generic-associated-types-initiative/explainer/required_bounds.html).
359406

407+
r[items.associated.type.generic-where-clause.valid-fn]
360408
In a few words, these where clauses are required in order to maximize the allowed
361409
definitions of the associated type in impls. To do this, any clauses that *can be
362410
proven to hold* on functions (using the parameters of the function or trait)
@@ -373,6 +421,7 @@ In the above, on the `next` function, we can prove that `Self: 'a`, because of
373421
the implied bounds from `&'a mut self`; therefore, we must write the equivalent
374422
bound on the GAT itself: `where Self: 'x`.
375423

424+
r[items.associated.type.generic-where-clause.intersection]
376425
When there are multiple functions in a trait that use the GAT, then the
377426
*intersection* of the bounds from the different functions are used, rather than
378427
the union.
@@ -390,6 +439,7 @@ know that `T: 'a` on `create_checker`, we do not know that on `do_check`. Howeve
390439
if `do_check` was commented out, then the `where T: 'x` bound would be required
391440
on `Checker`.
392441

442+
r[items.associated.type.generic-where-clause.forward]
393443
The bounds on associated types also propagate required where clauses.
394444

395445
```rust
@@ -404,6 +454,7 @@ Here, `where Self: 'a` is required on `Item` because of `iter`. However, `Item`
404454
is used in the bounds of `Iterator`, the `where Self: 'a` clause is also required
405455
there.
406456

457+
r[items.associated.type.generic-where-clause.static]
407458
Finally, any explicit uses of `'static` on GATs in the trait do not count towards
408459
the required bounds.
409460

@@ -416,18 +467,25 @@ trait StaticReturn {
416467

417468
## Associated Constants
418469

470+
r[items.associated.const]
471+
472+
r[items.associated.const.intro]
419473
*Associated constants* are [constants] associated with a type.
420474

475+
r[items.associated.const.decl]
421476
An *associated constant declaration* declares a signature for associated
422477
constant definitions. It is written as `const`, then an identifier,
423478
then `:`, then a type, finished by a `;`.
424479

480+
r[items.associated.const.name]
425481
The identifier is the name of the constant used in the path. The type is the
426482
type that the definition has to implement.
427483

484+
r[items.associated.const.def]
428485
An *associated constant definition* defines a constant associated with a
429486
type. It is written the same as a [constant item].
430487

488+
r[items.associated.const.eval]
431489
Associated constant definitions undergo [constant evaluation] only when
432490
referenced. Further, definitions that include [generic parameters] are
433491
evaluated after monomorphization.

src/items/constant-items.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
# Constant items
22

3+
r[items.const]
4+
5+
r[items.const.syntax]
36
> **<sup>Syntax</sup>**\
47
> _ConstantItem_ :\
58
> &nbsp;&nbsp; `const` ( [IDENTIFIER] | `_` ) `:` [_Type_] ( `=` [_Expression_] )<sup>?</sup> `;`
69
10+
r[items.const.intro]
711
A *constant item* is an optionally named _[constant value]_ which is not associated
8-
with a specific memory location in the program. Constants are essentially inlined
9-
wherever they are used, meaning that they are copied directly into the relevant
12+
with a specific memory location in the program.
13+
14+
r[items.const.behavior]
15+
Constants are essentially inlined wherever they are used, meaning that they are copied directly into the relevant
1016
context when used. This includes usage of constants from external crates, and
1117
non-[`Copy`] types. References to the same constant are not necessarily
1218
guaranteed to refer to the same memory address.
1319

20+
r[items.const.namespace]
1421
The constant declaration defines the constant value in the [value namespace] of the module or block where it is located.
1522

23+
r[items.const.static]
1624
Constants must be explicitly typed. The type must have a `'static` lifetime: any
1725
references in the initializer must have `'static` lifetimes. References
1826
in the type of a constant default to `'static` lifetime; see [static lifetime
1927
elision].
2028

29+
r[items.const.static-temporary]
2130
A reference to a constant will have `'static` lifetime if the constant value is eligible for
2231
[promotion]; otherwise, a temporary will be created.
2332

@@ -39,10 +48,13 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
3948
};
4049
```
4150

51+
r[items.const.expr-omission]
4252
The constant expression may only be omitted in a [trait definition].
4353

4454
## Constants with Destructors
4555

56+
r[items.const.destructor]
57+
4658
Constants can contain destructors. Destructors are run when the value goes out
4759
of scope.
4860

@@ -66,6 +78,9 @@ fn create_and_drop_zero_with_destructor() {
6678

6779
## Unnamed constant
6880

81+
r[items.const.unnamed]
82+
83+
r[items.const.unnamed.intro]
6984
Unlike an [associated constant], a [free] constant may be unnamed by using
7085
an underscore instead of the name. For example:
7186

@@ -76,6 +91,7 @@ const _: () = { struct _SameNameTwice; };
7691
const _: () = { struct _SameNameTwice; };
7792
```
7893

94+
r[items.const.unnamed.repetition]
7995
As with [underscore imports], macros may safely emit the same unnamed constant in
8096
the same scope more than once. For example, the following should not produce an error:
8197

@@ -92,6 +108,8 @@ m!(const _: () = (););
92108

93109
## Evaluation
94110

111+
r[items.const.eval]
112+
95113
[Free][free] constants are always [evaluated][const_eval] at compile-time to surface
96114
panics. This happens even within an unused function:
97115

0 commit comments

Comments
 (0)