Skip to content

Commit f3627a2

Browse files
authored
Merge pull request rust-lang#2257 from fmease/fix-bounds-grammars
Fix grammar rules containing or pertaining to bounds
2 parents f6f674f + 362c0ee commit f3627a2

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/items/generics.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GenericParams -> `<` ( GenericParam (`,` GenericParam)* `,`? )? `>`
77
88
GenericParam -> OuterAttribute* ( LifetimeParam | TypeParam | ConstParam )
99
10-
LifetimeParam -> Lifetime ( `:` LifetimeBounds )?
10+
LifetimeParam -> Lifetime ( `:` LifetimeBounds? )?
1111
1212
TypeParam -> IDENTIFIER ( `:` Bounds? )? ( `=` Type )?
1313
@@ -234,7 +234,7 @@ WhereClauseItem ->
234234
LifetimeWhereClauseItem
235235
| TypeBoundWhereClauseItem
236236
237-
LifetimeWhereClauseItem -> Lifetime `:` LifetimeBounds
237+
LifetimeWhereClauseItem -> Lifetime `:` LifetimeBounds?
238238
239239
TypeBoundWhereClauseItem -> ForLifetimes? Type `:` Bounds?
240240
```

src/items/type-aliases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ r[items.type]
44
r[items.type.syntax]
55
```grammar,items
66
TypeAlias ->
7-
`type` IDENTIFIER GenericParams? ( `:` Bounds )?
7+
`type` IDENTIFIER GenericParams? ( `:` Bounds? )?
88
WhereClause?
99
( `=` Type WhereClause?)? `;`
1010
```

src/paths.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ GenericArgsBinding ->
7474
TypePathSegment `=` Type
7575
7676
GenericArgsBounds ->
77-
TypePathSegment `:` Bounds
77+
TypePathSegment `:` Bounds?
7878
```
7979

8080
r[paths.expr.intro]

src/types/impl-trait.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ r[type.impl-trait]
33

44
r[type.impl-trait.syntax]
55
```grammar,types
6-
ImplTraitType -> `impl` Bounds
6+
ImplTraitType -> `impl` Bounds?
77
8-
ImplTraitTypeOneBound -> `impl` TraitBound
8+
ImplTraitTypeOneBound -> `impl` TraitBound?
99
```
1010

1111
r[type.impl-trait.intro]
@@ -23,6 +23,10 @@ fn foo(arg: impl Trait) {
2323
fn bar() -> impl Trait {
2424
}
2525
```
26+
27+
r[type.impl-trait.bounds]
28+
There must be at least one trait bound, no more than one `use<..>` bound, and no more than one opt-out bound (e.g., `?Sized`).
29+
2630
r[type.impl-trait.param]
2731
## Anonymous type parameters
2832

src/types/trait-object.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ r[type.trait-object]
33

44
r[type.trait-object.syntax]
55
```grammar,types
6-
TraitObjectType -> `dyn`? Bounds
6+
TraitObjectType -> Bounds[^bare-2021] | `dyn`[^dyn-2018] Bounds?
77
8-
TraitObjectTypeOneBound -> `dyn`? TraitBound
8+
TraitObjectTypeOneBound -> TraitBound[^bare-2021] | `dyn`[^dyn-2018] TraitBound?
99
```
1010

11+
[^bare-2021]: See [type.trait-object.syntax-edition2021].
12+
[^dyn-2018]: See [type.trait-object.syntax-edition2018].
13+
1114
r[type.trait-object.intro]
1215
A *trait object* is an opaque value of another type that implements a set of traits. The set of traits is made up of a [dyn compatible] *base trait* plus any number of [auto traits].
1316

1417
r[type.trait-object.impls]
1518
Trait objects implement the base trait, its auto traits, and any [supertraits] of the base trait.
1619

17-
r[type.trait-object.name]
18-
Trait objects are written as the keyword `dyn` followed by a set of trait bounds, but with the following restrictions on the trait bounds.
19-
20-
r[type.trait-object.constraint]
21-
There may not be more than one non-auto trait, no more than one lifetime, and opt-out bounds (e.g. `?Sized`) are not allowed. Furthermore, paths to traits may be parenthesized.
20+
r[type.trait-object.bounds]
21+
There must be at least one trait bound, there may not be more than one non-auto trait, no more than one lifetime, and opt-out bounds (e.g., `?Sized`) and `use<..>` bounds are not allowed.
2222

2323
For example, given a trait `Trait`, the following are all trait objects:
2424

@@ -33,11 +33,13 @@ For example, given a trait `Trait`, the following are all trait objects:
3333

3434
r[type.trait-object.syntax-edition2021]
3535
> [!EDITION-2021]
36-
> Before the 2021 edition, the `dyn` keyword may be omitted.
36+
> Before the 2021 edition, the `dyn` keyword may be omitted. In the 2021 edition and beyond, the `dyn` keyword is required semantically.
3737
3838
r[type.trait-object.syntax-edition2018]
3939
> [!EDITION-2018]
40-
> In the 2015 edition, if the first bound of the trait object is a path that starts with `::`, then the `dyn` will be treated as a part of the path. The first path can be put in parenthesis to get around this. As such, if you want a trait object with the trait `::your_module::Trait`, you should write it as `dyn (::your_module::Trait)`.
40+
> In the 2015 edition, `dyn` must be followed by [PathIdentSegment], [LIFETIME_TOKEN], `for`, `(` or `?` to be interpreted as a keyword instead of a regular identifier.
41+
>
42+
> Most notably, `dyn`, `dyn::T` and `dyn<T>` will all be treated as type paths. As such, if you want a trait object type with the trait `::module::Trait`, you need to put the path in parentheses and write it as `dyn (::module::Trait)`.
4143
>
4244
> Beginning in the 2018 edition, `dyn` is a true keyword and is not allowed in paths, so the parentheses are not necessary.
4345

0 commit comments

Comments
 (0)