Skip to content

Commit 8511cf4

Browse files
committed
1 parent c81cd07 commit 8511cf4

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

src/items/generics.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@
22

33
> **<sup>Syntax</sup>**\
44
> _Generics_ :\
5-
> &nbsp;&nbsp; `<` _GenericParams_ `>`
5+
> &nbsp;&nbsp; `<` _GenericParams_<sup>?</sup> `>`
66
>
77
> _GenericParams_ :\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; _LifetimeParams_\
9-
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_\
10-
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> ( _TypeParam_ `,` )<sup>\*</sup> _ConstParams_
8+
> &nbsp;&nbsp; (_GenericParam_ `,`)<sup>\*</sup> _GenericParam_ `,`<sup>?</sup>
119
>
12-
> _LifetimeParams_ :\
13-
> &nbsp;&nbsp; ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup>
10+
> _GenericParam_ :\
11+
> &nbsp;&nbsp; [_OuterAttribute_] <sup>\*</sup>\
12+
> &nbsp;&nbsp; (\
13+
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; _LifetimeParam_\
14+
> &nbsp;&nbsp; &nbsp;&nbsp; | _TypeParam_\
15+
> &nbsp;&nbsp; &nbsp;&nbsp; | _ConstParam_\
16+
> &nbsp;&nbsp; )
1417
>
1518
> _LifetimeParam_ :\
16-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [LIFETIME_OR_LABEL]&nbsp;( `:` [_LifetimeBounds_] )<sup>?</sup>
17-
>
18-
> _TypeParams_:\
19-
> &nbsp;&nbsp; ( _TypeParam_ `,` )<sup>\*</sup> _TypeParam_<sup>?</sup>
19+
> &nbsp;&nbsp; [LIFETIME_OR_LABEL]&nbsp;( `:` [_LifetimeBounds_] )<sup>?</sup>
2020
>
2121
> _TypeParam_ :\
22-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
23-
>
24-
> _ConstParams_:\
25-
> &nbsp;&nbsp; ( _ConstParam_ `,` )<sup>\*</sup> _ConstParam_<sup>?</sup>
22+
> &nbsp;&nbsp; [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
2623
>
2724
> _ConstParam_:\
28-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> `const` [IDENTIFIER] `:` [_Type_]
25+
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_]
2926
3027
Functions, type aliases, structs, enumerations, unions, traits, and
3128
implementations may be *parameterized* by types, constants, and lifetimes. These
@@ -91,12 +88,15 @@ referred to with path syntax.
9188
> &nbsp;&nbsp; _ForLifetimes_<sup>?</sup> [_Type_] `:` [_TypeParamBounds_]<sup>?</sup>
9289
>
9390
> _ForLifetimes_ :\
94-
> &nbsp;&nbsp; `for` `<` [_LifetimeParams_](#generic-parameters) `>`
91+
> &nbsp;&nbsp; `for` `<` [_GenericParams_](#generic-parameters) `>`
9592
9693
*Where clauses* provide another way to specify bounds on type and lifetime
9794
parameters as well as a way to specify bounds on types that aren't type
9895
parameters.
9996

97+
The `for` keyword can be used to introduce [higher-ranked lifetimes]. It only
98+
allows [_LifetimeParam_] parameters.
99+
100100
Bounds that don't use the item's parameters or higher-ranked lifetimes are
101101
checked when the item is defined. It is an error for such a bound to be false.
102102

@@ -141,6 +141,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
141141
[IDENTIFIER]: ../identifiers.md
142142
[LIFETIME_OR_LABEL]: ../tokens.md#lifetimes-and-loop-labels
143143

144+
[_LifetimeParam_]: #generic-parameters
144145
[_LifetimeBounds_]: ../trait-bounds.md
145146
[_Lifetime_]: ../trait-bounds.md
146147
[_OuterAttribute_]: ../attributes.md
@@ -150,6 +151,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
150151
[arrays]: ../types/array.md
151152
[const contexts]: ../const_eval.md#const-context
152153
[function pointers]: ../types/function-pointer.md
154+
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
153155
[raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
154156
[references]: ../types/pointer.md#shared-references-
155157
[repeat expressions]: ../expressions/array-expr.md

src/paths.md

+6-23
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,17 @@ mod m {
5050
>
5151
> _GenericArgs_ :\
5252
> &nbsp;&nbsp; &nbsp;&nbsp; `<` `>`\
53-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,`<sup>?</sup> `>`\
54-
> &nbsp;&nbsp; | `<` _GenericArgsTypes_ `,`<sup>?</sup> `>`\
55-
> &nbsp;&nbsp; | `<` _GenericArgsConsts_ `,`<sup>?</sup> `>`\
56-
> &nbsp;&nbsp; | `<` _GenericArgsBindings_ `,`<sup>?</sup> `>`\
57-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,`<sup>?</sup> `>`\
58-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsConsts_ `,`<sup>?</sup> `>`\
59-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsBindings_ `,`<sup>?</sup> `>`\
60-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,`<sup>?</sup> `>`\
61-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsBindings_ `,`<sup>?</sup> `>`\
62-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`<sup>?</sup> `>`\
63-
> &nbsp;&nbsp; | `<` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`<sup>?</sup> `>`\
64-
> &nbsp;&nbsp; | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`<sup>?</sup> `>`
53+
> &nbsp;&nbsp; | `<` ( _GenericArg_ `,` )<sup>\*</sup> _GenericArg_ `,`<sup>?</sup> `>`
6554
>
66-
> _GenericArgsLifetimes_ :\
67-
> &nbsp;&nbsp; [_Lifetime_] (`,` [_Lifetime_])<sup>\*</sup>
68-
>
69-
> _GenericArgsTypes_ :\
70-
> &nbsp;&nbsp; [_Type_] (`,` [_Type_])<sup>\*</sup>
71-
>
72-
> _GenericArgsConsts_ :\
73-
> &nbsp;&nbsp; _GenericArgsConst_ (`,` _GenericArgsConst_)<sup>\*</sup>
55+
> _GenericArg_ :\
56+
> &nbsp;&nbsp; [_Lifetime_] | [_Type_] | _GenericArgsConst_ | _GenericArgsBinding_
7457
>
7558
> _GenericArgsConst_ :\
7659
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]\
7760
> &nbsp;&nbsp; | [_LiteralExpression_]\
7861
> &nbsp;&nbsp; | `-` [_LiteralExpression_]\
7962
> &nbsp;&nbsp; | [_SimplePathSegment_]
8063
>
81-
> _GenericArgsBindings_ :\
82-
> &nbsp;&nbsp; _GenericArgsBinding_ (`,` _GenericArgsBinding_)<sup>\*</sup>
83-
>
8464
> _GenericArgsBinding_ :\
8565
> &nbsp;&nbsp; [IDENTIFIER] `=` [_Type_]
8666
@@ -95,6 +75,9 @@ ambiguity with the less-than operator. This is colloquially known as "turbofish"
9575
Vec::<u8>::with_capacity(1024);
9676
```
9777

78+
The order of generic arguments is restricted to lifetime arguments, then type
79+
arguments, then const arguments, then equality constraints.
80+
9881
Const arguments must be surrounded by braces unless they are a
9982
[literal] or a single segment path.
10083

0 commit comments

Comments
 (0)