Skip to content

Commit d96b228

Browse files
authored
Merge branch 'master' into spec-add-identifiers-items
2 parents 1618f02 + df36291 commit d96b228

18 files changed

+95
-51
lines changed

docs/authoring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,4 @@ The reference does not document which targets exist, or the properties of specif
154154

155155
### Editions
156156

157-
The main text and flow should document only the current edition. Whenever there is a difference between editions, the differences should be called out with an "Edition Differences" block.
157+
The main text and flow should document only the current edition. Whenever there is a difference between editions, the differences should be called out with an "Edition differences" block.

mdbook-spec/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rust_2018_idioms, unused_lifetimes)]
2+
13
use mdbook::book::{Book, Chapter};
24
use mdbook::errors::Error;
35
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
@@ -82,8 +84,8 @@ impl Spec {
8284
}
8385
}
8486
format!(
85-
"<div class=\"rule\" id=\"{rule_id}\">\
86-
<a class=\"rule-link\" href=\"#{rule_id}\">[{rule_id}]</a>\
87+
"<div class=\"rule\" id=\"r-{rule_id}\">\
88+
<a class=\"rule-link\" href=\"#r-{rule_id}\">[{rule_id}]</a>\
8789
</div>\n"
8890
)
8991
})
@@ -102,7 +104,7 @@ impl Spec {
102104
.iter()
103105
.map(|(rule_id, (_, path))| {
104106
let relative = pathdiff::diff_paths(path, current_path).unwrap();
105-
format!("[{rule_id}]: {}#{rule_id}\n", relative.display())
107+
format!("[{rule_id}]: {}#r-{rule_id}\n", relative.display())
106108
})
107109
.collect();
108110
format!(

mdbook-spec/src/std_links.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn collect_markdown_links(chapter: &Chapter) -> Vec<Link<'_>> {
145145
// Broken links are collected so that you can write something like
146146
// `[std::option::Option]` which in pulldown_cmark's eyes is a broken
147147
// link. However, that is the normal syntax for rustdoc.
148-
let broken_link = |broken_link: BrokenLink| {
148+
let broken_link = |broken_link: BrokenLink<'_>| {
149149
broken_links.push(Link {
150150
link_type: broken_link.link_type,
151151
// Necessary due to lifetime issues.
@@ -205,7 +205,7 @@ fn collect_markdown_links(chapter: &Chapter) -> Vec<Link<'_>> {
205205
/// generate intra-doc links on them.
206206
///
207207
/// The output will be in the given `tmp` directory.
208-
fn run_rustdoc(tmp: &TempDir, chapter_links: &HashMap<&PathBuf, Vec<Link>>) {
208+
fn run_rustdoc(tmp: &TempDir, chapter_links: &HashMap<&PathBuf, Vec<Link<'_>>>) {
209209
let src_path = tmp.path().join("a.rs");
210210
// Allow redundant since there could some in-scope things that are
211211
// technically not necessary, but we don't care about (like

src/const_eval.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,24 @@ A _const context_ is one of the following:
6161
* A [const generic argument]
6262
* A [const block]
6363

64+
Const contexts that are used as parts of types (array type and repeat length
65+
expressions as well as const generic arguments) can only make restricted use of
66+
surrounding generic parameters: such an expression must either be a single bare
67+
const generic parameter, or an arbitrary expression not making use of any
68+
generics.
69+
6470
## Const Functions
6571

6672
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
6773
`const` has no effect on any existing uses, it only restricts the types that arguments and the
68-
return type may use, as well as prevent various expressions from being used within it. You can freely
69-
do anything with a const function that you can do with a regular function.
74+
return type may use, and restricts the function body to constant expressions.
7075

7176
When called from a const context, the function is interpreted by the
7277
compiler at compile time. The interpretation happens in the
7378
environment of the compilation target and not the host. So `usize` is
7479
`32` bits if you are compiling against a `32` bit system, irrelevant
7580
of whether you are building on a `64` bit or a `32` bit system.
7681

77-
Const functions have various restrictions to make sure that they can be
78-
evaluated at compile-time. It is, for example, not possible to write a random
79-
number generator as a const function. Calling a const function at compile-time
80-
will always yield the same result as calling it at runtime, even when called
81-
multiple times. There's one exception to this rule: if you are doing complex
82-
floating point operations in extreme situations, then you might get (very
83-
slightly) different results. It is advisable to not make array lengths and enum
84-
discriminants depend on floating point computations.
85-
86-
87-
Notable features that are allowed in const contexts but not in const functions include:
88-
89-
* floating point operations
90-
* floating point values are treated just like generic parameters without trait bounds beyond
91-
`Copy`. So you cannot do anything with them but copy/move them around.
92-
93-
Conversely, the following are possible in a const function, but not in a const context:
94-
95-
* Use of generic type and lifetime parameters.
96-
* Const contexts do allow limited use of [const generic parameters].
97-
9882
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
9983
[array expressions]: expressions/array-expr.md
10084
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions

src/expressions/method-call-expr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Once a method is looked up, if it can't be called for one (or more) of those rea
6666
If a step is reached where there is more than one possible method, such as where generic methods or traits are considered the same, then it is a compiler error.
6767
These cases require a [disambiguating function call syntax] for method and function invocation.
6868
69-
> **Edition Differences**: Before the 2021 edition, during the search for visible methods, if the candidate receiver type is an [array type], methods provided by the standard library [`IntoIterator`] trait are ignored.
69+
> **Edition differences**: Before the 2021 edition, during the search for visible methods, if the candidate receiver type is an [array type], methods provided by the standard library [`IntoIterator`] trait are ignored.
7070
>
7171
> The edition used for this purpose is determined by the token representing the method name.
7272
>

src/introduction.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ These conventions are documented here.
8282

8383
An *example term* is an example of a term being defined.
8484

85-
* Differences in the language by which edition the crate is compiled under are in a blockquote that start with the words "Edition Differences:" in **bold**.
85+
* Differences in the language by which edition the crate is compiled under are in a blockquote that start with the words "Edition differences:" in **bold**.
8686

87-
> **Edition Differences**: In the 2015 edition, this syntax is valid that is disallowed as of the 2018 edition.
87+
> **Edition differences**: In the 2015 edition, this syntax is valid that is disallowed as of the 2018 edition.
8888
8989
* Notes that contain useful information about the state of the book or point out useful, but mostly out of scope, information are in blockquotes that start with the word "Note:" in **bold**.
9090

@@ -120,6 +120,15 @@ These conventions are documented here.
120120
121121
See [Notation] for more detail.
122122

123+
* Rule identifiers appear before each language rule enclosed in square brackets. These identifiers provide a way to refer to a specific rule in the language. The rule identifier uses periods to separate sections from most general to most specific ([destructors.scope.nesting.function-body] for example).
124+
125+
The rule name can be clicked to link to that rule.
126+
127+
r[example.rule.label]
128+
129+
> [!WARNING]
130+
> The organization of the rules is currently in flux. For the time being, these identifier names are not stable between releases, and links to these rules may fail if they are changed. We intend to stabilize these once the organization has settled so that links to the rule names will not break between releases.
131+
123132
## Contributing
124133

125134
We welcome contributions of all kinds.

src/items/associated-items.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ let circle_shape = Circle::new();
214214
let bounding_box = circle_shape.bounding_box();
215215
```
216216

217+
217218
r[items.associated.fn.params-edition2015]
218219
> **Edition Differences**: In the 2015 edition, it is possible to declare trait
219220
> methods with anonymous parameters (e.g. `fn foo(u8)`). This is deprecated and

src/macros-by-example.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ the `expr` fragment specifier. However, `_` is matched by the `expr` fragment
148148
specifier when it appears as a subexpression.
149149
For the same reason, a standalone [const block] is not matched but it is matched when appearing as a subexpression.
150150

151-
> **Edition Differences**: Starting with the 2021 edition, `pat` fragment-specifiers match top-level or-patterns (that is, they accept [_Pattern_]).
151+
> **Edition differences**: Starting with the 2021 edition, `pat` fragment-specifiers match top-level or-patterns (that is, they accept [_Pattern_]).
152152
>
153153
> Before the 2021 edition, they match exactly the same fragments as `pat_param` (that is, they accept [_PatternNoTopAlt_]).
154154
>
@@ -421,7 +421,7 @@ macro_rules! call_foo {
421421
fn foo() {}
422422
```
423423

424-
> **Version & Edition Differences**: Prior to Rust 1.30, `$crate` and
424+
> **Version & Edition differences**: Prior to Rust 1.30, `$crate` and
425425
> `local_inner_macros` (below) were unsupported. They were added alongside
426426
> path-based imports of macros (described above), to ensure that helper macros
427427
> did not need to be manually imported by users of a macro-exporting crate.
@@ -475,7 +475,7 @@ Matchers like `$i:expr,` or `$i:expr;` would be legal, however, because `,` and
475475
`ident`, `ty`, or `path` fragment specifier.
476476
* All other fragment specifiers have no restrictions.
477477

478-
> **Edition Differences**: Before the 2021 edition, `pat` may also be followed by `|`.
478+
> **Edition differences**: Before the 2021 edition, `pat` may also be followed by `|`.
479479
480480
When repetitions are involved, then the rules apply to every possible number of
481481
expansions, taking separators into account. This means:

src/names/preludes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ new_name`, then the symbol `new_name` is instead added to the prelude.
4444
The [`core`] crate is always added to the extern prelude. The [`std`] crate is
4545
added as long as the [`no_std` attribute] is not specified in the crate root.
4646

47-
> **Edition Differences**: In the 2015 edition, crates in the extern prelude
47+
> **Edition differences**: In the 2015 edition, crates in the extern prelude
4848
> cannot be referenced via [use declarations], so it is generally standard
4949
> practice to include `extern crate` declarations to bring them into scope.
5050
>
@@ -132,7 +132,7 @@ module or any of its descendants.
132132
133133
This attribute does not affect the [language prelude].
134134
135-
> **Edition Differences**: In the 2015 edition, the `no_implicit_prelude`
135+
> **Edition differences**: In the 2015 edition, the `no_implicit_prelude`
136136
> attribute does not affect the [`macro_use` prelude], and all macros exported
137137
> from the standard library are still included in the `macro_use` prelude.
138138
> Starting in the 2018 edition, it will remove the `macro_use` prelude.

src/names/scopes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ type FnExample = for<'a> fn(x: Example<'a>) -> Example<'a>;
184184
#
185185
// The `impl Trait2` here is not allowed to refer to 'b but it is allowed to
186186
// refer to 'a.
187-
fn foo<'a>() -> impl for<'b> Trait1<Item = impl Trait2<'a>> {
187+
fn foo<'a>() -> impl for<'b> Trait1<Item = impl Trait2<'a> + use<'a>> {
188188
// ...
189189
# Example
190190
}

src/paths.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Paths starting with `::` are considered to be *global paths* where the segments
166166
start being resolved from a place which differs based on edition. Each identifier in
167167
the path must resolve to an item.
168168

169-
> **Edition Differences**: In the 2015 Edition, identifiers resolve from the "crate root"
169+
> **Edition differences**: In the 2015 Edition, identifiers resolve from the "crate root"
170170
> (`crate::` in the 2018 edition), which contains a variety of different items, including
171171
> external crates, default crates such as `std` or `core`, and items in the top level of
172172
> the crate (including `use` imports).

src/patterns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ For example, `0u8..=255u8` is irrefutable.
533533
The range of values for an integer type is the closed range from its minimum to maximum value.
534534
The range of values for a `char` type are precisely those ranges containing all Unicode Scalar Values: `'\u{0000}'..='\u{D7FF}'` and `'\u{E000}'..='\u{10FFFF}'`.
535535

536-
> **Edition Differences**: Before the 2021 edition, range patterns with both a lower and upper bound may also be written using `...` in place of `..=`, with the same meaning.
536+
> **Edition differences**: Before the 2021 edition, range patterns with both a lower and upper bound may also be written using `...` in place of `..=`, with the same meaning.
537537
538538
## Reference patterns
539539

src/tokens.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ c"\u{00E6}";
375375
c"\xC3\xA6";
376376
```
377377

378-
> **Edition Differences**: C string literals are accepted in the 2021 edition or
378+
> **Edition differences**: C string literals are accepted in the 2021 edition or
379379
> later. In earlier additions the token `c""` is lexed as `c ""`.
380380
381381
#### Raw C string literals
@@ -400,7 +400,7 @@ encoding. The characters `U+0022` (double-quote) (except when followed by at
400400
least as many `U+0023` (`#`) characters as were used to start the raw C string
401401
literal) or `U+005C` (`\`) do not have any special meaning.
402402

403-
> **Edition Differences**: Raw C string literals are accepted in the 2021
403+
> **Edition differences**: Raw C string literals are accepted in the 2021
404404
> edition or later. In earlier additions the token `cr""` is lexed as `cr ""`,
405405
> and `cr#""#` is lexed as `cr #""#` (which is non-grammatical).
406406
@@ -735,7 +735,7 @@ Note that raw identifiers, raw string literals, and raw byte string literals may
735735

736736
Similarly the `r`, `b`, `br`, `c`, and `cr` prefixes used in raw string literals, byte literals, byte string literals, raw byte string literals, C string literals, and raw C string literals are not interpreted as reserved prefixes.
737737

738-
> **Edition Differences**: Starting with the 2021 edition, reserved prefixes are reported as an error by the lexer (in particular, they cannot be passed to macros).
738+
> **Edition differences**: Starting with the 2021 edition, reserved prefixes are reported as an error by the lexer (in particular, they cannot be passed to macros).
739739
>
740740
> Before the 2021 edition, reserved prefixes are accepted by the lexer and interpreted as multiple tokens (for example, one token for the identifier or keyword, followed by a `#` token).
741741
>

src/trait-bounds.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
> &nbsp;&nbsp; _TypeParamBound_ ( `+` _TypeParamBound_ )<sup>\*</sup> `+`<sup>?</sup>
66
>
77
> _TypeParamBound_ :\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; _Lifetime_ | _TraitBound_
8+
> &nbsp;&nbsp; &nbsp;&nbsp; _Lifetime_ | _TraitBound_ | _UseBound_
99
>
1010
> _TraitBound_ :\
1111
> &nbsp;&nbsp; &nbsp;&nbsp; `?`<sup>?</sup>
@@ -19,6 +19,21 @@
1919
> _Lifetime_ :\
2020
> &nbsp;&nbsp; &nbsp;&nbsp; [LIFETIME_OR_LABEL]\
2121
> &nbsp;&nbsp; | `'static`
22+
>
23+
> _UseBound_ :\
24+
> &nbsp;&nbsp; `use` _UseBoundGenericArgs_
25+
>
26+
> _UseBoundGenericArgs_ :\
27+
> &nbsp;&nbsp; &nbsp;&nbsp; `<` `>` \
28+
> &nbsp;&nbsp; | `<` \
29+
> &nbsp;&nbsp; &nbsp;&nbsp; ( _UseBoundGenericArg_ `,`)<sup>\*</sup> \
30+
> &nbsp;&nbsp; &nbsp;&nbsp; _UseBoundGenericArg_ `,`<sup>?</sup> \
31+
> &nbsp;&nbsp; &nbsp;&nbsp; `>`
32+
>
33+
> _UseBoundGenericArg_ :\
34+
> &nbsp;&nbsp; &nbsp;&nbsp; _Lifetime_ \
35+
> &nbsp;&nbsp; | [IDENTIFIER][] \
36+
> &nbsp;&nbsp; | `Self`
2237
2338
[Trait] and lifetime bounds provide a way for [generic items][generic] to
2439
restrict which types and lifetimes are used as their parameters. Bounds can be
@@ -227,20 +242,26 @@ trait Trait<'a, T: 'a> {}
227242
impl<'a, T> Trait<'a, T> for &'a T {}
228243
```
229244

245+
## Use bounds
246+
247+
Certain bounds lists may include a `use<..>` bound to control which generic parameters are captured by the `impl Trait` [abstract return type]. See [precise capturing] for more details.
230248

249+
[IDENTIFIER]: identifiers.html
231250
[LIFETIME_OR_LABEL]: tokens.md#lifetimes-and-loop-labels
232251
[_GenericParams_]: items/generics.md
233252
[_TypePath_]: paths.md#paths-in-types
234253
[`Clone`]: special-types-and-traits.md#clone
235254
[`Copy`]: special-types-and-traits.md#copy
236255
[`Sized`]: special-types-and-traits.md#sized
237256

257+
[abstract return type]: types/impl-trait.md#abstract-return-types
238258
[arrays]: types/array.md
239259
[associated types]: items/associated-items.md#associated-types
240260
[hrtb-scopes]: names/scopes.md#higher-ranked-trait-bound-scopes
241261
[supertraits]: items/traits.md#supertraits
242262
[generic]: items/generics.md
243263
[higher-ranked lifetimes]: #higher-ranked-trait-bounds
264+
[precise capturing]: types/impl-trait.md#precise-capturing
244265
[slice]: types/slice.md
245266
[Trait]: items/traits.md#trait-bounds
246267
[trait object]: types/trait-object.md

src/types/enum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Enum types cannot be denoted *structurally* as types, but must be denoted by
1616
named reference to an [`enum` item].
1717

1818
[^enumtype]: The `enum` type is analogous to a `data` constructor declaration in
19-
ML, or a *pick ADT* in Limbo.
19+
Haskell, or a *pick ADT* in Limbo.
2020

2121
[`enum` item]: ../items/enumerations.md
2222
[struct expression]: ../expressions/struct-expr.md

src/types/impl-trait.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,33 @@ Functions in traits may also use `impl Trait` as a syntax for an anonymous assoc
9191

9292
Every `impl Trait` in the return type of an associated function in a trait is desugared to an anonymous associated type. The return type that appears in the implementation's function signature is used to determine the value of the associated type.
9393

94-
### Differences between generics and `impl Trait` in return position
94+
## Capturing
95+
96+
Behind each return-position `impl Trait` abstract type is some hidden concrete type. For this concrete type to use a generic parameter, that generic parameter must be *captured* by the abstract type.
97+
98+
## Automatic capturing
99+
100+
Return-position `impl Trait` abstract types automatically capture certain of the in-scope generic parameters. Everywhere, these automatically capture all in-scope type and const generic parameters.
101+
102+
On items of trait impls and trait definitions, these types additionally automatically capture all in-scope generic lifetime parameters, including higher-ranked ones. On free functions and on associated functions and methods of inherent impls, only the generic lifetime parameters that appear in the bounds of abstract return type are captured.
103+
104+
## Precise capturing
105+
106+
The set of generic parameters captured by a return-position `impl Trait` abstract type may be explicitly controlled with a [`use<..>` bound]. If present, only the generic parameters listed in the `use<..>` bound will be captured. E.g.:
107+
108+
```rust
109+
fn capture<'a, 'b, T>(x: &'a (), y: T) -> impl Sized + use<'a, T> {
110+
// ~~~~~~~~~~~~~~~~~~~~~~~
111+
// Captures `'a` and `T` only.
112+
(x, y)
113+
}
114+
```
115+
116+
Currently, only one `use<..>` bound may be present in a bounds list, such bounds are not allowed in the signature of items of a trait definition, all in-scope type and const generic parameters must be included, and all lifetime parameters that appear in other bounds of the abstract type must be included. Within the `use<..>` bound, any lifetime parameters present must appear before all type and const generic parameters, and the elided lifetime (`'_`) may be present if it is otherwise allowed to appear within the `impl Trait` return type.
117+
118+
Because all in-scope type parameters must be included by name, a `use<..>` bound may not be used in the signature of items that use argument-position `impl Trait`, as those items have anonymous type parameters in scope.
119+
120+
## Differences between generics and `impl Trait` in return position
95121

96122
In argument position, `impl Trait` is very similar in semantics to a generic type parameter.
97123
However, there are significant differences between the two in return position.
@@ -127,9 +153,10 @@ Instead, the function chooses the return type, but only promises that it will im
127153
`impl Trait` can only appear as a parameter or return type of a non-`extern` function.
128154
It cannot be the type of a `let` binding, field type, or appear inside a type alias.
129155

130-
[closures]: closure.md
131156
[_GenericArgs_]: ../paths.md#paths-in-expressions
132157
[_GenericParams_]: ../items/generics.md
133158
[_TraitBound_]: ../trait-bounds.md
134-
[trait object]: trait-object.md
135159
[_TypeParamBounds_]: ../trait-bounds.md
160+
[`use<..>` bound]: ../trait-bounds.md#use-bounds
161+
[closures]: closure.md
162+
[trait object]: trait-object.md

0 commit comments

Comments
 (0)