From 8511cf48f93fc37c44a87f5823cce1338da1bb7e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 1 Jan 2021 15:52:08 -0800 Subject: [PATCH] Update grammar for order of parameters/arguments. Generic param order restriction was changed in https://github.com/rust-lang/rust/pull/58191. Generic argument order restriction was changed in https://github.com/rust-lang/rust/pull/70261. `for` lifetime argument restriction was changed in https://github.com/rust-lang/rust/pull/48326. Generic parameter parsing: https://github.com/rust-lang/rust/blob/206ee1eea3467fd1d7f1efdbeafe27880897bb2c/compiler/rustc_parse/src/parser/generics.rs#L83-L153 Generic argument parsing: https://github.com/rust-lang/rust/blob/17eec1433c69972844dd228b5fe801f218e118c3/compiler/rustc_parse/src/parser/path.rs#L395-L413 `for` argument parsing: https://github.com/rust-lang/rust/blob/206ee1eea3467fd1d7f1efdbeafe27880897bb2c/compiler/rustc_parse/src/parser/ty.rs#L724-L736 Fixes #785 --- src/items/generics.md | 34 ++++++++++++++++++---------------- src/paths.md | 29 ++++++----------------------- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/items/generics.md b/src/items/generics.md index abb50b47f..a798d91f7 100644 --- a/src/items/generics.md +++ b/src/items/generics.md @@ -2,30 +2,27 @@ > **Syntax**\ > _Generics_ :\ ->    `<` _GenericParams_ `>` +>    `<` _GenericParams_? `>` > > _GenericParams_ :\ ->       _LifetimeParams_\ ->    | ( _LifetimeParam_ `,` )\* _TypeParams_\ ->    | ( _LifetimeParam_ `,` )\* ( _TypeParam_ `,` )\* _ConstParams_ +>    (_GenericParam_ `,`)\* _GenericParam_ `,`? > -> _LifetimeParams_ :\ ->    ( _LifetimeParam_ `,` )\* _LifetimeParam_? +> _GenericParam_ :\ +>    [_OuterAttribute_] \*\ +>    (\ +>          _LifetimeParam_\ +>       | _TypeParam_\ +>       | _ConstParam_\ +>    ) > > _LifetimeParam_ :\ ->    [_OuterAttribute_]? [LIFETIME_OR_LABEL] ( `:` [_LifetimeBounds_] )? -> -> _TypeParams_:\ ->    ( _TypeParam_ `,` )\* _TypeParam_? +>    [LIFETIME_OR_LABEL] ( `:` [_LifetimeBounds_] )? > > _TypeParam_ :\ ->    [_OuterAttribute_]? [IDENTIFIER]( `:` [_TypeParamBounds_]? )? ( `=` [_Type_] )? -> -> _ConstParams_:\ ->    ( _ConstParam_ `,` )\* _ConstParam_? +>    [IDENTIFIER]( `:` [_TypeParamBounds_]? )? ( `=` [_Type_] )? > > _ConstParam_:\ ->    [_OuterAttribute_]? `const` [IDENTIFIER] `:` [_Type_] +>    `const` [IDENTIFIER] `:` [_Type_] Functions, type aliases, structs, enumerations, unions, traits, and implementations may be *parameterized* by types, constants, and lifetimes. These @@ -91,12 +88,15 @@ referred to with path syntax. >    _ForLifetimes_? [_Type_] `:` [_TypeParamBounds_]? > > _ForLifetimes_ :\ ->    `for` `<` [_LifetimeParams_](#generic-parameters) `>` +>    `for` `<` [_GenericParams_](#generic-parameters) `>` *Where clauses* provide another way to specify bounds on type and lifetime parameters as well as a way to specify bounds on types that aren't type parameters. +The `for` keyword can be used to introduce [higher-ranked lifetimes]. It only +allows [_LifetimeParam_] parameters. + Bounds that don't use the item's parameters or higher-ranked lifetimes are checked when the item is defined. It is an error for such a bound to be false. @@ -141,6 +141,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> { [IDENTIFIER]: ../identifiers.md [LIFETIME_OR_LABEL]: ../tokens.md#lifetimes-and-loop-labels +[_LifetimeParam_]: #generic-parameters [_LifetimeBounds_]: ../trait-bounds.md [_Lifetime_]: ../trait-bounds.md [_OuterAttribute_]: ../attributes.md @@ -150,6 +151,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> { [arrays]: ../types/array.md [const contexts]: ../const_eval.md#const-context [function pointers]: ../types/function-pointer.md +[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds [raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut [references]: ../types/pointer.md#shared-references- [repeat expressions]: ../expressions/array-expr.md diff --git a/src/paths.md b/src/paths.md index 47a14ee91..29d2475b5 100644 --- a/src/paths.md +++ b/src/paths.md @@ -50,27 +50,10 @@ mod m { > > _GenericArgs_ :\ >       `<` `>`\ ->    | `<` _GenericArgsLifetimes_ `,`? `>`\ ->    | `<` _GenericArgsTypes_ `,`? `>`\ ->    | `<` _GenericArgsConsts_ `,`? `>`\ ->    | `<` _GenericArgsBindings_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsConsts_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsBindings_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsBindings_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`? `>`\ ->    | `<` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`? `>`\ ->    | `<` _GenericArgsLifetimes_ `,` _GenericArgsTypes_ `,` _GenericArgsConsts_ `,` _GenericArgsBindings_ `,`? `>` +>    | `<` ( _GenericArg_ `,` )\* _GenericArg_ `,`? `>` > -> _GenericArgsLifetimes_ :\ ->    [_Lifetime_] (`,` [_Lifetime_])\* -> -> _GenericArgsTypes_ :\ ->    [_Type_] (`,` [_Type_])\* -> -> _GenericArgsConsts_ :\ ->    _GenericArgsConst_ (`,` _GenericArgsConst_)\* +> _GenericArg_ :\ +>    [_Lifetime_] | [_Type_] | _GenericArgsConst_ | _GenericArgsBinding_ > > _GenericArgsConst_ :\ >       [_BlockExpression_]\ @@ -78,9 +61,6 @@ mod m { >    | `-` [_LiteralExpression_]\ >    | [_SimplePathSegment_] > -> _GenericArgsBindings_ :\ ->    _GenericArgsBinding_ (`,` _GenericArgsBinding_)\* -> > _GenericArgsBinding_ :\ >    [IDENTIFIER] `=` [_Type_] @@ -95,6 +75,9 @@ ambiguity with the less-than operator. This is colloquially known as "turbofish" Vec::::with_capacity(1024); ``` +The order of generic arguments is restricted to lifetime arguments, then type +arguments, then const arguments, then equality constraints. + Const arguments must be surrounded by braces unless they are a [literal] or a single segment path.