Skip to content

Commit f8c39fe

Browse files
authored
Merge pull request #920 from ehuss/fix-generic-params
Update grammar for order of parameters/arguments.
2 parents 448002d + 9d0aa17 commit f8c39fe

11 files changed

+41
-63
lines changed

src/items/associated-items.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ let _: f64 = f64::from_i32(42);
8080
### Methods
8181

8282
> _Method_ :\
83-
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
83+
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
8484
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _SelfParam_ (`,` [_FunctionParam_])<sup>\*</sup> `,`<sup>?</sup> `)`\
8585
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>\
8686
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
@@ -344,7 +344,7 @@ fn main() {
344344
[_FunctionParam_]: functions.md
345345
[_FunctionQualifiers_]: functions.md
346346
[_FunctionReturnType_]: functions.md
347-
[_Generics_]: generics.md
347+
[_GenericParams_]: generics.md
348348
[_Lifetime_]: ../trait-bounds.md
349349
[_Type_]: ../types.md#type-expressions
350350
[_WhereClause_]: generics.md#where-clauses

src/items/enumerations.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
> _Enumeration_ :\
55
> &nbsp;&nbsp; `enum`
66
> [IDENTIFIER]&nbsp;
7-
> [_Generics_]<sup>?</sup>
7+
> [_GenericParams_]<sup>?</sup>
88
> [_WhereClause_]<sup>?</sup>
99
> `{` _EnumItems_<sup>?</sup> `}`
1010
>
@@ -172,7 +172,7 @@ enum E {
172172
```
173173

174174
[IDENTIFIER]: ../identifiers.md
175-
[_Generics_]: generics.md
175+
[_GenericParams_]: generics.md
176176
[_WhereClause_]: generics.md#where-clauses
177177
[_Expression_]: ../expressions.md
178178
[_TupleFields_]: structs.md

src/items/external-blocks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
> &nbsp;&nbsp; `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_] `;`
1818
>
1919
> _ExternalFunctionItem_ :\
20-
> &nbsp;&nbsp; `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
20+
> &nbsp;&nbsp; `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
2121
> &nbsp;&nbsp; `(` ( _NamedFunctionParameters_ | _NamedFunctionParametersWithVariadics_ )<sup>?</sup> `)`\
2222
> &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> `;`
2323
>
@@ -190,7 +190,7 @@ restrictions as [regular function parameters].
190190
[statics]: static-items.md
191191
[_Abi_]: functions.md
192192
[_FunctionReturnType_]: functions.md
193-
[_Generics_]: generics.md
193+
[_GenericParams_]: generics.md
194194
[_InnerAttribute_]: ../attributes.md
195195
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
196196
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax

src/items/functions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax</sup>**\
44
> _Function_ :\
5-
> &nbsp;&nbsp; _FunctionQualifiers_ `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
5+
> &nbsp;&nbsp; _FunctionQualifiers_ `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
66
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _FunctionParameters_<sup>?</sup> `)`\
77
> &nbsp;&nbsp; &nbsp;&nbsp; _FunctionReturnType_<sup>?</sup> [_WhereClause_]<sup>?</sup>\
88
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
@@ -341,7 +341,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
341341
[RAW_STRING_LITERAL]: ../tokens.md#raw-string-literals
342342
[STRING_LITERAL]: ../tokens.md#string-literals
343343
[_BlockExpression_]: ../expressions/block-expr.md
344-
[_Generics_]: generics.md
344+
[_GenericParams_]: generics.md
345345
[_Pattern_]: ../patterns.md
346346
[_Type_]: ../types.md#type-expressions
347347
[_WhereClause_]: generics.md#where-clauses

src/items/generics.md

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
# Generic parameters
22

33
> **<sup>Syntax</sup>**\
4-
> _Generics_ :\
5-
> &nbsp;&nbsp; `<` _GenericParams_ `>`
6-
>
74
> _GenericParams_ :\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; _LifetimeParams_\
9-
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> _TypeParams_\
10-
> &nbsp;&nbsp; | ( _LifetimeParam_ `,` )<sup>\*</sup> ( _TypeParam_ `,` )<sup>\*</sup> _ConstParams_
5+
> &nbsp;&nbsp; &nbsp;&nbsp; `<` `>`\
6+
> &nbsp;&nbsp; | `<` (_GenericParam_ `,`)<sup>\*</sup> _GenericParam_ `,`<sup>?</sup> `>`
117
>
12-
> _LifetimeParams_ :\
13-
> &nbsp;&nbsp; ( _LifetimeParam_ `,` )<sup>\*</sup> _LifetimeParam_<sup>?</sup>
8+
> _GenericParam_ :\
9+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( _LifetimeParam_ | _TypeParam_ | _ConstParam_ )
1410
>
1511
> _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>
12+
> &nbsp;&nbsp; [LIFETIME_OR_LABEL]&nbsp;( `:` [_LifetimeBounds_] )<sup>?</sup>
2013
>
2114
> _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>
15+
> &nbsp;&nbsp; [IDENTIFIER]( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> ( `=` [_Type_] )<sup>?</sup>
2616
>
2717
> _ConstParam_:\
28-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>?</sup> `const` [IDENTIFIER] `:` [_Type_]
18+
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_]
2919
3020
Functions, type aliases, structs, enumerations, unions, traits, and
3121
implementations may be *parameterized* by types, constants, and lifetimes. These
@@ -91,12 +81,15 @@ referred to with path syntax.
9181
> &nbsp;&nbsp; _ForLifetimes_<sup>?</sup> [_Type_] `:` [_TypeParamBounds_]<sup>?</sup>
9282
>
9383
> _ForLifetimes_ :\
94-
> &nbsp;&nbsp; `for` `<` [_LifetimeParams_](#generic-parameters) `>`
84+
> &nbsp;&nbsp; `for` [_GenericParams_](#generic-parameters)
9585
9686
*Where clauses* provide another way to specify bounds on type and lifetime
9787
parameters as well as a way to specify bounds on types that aren't type
9888
parameters.
9989

90+
The `for` keyword can be used to introduce [higher-ranked lifetimes]. It only
91+
allows [_LifetimeParam_] parameters.
92+
10093
Bounds that don't use the item's parameters or higher-ranked lifetimes are
10194
checked when the item is defined. It is an error for such a bound to be false.
10295

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

137+
[_LifetimeParam_]: #generic-parameters
144138
[_LifetimeBounds_]: ../trait-bounds.md
145139
[_Lifetime_]: ../trait-bounds.md
146140
[_OuterAttribute_]: ../attributes.md
@@ -150,6 +144,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
150144
[arrays]: ../types/array.md
151145
[const contexts]: ../const_eval.md#const-context
152146
[function pointers]: ../types/function-pointer.md
147+
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
153148
[raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
154149
[references]: ../types/pointer.md#shared-references-
155150
[repeat expressions]: ../expressions/array-expr.md

src/items/implementations.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
> &nbsp;&nbsp; _InherentImpl_ | _TraitImpl_
66
>
77
> _InherentImpl_ :\
8-
> &nbsp;&nbsp; `impl` [_Generics_]<sup>?</sup>&nbsp;[_Type_]&nbsp;[_WhereClause_]<sup>?</sup> `{`\
8+
> &nbsp;&nbsp; `impl` [_GenericParams_]<sup>?</sup>&nbsp;[_Type_]&nbsp;[_WhereClause_]<sup>?</sup> `{`\
99
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
1010
> &nbsp;&nbsp; &nbsp;&nbsp; _InherentImplItem_<sup>\*</sup>\
1111
> &nbsp;&nbsp; `}`
@@ -17,7 +17,7 @@
1717
> &nbsp;&nbsp; )
1818
>
1919
> _TraitImpl_ :\
20-
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `impl` [_Generics_]<sup>?</sup> `!`<sup>?</sup>
20+
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `impl` [_GenericParams_]<sup>?</sup> `!`<sup>?</sup>
2121
> [_TypePath_] `for` [_Type_]\
2222
> &nbsp;&nbsp; [_WhereClause_]<sup>?</sup>\
2323
> &nbsp;&nbsp; `{`\
@@ -206,7 +206,7 @@ attributes].
206206

207207
[_ConstantItem_]: constant-items.md
208208
[_Function_]: functions.md
209-
[_Generics_]: generics.md
209+
[_GenericParams_]: generics.md
210210
[_InnerAttribute_]: ../attributes.md
211211
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
212212
[_Method_]: associated-items.md#methods

src/items/structs.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
> _StructStruct_ :\
99
> &nbsp;&nbsp; `struct`
1010
> [IDENTIFIER]&nbsp;
11-
> [_Generics_]<sup>?</sup>
11+
> [_GenericParams_]<sup>?</sup>
1212
> [_WhereClause_]<sup>?</sup>
1313
> ( `{` _StructFields_<sup>?</sup> `}` | `;` )
1414
>
1515
> _TupleStruct_ :\
1616
> &nbsp;&nbsp; `struct`
1717
> [IDENTIFIER]&nbsp;
18-
> [_Generics_]<sup>?</sup>
18+
> [_GenericParams_]<sup>?</sup>
1919
> `(` _TupleFields_<sup>?</sup> `)`
2020
> [_WhereClause_]<sup>?</sup>
2121
> `;`
@@ -82,7 +82,7 @@ particular layout using the [`repr` attribute].
8282

8383
[_OuterAttribute_]: ../attributes.md
8484
[IDENTIFIER]: ../identifiers.md
85-
[_Generics_]: generics.md
85+
[_GenericParams_]: generics.md
8686
[_WhereClause_]: generics.md#where-clauses
8787
[_Visibility_]: ../visibility-and-privacy.md
8888
[_Type_]: ../types.md#type-expressions

src/items/traits.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
> **<sup>Syntax</sup>**\
44
> _Trait_ :\
55
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `trait` [IDENTIFIER]&nbsp;
6-
> [_Generics_]<sup>?</sup>
6+
> [_GenericParams_]<sup>?</sup>
77
> ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup>
88
> [_WhereClause_]<sup>?</sup> `{`\
99
> &nbsp;&nbsp;&nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
@@ -26,12 +26,12 @@
2626
> &nbsp;&nbsp; &nbsp;&nbsp; _TraitMethodDecl_ ( `;` | [_BlockExpression_] )
2727
>
2828
> _TraitFunctionDecl_ :\
29-
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
29+
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
3030
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _TraitFunctionParameters_<sup>?</sup> `)`\
3131
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
3232
>
3333
> _TraitMethodDecl_ :\
34-
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
34+
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
3535
> &nbsp;&nbsp; &nbsp;&nbsp; `(` [_SelfParam_] (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup> `)`\
3636
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
3737
>
@@ -339,7 +339,7 @@ fn main() {
339339
[_Expression_]: ../expressions.md
340340
[_FunctionQualifiers_]: functions.md
341341
[_FunctionReturnType_]: functions.md
342-
[_Generics_]: generics.md
342+
[_GenericParams_]: generics.md
343343
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
344344
[_OuterAttribute_]: ../attributes.md
345345
[_InnerAttribute_]: ../attributes.md

src/items/type-aliases.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax</sup>**\
44
> _TypeAlias_ :\
5-
> &nbsp;&nbsp; `type` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>
5+
> &nbsp;&nbsp; `type` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>
66
> [_WhereClause_]<sup>?</sup> `=` [_Type_] `;`
77
88
A _type alias_ defines a new name for an existing [type]. Type aliases are
@@ -33,6 +33,6 @@ let _ = TypeAlias(5); // Doesn't work
3333
```
3434

3535
[IDENTIFIER]: ../identifiers.md
36-
[_Generics_]: generics.md
36+
[_GenericParams_]: generics.md
3737
[_WhereClause_]: generics.md#where-clauses
3838
[_Type_]: ../types.md#type-expressions

src/items/unions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax</sup>**\
44
> _Union_ :\
5-
> &nbsp;&nbsp; `union` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
5+
> &nbsp;&nbsp; `union` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
66
> `{`[_StructFields_] `}`
77
88
A union declaration uses the same syntax as a struct declaration, except with
@@ -177,7 +177,7 @@ generics, trait implementations, inherent implementations, coherence, pattern
177177
checking, etc etc etc).
178178

179179
[IDENTIFIER]: ../identifiers.md
180-
[_Generics_]: generics.md
180+
[_GenericParams_]: generics.md
181181
[_WhereClause_]: generics.md#where-clauses
182182
[_StructFields_]: structs.md
183183
[`transmute`]: ../../std/mem/fn.transmute.html

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)