Skip to content

Commit 76da773

Browse files
committed
Add identifier syntax to keywords, lifetime-elision, macros, memory-allocation-and-lifetimes, names, and paths
1 parent 24fb268 commit 76da773

File tree

6 files changed

+237
-23
lines changed

6 files changed

+237
-23
lines changed

src/keywords.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Keywords
22

3+
r[lex.keywords]
4+
35
Rust divides keywords into three categories:
46

57
* [strict](#strict-keywords)
@@ -8,6 +10,9 @@ Rust divides keywords into three categories:
810

911
## Strict keywords
1012

13+
r[lex.keywords.strict]
14+
15+
r[lex.keywords.strict.intro]
1116
These keywords can only be used in their correct contexts. They cannot
1217
be used as the names of:
1318

@@ -20,7 +25,8 @@ be used as the names of:
2025
* [Macro placeholders]
2126
* [Crates]
2227

23-
> **<sup>Lexer:</sup>**\
28+
r[lex.keywords.strict.list]
29+
> **<sup>Lexer:<sup>**\
2430
> KW_AS : `as`\
2531
> KW_BREAK : `break`\
2632
> KW_CONST : `const`\
@@ -57,6 +63,7 @@ be used as the names of:
5763
> KW_WHERE : `where`\
5864
> KW_WHILE : `while`
5965
66+
r[lex.keywords.strict.edition2018]
6067
The following keywords were added beginning in the 2018 edition.
6168

6269
> **<sup>Lexer 2018+</sup>**\
@@ -66,11 +73,15 @@ The following keywords were added beginning in the 2018 edition.
6673
6774
## Reserved keywords
6875

76+
r[lex.keywords.reserved]
77+
78+
r[lex.keywords.reserved.intro]
6979
These keywords aren't used yet, but they are reserved for future use. They have
7080
the same restrictions as strict keywords. The reasoning behind this is to make
7181
current programs forward compatible with future versions of Rust by forbidding
7282
them to use these keywords.
7383

84+
r[lex.keywords.reserved.list]
7485
> **<sup>Lexer</sup>**\
7586
> KW_ABSTRACT : `abstract`\
7687
> KW_BECOME : `become`\
@@ -85,39 +96,53 @@ them to use these keywords.
8596
> KW_VIRTUAL : `virtual`\
8697
> KW_YIELD : `yield`
8798
99+
r[lex.keywords.reserved.edition2018]
88100
The following keywords are reserved beginning in the 2018 edition.
89101

90102
> **<sup>Lexer 2018+</sup>**\
91103
> KW_TRY : `try`
92104
93105
## Weak keywords
94106

107+
r[lex.keywords.weak]
108+
109+
r[lex.keywords.weak.intro]
95110
These keywords have special meaning only in certain contexts. For example, it
96111
is possible to declare a variable or method with the name `union`.
97112

113+
r[lex.keywords.weak.macro_rules]
98114
* `macro_rules` is used to create custom [macros].
115+
116+
r[lex.keywords.weak.union]
99117
* `union` is used to declare a [union] and is only a keyword when used in a
100118
union declaration.
119+
120+
r[lex.keywords.weak.lifetime-static]
101121
* `'static` is used for the static lifetime and cannot be used as a [generic
102122
lifetime parameter] or [loop label]
103123

104124
```compile_fail
105125
// error[E0262]: invalid lifetime parameter name: `'static`
106126
fn invalid_lifetime_parameter<'static>(s: &'static str) -> &'static str { s }
107127
```
128+
129+
r[lex.keywords.weak.dyn]
108130
* In the 2015 edition, [`dyn`] is a keyword when used in a type position
109131
followed by a path that does not start with `::` or `<`, a lifetime, a question mark, a `for`
110132
keyword or an opening parenthesis.
111133

112134
Beginning in the 2018 edition, `dyn` has been promoted to a strict keyword.
113135

136+
r[lex.keywords.weak.list]
114137
> **<sup>Lexer</sup>**\
115138
> KW_MACRO_RULES : `macro_rules`\
116139
> KW_UNION : `union`\
117140
> KW_STATICLIFETIME : `'static`
118141
>
119142
> **<sup>Lexer 2015</sup>**\
120143
> KW_DYN : `dyn`
144+
145+
r[lex.keywords.weak.safe]
121146
* `safe` is used for functions and statics, which has meaning in [external blocks].
122147

123148
[items]: items.md

src/lifetime-elision.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
# Lifetime elision
22

3+
r[lifetime-elision]
4+
35
Rust has rules that allow lifetimes to be elided in various places where the
46
compiler can infer a sensible default choice.
57

68
## Lifetime elision in functions
79

10+
r[lifetime-elision.function]
11+
12+
r[lifetime-elision.function.intro]
813
In order to make common patterns more ergonomic, lifetime arguments can be
914
*elided* in [function item], [function pointer], and [closure trait] signatures.
1015
The following rules are used to infer lifetime parameters for elided lifetimes.
11-
It is an error to elide lifetime parameters that cannot be inferred. The
12-
placeholder lifetime, `'_`, can also be used to have a lifetime inferred in the
13-
same way. For lifetimes in paths, using `'_` is preferred. Trait object
14-
lifetimes follow different rules discussed
16+
17+
r[lifetime-elision.function.constraint]
18+
It is an error to elide lifetime parameters that cannot be inferred.
19+
20+
r[lifetime-elision.function.explicit-placeholder]
21+
The placeholder lifetime, `'_`, can also be used to have a lifetime inferred in the
22+
same way. For lifetimes in paths, using `'_` is preferred.
23+
24+
r[lifetime-elision.function.only-functions]
25+
Trait object lifetimes follow different rules discussed
1526
[below](#default-trait-object-lifetimes).
1627

28+
r[lifetime-elision.function.implicit-lifetime-parameters]
1729
* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
30+
31+
r[lifetime-elision.function.output-lifetime]
1832
* If there is exactly one lifetime used in the parameters (elided or not), that
1933
lifetime is assigned to *all* elided output lifetimes.
2034

35+
r[lifetime-elision.function.reciever-lifetime]
2136
In method signatures there is another rule
2237

2338
* If the receiver has type `&Self` or `&mut Self`, then the lifetime of that
@@ -75,27 +90,43 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
7590

7691
## Default trait object lifetimes
7792

93+
r[lifetime-elision.trait-object]
94+
95+
r[lifetime-elision.trait-object.intro]
7896
The assumed lifetime of references held by a [trait object] is called its
7997
_default object lifetime bound_. These were defined in [RFC 599] and amended in
8098
[RFC 1156].
8199

100+
r[lifetime-elision.trait-object.explicit-bound]
82101
These default object lifetime bounds are used instead of the lifetime parameter
83-
elision rules defined above when the lifetime bound is omitted entirely. If
84-
`'_` is used as the lifetime bound then the bound follows the usual elision
102+
elision rules defined above when the lifetime bound is omitted entirely.
103+
104+
r[lifetime-elision.trait-object.explicit-placeholder]
105+
If `'_` is used as the lifetime bound then the bound follows the usual elision
85106
rules.
86107

108+
r[lifetime-elision.trait-object.containing-type]
87109
If the trait object is used as a type argument of a generic type then the
88110
containing type is first used to try to infer a bound.
89111

112+
r[lifetime-elision.trait-object.containing-type-unique]
90113
* If there is a unique bound from the containing type then that is the default
114+
115+
r[lifetime-elision.trait-object.containing-type-explicit]
91116
* If there is more than one bound from the containing type then an explicit
92117
bound must be specified
93118

119+
r[lifetime-elision.trait-object.trait-bounds]
94120
If neither of those rules apply, then the bounds on the trait are used:
95121

122+
r[lifetime-elision.trait-object.trait-unique]
96123
* If the trait is defined with a single lifetime _bound_ then that bound is
97124
used.
125+
126+
r[lifetime-elision.trait-object.static-lifetime]
98127
* If `'static` is used for any lifetime bound then `'static` is used.
128+
129+
r[lifetime-elision.trait-object.default]
99130
* If the trait has no lifetime bounds, then the lifetime is inferred in
100131
expressions and is `'static` outside of expressions.
101132

@@ -133,6 +164,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
133164
// Error: the lifetime bound for this object type cannot be deduced from context
134165
```
135166

167+
r[lifetime-elision.trait-object.innermost-type]
136168
Note that the innermost object sets the bound, so `&'a Box<dyn Foo>` is still
137169
`&'a Box<dyn Foo + 'static>`.
138170

@@ -151,6 +183,9 @@ impl<'a> dyn Bar<'a> + 'a {}
151183

152184
## `'static` lifetime elision
153185

186+
r[lifetime-elision.item]
187+
188+
r[lifetime-elision.item.intro]
154189
Both [constant] and [static] declarations of reference types have *implicit*
155190
`'static` lifetimes unless an explicit lifetime is specified. As such, the
156191
constant declarations involving `'static` above may be written without the
@@ -172,6 +207,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
172207
};
173208
```
174209

210+
r[lifetime-elision.item.fn-types]
175211
Note that if the `static` or `const` items include function or closure
176212
references, which themselves include references, the compiler will first try
177213
the standard elision rules. If it is unable to resolve the lifetimes by its

src/macros.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# Macros
22

3+
r[macro]
4+
5+
r[macro.intro]
36
The functionality and syntax of Rust can be extended with custom definitions
47
called macros. They are given names, and invoked through a consistent
58
syntax: `some_extension!(...)`.
69

710
There are two ways to define new macros:
811

12+
r[macro.rules]
913
* [Macros by Example] define new syntax in a higher-level, declarative way.
14+
15+
r[macro.proc]
1016
* [Procedural Macros] define function-like macros, custom derives, and custom
1117
attributes using functions that operate on input tokens.
1218

1319
## Macro Invocation
1420

21+
r[macro.invocation]
22+
23+
r[macro.invocation.syntax]
1524
> **<sup>Syntax</sup>**\
1625
> _MacroInvocation_ :\
1726
> &nbsp;&nbsp; [_SimplePath_] `!` _DelimTokenTree_
@@ -29,17 +38,30 @@ There are two ways to define new macros:
2938
> &nbsp;&nbsp; | [_SimplePath_] `!` `[` _TokenTree_<sup>\*</sup> `]` `;`\
3039
> &nbsp;&nbsp; | [_SimplePath_] `!` `{` _TokenTree_<sup>\*</sup> `}`
3140
41+
r[macro.invocation.intro]
3242
A macro invocation expands a macro at compile time and replaces the
3343
invocation with the result of the macro. Macros may be invoked in the
3444
following situations:
3545

46+
r[macro.invocation.expr]
3647
* [Expressions] and [statements]
48+
49+
r[macro.invocation.pattern]
3750
* [Patterns]
51+
52+
r[macro.invocation.type]
3853
* [Types]
54+
55+
r[macro.invocation.item]
3956
* [Items] including [associated items]
57+
58+
r[macro.invocation.nested]
4059
* [`macro_rules`] transcribers
60+
61+
r[macro.invocation.extern]
4162
* [External blocks]
4263

64+
r[macro.invocation.item-statement]
4365
When used as an item or a statement, the _MacroInvocationSemi_ form is used
4466
where a semicolon is required at the end when not using curly braces.
4567
[Visibility qualifiers] are never allowed before a macro invocation or

src/memory-allocation-and-lifetime.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Memory allocation and lifetime
22

3+
r[alloc]
4+
5+
r[alloc.static]
36
The _items_ of a program are those functions, modules, and types that have their
47
value calculated at compile-time and stored uniquely in the memory image of the
58
rust process. Items are neither dynamically allocated nor freed.
69

10+
r[alloc.dynamic]
711
The _heap_ is a general term that describes boxes. The lifetime of an
812
allocation in the heap depends on the lifetime of the box values pointing to
913
it. Since box values may themselves be passed in and out of frames, or stored

0 commit comments

Comments
 (0)