Skip to content

Commit 038c2e2

Browse files
authored
Merge pull request #1597 from chorman0773/spec-add-identifiers-rest
Add Identifier Syntax to Several Chapters
2 parents f80986b + 3396132 commit 038c2e2

10 files changed

+504
-40
lines changed

src/items/constant-items.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn unused_generic_function<T>() {
126126
[associated constant]: ../items/associated-items.md#associated-constants
127127
[constant value]: ../const_eval.md#constant-expressions
128128
[free]: ../glossary.md#free-item
129-
[static lifetime elision]: ../lifetime-elision.md#static-lifetime-elision
129+
[static lifetime elision]: ../lifetime-elision.md#const-and-static-elision
130130
[trait definition]: traits.md
131131
[IDENTIFIER]: ../identifiers.md
132132
[underscore imports]: use-declarations.md#underscore-imports

src/keywords.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
r[lex.keywords]
12
# Keywords
23

34
Rust divides keywords into three categories:
@@ -6,8 +7,10 @@ Rust divides keywords into three categories:
67
* [reserved](#reserved-keywords)
78
* [weak](#weak-keywords)
89

10+
r[lex.keywords.strict]
911
## Strict keywords
1012

13+
r[lex.keywords.strict.intro]
1114
These keywords can only be used in their correct contexts. They cannot
1215
be used as the names of:
1316

@@ -20,7 +23,8 @@ be used as the names of:
2023
* [Macro placeholders]
2124
* [Crates]
2225

23-
> **<sup>Lexer:</sup>**\
26+
r[lex.keywords.strict.list]
27+
> **<sup>Lexer:<sup>**\
2428
> KW_AS : `as`\
2529
> KW_BREAK : `break`\
2630
> KW_CONST : `const`\
@@ -57,20 +61,24 @@ be used as the names of:
5761
> KW_WHERE : `where`\
5862
> KW_WHILE : `while`
5963
64+
r[lex.keywords.strict.edition2018]
6065
The following keywords were added beginning in the 2018 edition.
6166

6267
> **<sup>Lexer 2018+</sup>**\
6368
> KW_ASYNC : `async`\
6469
> KW_AWAIT : `await`\
6570
> KW_DYN : `dyn`
6671
72+
r[lex.keywords.reserved]
6773
## Reserved keywords
6874

75+
r[lex.keywords.reserved.intro]
6976
These keywords aren't used yet, but they are reserved for future use. They have
7077
the same restrictions as strict keywords. The reasoning behind this is to make
7178
current programs forward compatible with future versions of Rust by forbidding
7279
them to use these keywords.
7380

81+
r[lex.keywords.reserved.list]
7482
> **<sup>Lexer</sup>**\
7583
> KW_ABSTRACT : `abstract`\
7684
> KW_BECOME : `become`\
@@ -85,6 +93,7 @@ them to use these keywords.
8593
> KW_VIRTUAL : `virtual`\
8694
> KW_YIELD : `yield`
8795
96+
r[lex.keywords.reserved.edition2018]
8897
The following keywords are reserved beginning in the 2018 edition.
8998

9099
> **<sup>Lexer 2018+</sup>**\
@@ -95,8 +104,10 @@ The following keywords are reserved beginning in the 2024 edition.
95104
> **<sup>Lexer 2024+</sup>**\
96105
> KW_GEN : `gen`
97106
107+
r[lex.keywords.weak]
98108
## Weak keywords
99109

110+
r[lex.keywords.weak.intro]
100111
These keywords have special meaning only in certain contexts. For example, it
101112
is possible to declare a variable or method with the name `union`.
102113

@@ -110,24 +121,33 @@ is possible to declare a variable or method with the name `union`.
110121
> **<sup>Lexer 2015</sup>**\
111122
> KW_DYN : `dyn`
112123
124+
r[lex.keywords.weak.macro_rules]
113125
* `macro_rules` is used to create custom [macros].
126+
127+
r[lex.keywords.weak.union]
114128
* `union` is used to declare a [union] and is only a keyword when used in a
115129
union declaration.
130+
131+
r[lex.keywords.weak.lifetime-static]
116132
* `'static` is used for the static lifetime and cannot be used as a [generic
117133
lifetime parameter] or [loop label]
118134

119135
```compile_fail
120136
// error[E0262]: invalid lifetime parameter name: `'static`
121137
fn invalid_lifetime_parameter<'static>(s: &'static str) -> &'static str { s }
122138
```
139+
140+
r[lex.keywords.weak.dyn]
123141
* In the 2015 edition, [`dyn`] is a keyword when used in a type position
124142
followed by a path that does not start with `::` or `<`, a lifetime, a question mark, a `for`
125143
keyword or an opening parenthesis.
126144

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

147+
r[lex.keywords.weak.safe]
129148
* `safe` is used for functions and statics, which has meaning in [external blocks].
130149

150+
r[lex.keywords.weak.raw]
131151
* `raw` is used for [raw borrow operators], and is only a keyword when matching a raw borrow operator form (such as `&raw const expr` or `&raw mut expr`).
132152

133153
[items]: items.md

src/lifetime-elision.md

+53-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1+
r[lifetime-elision]
12
# Lifetime elision
23

34
Rust has rules that allow lifetimes to be elided in various places where the
45
compiler can infer a sensible default choice.
56

7+
r[lifetime-elision.function]
68
## Lifetime elision in functions
79

10+
r[lifetime-elision.function.intro]
811
In order to make common patterns more ergonomic, lifetime arguments can be
912
*elided* in [function item], [function pointer], and [closure trait] signatures.
1013
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
14+
15+
r[lifetime-elision.function.lifetimes-not-inferred]
16+
It is an error to elide lifetime parameters that cannot be inferred.
17+
18+
r[lifetime-elision.function.explicit-placeholder]
19+
The placeholder lifetime, `'_`, can also be used to have a lifetime inferred in the
20+
same way. For lifetimes in paths, using `'_` is preferred.
21+
22+
r[lifetime-elision.function.only-functions]
23+
Trait object lifetimes follow different rules discussed
1524
[below](#default-trait-object-lifetimes).
1625

26+
r[lifetime-elision.function.implicit-lifetime-parameters]
1727
* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
28+
29+
r[lifetime-elision.function.output-lifetime]
1830
* If there is exactly one lifetime used in the parameters (elided or not), that
1931
lifetime is assigned to *all* elided output lifetimes.
2032

33+
r[lifetime-elision.function.receiver-lifetime]
2134
In method signatures there is another rule
2235

2336
* If the receiver has type `&Self` or `&mut Self`, then the lifetime of that
@@ -76,29 +89,44 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
7689
# }
7790
```
7891

92+
r[lifetime-elision.trait-object]
7993
## Default trait object lifetimes
8094

95+
r[lifetime-elision.trait-object.intro]
8196
The assumed lifetime of references held by a [trait object] is called its
8297
_default object lifetime bound_. These were defined in [RFC 599] and amended in
8398
[RFC 1156].
8499

100+
r[lifetime-elision.trait-object.explicit-bound]
85101
These default object lifetime bounds are used instead of the lifetime parameter
86-
elision rules defined above when the lifetime bound is omitted entirely. If
87-
`'_` 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
88106
rules.
89107

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

112+
r[lifetime-elision.trait-object.containing-type-unique]
93113
* 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]
94116
* If there is more than one bound from the containing type then an explicit
95117
bound must be specified
96118

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

122+
r[lifetime-elision.trait-object.trait-unique]
99123
* If the trait is defined with a single lifetime _bound_ then that bound is
100124
used.
125+
126+
r[lifetime-elision.trait-object.static-lifetime]
101127
* If `'static` is used for any lifetime bound then `'static` is used.
128+
129+
r[lifetime-elision.trait-object.default]
102130
* If the trait has no lifetime bounds, then the lifetime is inferred in
103131
expressions and is `'static` outside of expressions.
104132

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

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

@@ -152,8 +181,10 @@ impl<'a> dyn Bar<'a> {}
152181
impl<'a> dyn Bar<'a> + 'a {}
153182
```
154183

155-
## `'static` lifetime elision
184+
r[lifetime-elision.const-static]
185+
## `const` and `static` elision
156186

187+
r[lifetime-elision.const-static.implicit-static]
157188
Both [constant] and [static] declarations of reference types have *implicit*
158189
`'static` lifetimes unless an explicit lifetime is specified. As such, the
159190
constant declarations involving `'static` above may be written without the
@@ -175,6 +206,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
175206
};
176207
```
177208

209+
r[lifetime-elision.const-static.fn-references]
178210
Note that if the `static` or `const` items include function or closure
179211
references, which themselves include references, the compiler will first try
180212
the standard elision rules. If it is unable to resolve the lifetimes by its
@@ -213,3 +245,17 @@ const RESOLVED_STATIC: &dyn Fn(&Foo, &Bar) -> &Baz = &somefunc;
213245
[RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
214246
[static]: items/static-items.md
215247
[trait object]: types/trait-object.md
248+
249+
<script>
250+
(function() {
251+
var fragments = {
252+
"#static-lifetime-elision": "lifetime-elision.html#const-and-static-elision",
253+
};
254+
var target = fragments[window.location.hash];
255+
if (target) {
256+
var url = window.location.toString();
257+
var base = url.substring(0, url.lastIndexOf('/'));
258+
window.location.replace(base + "/" + target);
259+
}
260+
})();
261+
</script>

src/macros.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
r[macro]
12
# Macros
23

4+
r[macro.intro]
35
The functionality and syntax of Rust can be extended with custom definitions
46
called macros. They are given names, and invoked through a consistent
57
syntax: `some_extension!(...)`.
@@ -10,8 +12,10 @@ There are two ways to define new macros:
1012
* [Procedural Macros] define function-like macros, custom derives, and custom
1113
attributes using functions that operate on input tokens.
1214

15+
r[macro.invocation]
1316
## Macro Invocation
1417

18+
r[macro.invocation.syntax]
1519
> **<sup>Syntax</sup>**\
1620
> _MacroInvocation_ :\
1721
> &nbsp;&nbsp; [_SimplePath_] `!` _DelimTokenTree_
@@ -29,17 +33,30 @@ There are two ways to define new macros:
2933
> &nbsp;&nbsp; | [_SimplePath_] `!` `[` _TokenTree_<sup>\*</sup> `]` `;`\
3034
> &nbsp;&nbsp; | [_SimplePath_] `!` `{` _TokenTree_<sup>\*</sup> `}`
3135
36+
r[macro.invocation.intro]
3237
A macro invocation expands a macro at compile time and replaces the
3338
invocation with the result of the macro. Macros may be invoked in the
3439
following situations:
3540

41+
r[macro.invocation.expr]
3642
* [Expressions] and [statements]
43+
44+
r[macro.invocation.pattern]
3745
* [Patterns]
46+
47+
r[macro.invocation.type]
3848
* [Types]
49+
50+
r[macro.invocation.item]
3951
* [Items] including [associated items]
52+
53+
r[macro.invocation.nested]
4054
* [`macro_rules`] transcribers
55+
56+
r[macro.invocation.extern]
4157
* [External blocks]
4258

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

src/memory-allocation-and-lifetime.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
r[alloc]
12
# Memory allocation and lifetime
23

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

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

0 commit comments

Comments
 (0)