Skip to content

Commit 1c0bf98

Browse files
committed
Update grammar for parser unification.
1 parent 6dc5c2f commit 1c0bf98

9 files changed

+128
-139
lines changed

src/items/associated-items.md

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Associated Items
22

3+
> **<sup>Syntax</sup>**\
4+
> _AssociatedItem_ :\
5+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
6+
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
7+
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_TypeAlias_] | [_ConstantItem_] | [_Function_] ) )\
8+
> &nbsp;&nbsp; )
9+
310
*Associated Items* are the items declared in [traits] or defined in
411
[implementations]. They are called this because they are defined on an associate
512
type &mdash; the type in the implementation. They are a subset of the kinds of
@@ -79,21 +86,6 @@ let _: f64 = f64::from_i32(42);
7986

8087
### Methods
8188

82-
> _Method_ :\
83-
> &nbsp;&nbsp; [_FunctionQualifiers_] `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
84-
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _SelfParam_ (`,` [_FunctionParam_])<sup>\*</sup> `,`<sup>?</sup> `)`\
85-
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>\
86-
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
87-
>
88-
> _SelfParam_ :\
89-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( _ShorthandSelf_ | _TypedSelf_ )
90-
>
91-
> _ShorthandSelf_ :\
92-
> &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`
93-
>
94-
> _TypedSelf_ :\
95-
> &nbsp;&nbsp; `mut`<sup>?</sup> `self` `:` [_Type_]
96-
9789
Associated functions whose first parameter is named `self` are called *methods*
9890
and may be invoked using the [method call operator], for example, `x.foo()`, as
9991
well as the usual function call notation.
@@ -227,6 +219,8 @@ If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
227219
associated type definition. Furthermore, if `Item` is a type parameter, then
228220
`Item::Assoc` can be used in type parameters.
229221

222+
Associated types must not include [generic parameters] or [where clauses].
223+
230224
```rust
231225
trait AssociatedType {
232226
// Associated type declaration
@@ -340,19 +334,16 @@ fn main() {
340334
}
341335
```
342336

343-
[_BlockExpression_]: ../expressions/block-expr.md
344-
[_FunctionParam_]: functions.md
345-
[_FunctionQualifiers_]: functions.md
346-
[_FunctionReturnType_]: functions.md
347-
[_GenericParams_]: generics.md
348-
[_Lifetime_]: ../trait-bounds.md
349-
[_Type_]: ../types.md#type-expressions
350-
[_WhereClause_]: generics.md#where-clauses
337+
[_ConstantItem_]: constant-items.md
338+
[_Function_]: functions.md
339+
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
340+
[_OuterAttribute_]: ../attributes.md
341+
[_TypeAlias_]: type-aliases.md
342+
[_Visibility_]: ../visibility-and-privacy.md
351343
[`Arc<Self>`]: ../special-types-and-traits.md#arct
352344
[`Box<Self>`]: ../special-types-and-traits.md#boxt
353345
[`Pin<P>`]: ../special-types-and-traits.md#pinp
354346
[`Rc<Self>`]: ../special-types-and-traits.md#rct
355-
[_OuterAttribute_]: ../attributes.md
356347
[traits]: traits.md
357348
[type aliases]: type-aliases.md
358349
[inherent implementations]: implementations.md#inherent-implementations
@@ -367,3 +358,5 @@ fn main() {
367358
[method call operator]: ../expressions/method-call-expr.md
368359
[path]: ../paths.md
369360
[regular function parameters]: functions.md#attributes-on-function-parameters
361+
[generic parameters]: generics.md
362+
[where clauses]: generics.md#where-clauses

src/items/constant-items.md

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

33
> **<sup>Syntax</sup>**\
44
> _ConstantItem_ :\
5-
> &nbsp;&nbsp; `const` ( [IDENTIFIER] | `_` ) `:` [_Type_] `=` [_Expression_] `;`
5+
> &nbsp;&nbsp; `const` ( [IDENTIFIER] | `_` ) `:` [_Type_] ( `=` [_Expression_] )<sup>?</sup> `;`
66
77
A *constant item* is an optionally named _[constant value]_ which is not associated
88
with a specific memory location in the program. Constants are essentially inlined
@@ -38,6 +38,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
3838
};
3939
```
4040

41+
The constant expression may only be omitted in a [trait definition].
42+
4143
## Constants with Destructors
4244

4345
Constants can contain destructors. Destructors are run when the value goes out
@@ -91,6 +93,7 @@ m!(const _: () = (););
9193
[constant value]: ../const_eval.md#constant-expressions
9294
[free]: ../glossary.md#free-item
9395
[static lifetime elision]: ../lifetime-elision.md#static-lifetime-elision
96+
[trait definition]: traits.md
9497
[IDENTIFIER]: ../identifiers.md
9598
[underscore imports]: use-declarations.md#underscore-imports
9699
[_Type_]: ../types.md#type-expressions

src/items/external-blocks.md

+12-27
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,8 @@
1010
> _ExternalItem_ :\
1111
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
1212
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
13-
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( _ExternalStaticItem_ | _ExternalFunctionItem_ ) )\
13+
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_StaticItem_] | [_Function_] ) )\
1414
> &nbsp;&nbsp; )
15-
>
16-
> _ExternalStaticItem_ :\
17-
> &nbsp;&nbsp; `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_] `;`
18-
>
19-
> _ExternalFunctionItem_ :\
20-
> &nbsp;&nbsp; `fn` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>\
21-
> &nbsp;&nbsp; `(` ( _NamedFunctionParameters_ | _NamedFunctionParametersWithVariadics_ )<sup>?</sup> `)`\
22-
> &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> `;`
23-
>
24-
> _NamedFunctionParameters_ :\
25-
> &nbsp;&nbsp; _NamedFunctionParam_ ( `,` _NamedFunctionParam_ )<sup>\*</sup> `,`<sup>?</sup>
26-
>
27-
> _NamedFunctionParam_ :\
28-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( [IDENTIFIER] | `_` ) `:` [_Type_]
29-
>
30-
> _NamedFunctionParametersWithVariadics_ :\
31-
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` [_OuterAttribute_]<sup>\*</sup> `...`
3215
3316
External blocks provide _declarations_ of items that are not _defined_ in the
3417
current crate and are the basis of Rust's foreign function interface. These are
@@ -46,9 +29,10 @@ token stream.
4629
## Functions
4730

4831
Functions within external blocks are declared in the same way as other Rust
49-
functions, with the exception that they may not have a body and are instead
32+
functions, with the exception that they must not have a body and are instead
5033
terminated by a semicolon. Patterns are not allowed in parameters, only
51-
[IDENTIFIER] or `_` may be used.
34+
[IDENTIFIER] or `_` may be used. Function qualifiers (`const`, `async`,
35+
`unsafe`, and `extern`) are not allowed.
5236

5337
Functions within external blocks may be called by Rust code, just like
5438
functions defined in Rust. The Rust compiler automatically translates between
@@ -109,12 +93,15 @@ There are also some platform-specific ABI strings:
10993

11094
## Variadic functions
11195

112-
Functions within external blocks may be variadic by specifying `...` after one
113-
or more named arguments in the argument list:
96+
Functions within external blocks may be variadic by specifying `...` as the
97+
last argument. There must be at least one parameter before the variadic
98+
parameter. The variadic parameter may optionally be specified with an
99+
identifier.
114100

115101
```rust
116-
extern {
102+
extern "C" {
117103
fn foo(x: i32, ...);
104+
fn with_name(format: *const u8, args: ...);
118105
}
119106
```
120107

@@ -189,15 +176,13 @@ restrictions as [regular function parameters].
189176
[functions]: functions.md
190177
[statics]: static-items.md
191178
[_Abi_]: functions.md
192-
[_FunctionReturnType_]: functions.md
193-
[_GenericParams_]: generics.md
179+
[_Function_]: functions.md
194180
[_InnerAttribute_]: ../attributes.md
195181
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
196182
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
197183
[_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
198184
[_OuterAttribute_]: ../attributes.md
199-
[_Type_]: ../types.md#type-expressions
185+
[_StaticItem_]: static-items.md
200186
[_Visibility_]: ../visibility-and-privacy.md
201-
[_WhereClause_]: generics.md#where-clauses
202187
[attributes]: ../attributes.md
203188
[regular function parameters]: functions.md#attributes-on-function-parameters

src/items/functions.md

+49-8
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,42 @@
55
> &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>\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
8+
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_BlockExpression_] | `;` )
99
>
1010
> _FunctionQualifiers_ :\
11-
> &nbsp;&nbsp; _AsyncConstQualifiers_<sup>?</sup> `unsafe`<sup>?</sup> (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
12-
>
13-
> _AsyncConstQualifiers_ :\
14-
> &nbsp;&nbsp; `async` | `const`
11+
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> `unsafe`<sup>?</sup> (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
1512
>
1613
> _Abi_ :\
1714
> &nbsp;&nbsp; [STRING_LITERAL] | [RAW_STRING_LITERAL]
1815
>
1916
> _FunctionParameters_ :\
20-
> &nbsp;&nbsp; _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
17+
> &nbsp;&nbsp; &nbsp;&nbsp; _SelfParam_ `,`<sup>?</sup>\
18+
> &nbsp;&nbsp; | (_SelfParam_ `,`)<sup>?</sup> _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
19+
>
20+
> _SelfParam_ :\
21+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> ( _ShorthandSelf_ | _TypedSelf_ )
22+
>
23+
> _ShorthandSelf_ :\
24+
> &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`
25+
>
26+
> _TypedSelf_ :\
27+
> &nbsp;&nbsp; `mut`<sup>?</sup> `self` `:` [_Type_]
2128
>
2229
> _FunctionParam_ :\
23-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Pattern_] `:` [_Type_]
30+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (
31+
> _FunctionParamPattern_ | `...` | [_Type_] [^fn-param-2015]
32+
> )
33+
>
34+
> _FunctionParamPattern_ :\
35+
> &nbsp;&nbsp; [_Pattern_] `:` ( [_Type_] | `...` )
2436
>
2537
> _FunctionReturnType_ :\
2638
> &nbsp;&nbsp; `->` [_Type_]
39+
>
40+
> [^async-edition]: The `async` qualifier is not allowed in the 2015 edition.
41+
>
42+
> [^fn-param-2015]: Function parameters with only a type are only allowed
43+
> in an associated function of a [trait item] in the 2015 edition.
2744
2845
A _function_ consists of a [block], along with a name and a set of parameters.
2946
Other than a name, all these are optional. Functions are declared with the
@@ -43,13 +60,25 @@ fn answer_to_life_the_universe_and_everything() -> i32 {
4360
}
4461
```
4562

46-
As with `let` bindings, function arguments are irrefutable [patterns], so any
63+
## Function parameters
64+
65+
As with `let` bindings, function parameters are irrefutable [patterns], so any
4766
pattern that is valid in a let binding is also valid as an argument:
4867

4968
```rust
5069
fn first((value, _): (i32, i32)) -> i32 { value }
5170
```
5271

72+
If the first parameter is a _SelfParam_, this indicates that the function is a
73+
[method]. Functions with a self parameter may only appear as an [associated
74+
function] in a [trait] or [implementation].
75+
76+
A parameter with the `...` token indicates a [variadic function], and may only
77+
be used as the last parameter of a [external block] function. The variadic
78+
parameter may have an optional identifier, such as `args: ...`.
79+
80+
## Function body
81+
5382
The block of a function is conceptually wrapped in a block that binds the
5483
argument patterns and then `return`s the value of the function's block. This
5584
means that the tail expression of the block, if evaluated, ends up being
@@ -67,6 +96,9 @@ return {
6796
};
6897
```
6998

99+
Functions without a body block are terminated with a semicolon. This form
100+
may only appear in a [trait] or [external block].
101+
70102
## Generic functions
71103

72104
A _generic function_ allows one or more _parameterized types_ to appear in its
@@ -187,6 +219,9 @@ Functions qualified with the `const` keyword are [const functions], as are
187219
[tuple struct] and [tuple variant] constructors. _Const functions_ can be
188220
called from within [const context]s.
189221

222+
Const functions are not allowed to be [async](#async-functions), and cannot
223+
use the [`extern` function qualifier](#extern-function-qualifier).
224+
190225
## Async functions
191226

192227
Functions may be qualified as async, and this can also be combined with the
@@ -342,6 +377,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
342377
[STRING_LITERAL]: ../tokens.md#string-literals
343378
[_BlockExpression_]: ../expressions/block-expr.md
344379
[_GenericParams_]: generics.md
380+
[_Lifetime_]: ../trait-bounds.md
345381
[_Pattern_]: ../patterns.md
346382
[_Type_]: ../types.md#type-expressions
347383
[_WhereClause_]: generics.md#where-clauses
@@ -372,3 +408,8 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
372408
[`link_section`]: ../abi.md#the-link_section-attribute
373409
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
374410
[built-in attributes]: ../attributes.html#built-in-attributes-index
411+
[trait item]: traits.md
412+
[method]: associated-items.md#methods
413+
[associated function]: associated-items.md#associated-functions-and-methods
414+
[implementation]: implementations.md
415+
[variadic function]: external-blocks.md#variadic-functions

src/items/implementations.md

+5-22
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,17 @@
77
> _InherentImpl_ :\
88
> &nbsp;&nbsp; `impl` [_GenericParams_]<sup>?</sup>&nbsp;[_Type_]&nbsp;[_WhereClause_]<sup>?</sup> `{`\
99
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
10-
> &nbsp;&nbsp; &nbsp;&nbsp; _InherentImplItem_<sup>\*</sup>\
10+
> &nbsp;&nbsp; &nbsp;&nbsp; [_AssociatedItem_]<sup>\*</sup>\
1111
> &nbsp;&nbsp; `}`
1212
>
13-
> _InherentImplItem_ :\
14-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
15-
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
16-
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_ConstantItem_] | [_Function_] | [_Method_] ) )\
17-
> &nbsp;&nbsp; )
18-
>
1913
> _TraitImpl_ :\
2014
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `impl` [_GenericParams_]<sup>?</sup> `!`<sup>?</sup>
2115
> [_TypePath_] `for` [_Type_]\
2216
> &nbsp;&nbsp; [_WhereClause_]<sup>?</sup>\
2317
> &nbsp;&nbsp; `{`\
2418
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
25-
> &nbsp;&nbsp; &nbsp;&nbsp; _TraitImplItem_<sup>\*</sup>\
19+
> &nbsp;&nbsp; &nbsp;&nbsp; [_AssociatedItem_]<sup>\*</sup>\
2620
> &nbsp;&nbsp; `}`
27-
>
28-
> _TraitImplItem_ :\
29-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
30-
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
31-
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_TypeAlias_] | [_ConstantItem_] | [_Function_] | [_Method_] ) )\
32-
> &nbsp;&nbsp; )
3321
3422
An _implementation_ is an item that associates items with an _implementing type_.
3523
Implementations are defined with the keyword `impl` and contain functions
@@ -52,7 +40,7 @@ the _associated items_ to the implementing type.
5240

5341
Inherent implementations associate the contained items to the
5442
implementing type. Inherent implementations can contain [associated
55-
functions] (including methods) and [associated constants]. They cannot
43+
functions] (including [methods]) and [associated constants]. They cannot
5644
contain associated type aliases.
5745

5846
The [path] to an associated item is any path to the implementing type,
@@ -204,17 +192,11 @@ attributes must come before any associated items. That attributes that have
204192
meaning here are [`cfg`], [`deprecated`], [`doc`], and [the lint check
205193
attributes].
206194

207-
[_ConstantItem_]: constant-items.md
208-
[_Function_]: functions.md
195+
[_AssociatedItem_]: associated-items.md
209196
[_GenericParams_]: generics.md
210197
[_InnerAttribute_]: ../attributes.md
211-
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
212-
[_Method_]: associated-items.md#methods
213-
[_OuterAttribute_]: ../attributes.md
214-
[_TypeAlias_]: type-aliases.md
215198
[_TypePath_]: ../paths.md#paths-in-types
216199
[_Type_]: ../types.md#type-expressions
217-
[_Visibility_]: ../visibility-and-privacy.md
218200
[_WhereClause_]: generics.md#where-clauses
219201
[trait]: traits.md
220202
[associated functions]: associated-items.md#associated-functions-and-methods
@@ -230,3 +212,4 @@ attributes].
230212
[local type]: ../glossary.md#local-type
231213
[fundamental types]: ../glossary.md#fundamental-type-constructors
232214
[uncovered type]: ../glossary.md#uncovered-type
215+
[methods]: associated-items.md#methods

src/items/static-items.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
> **<sup>Syntax</sup>**\
44
> _StaticItem_ :\
55
> &nbsp;&nbsp; `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_]
6-
> `=` [_Expression_] `;`
6+
> ( `=` [_Expression_] )<sup>?</sup> `;`
77
88
A *static item* is similar to a [constant], except that it represents a precise
99
memory location in the program. All references to the static refer to the same
@@ -23,6 +23,9 @@ statics:
2323
* The type must have the `Sync` trait bound to allow thread-safe access.
2424
* Constants cannot refer to statics.
2525

26+
The initializer expression must be omitted in an [external block], and must be
27+
provided for free static items.
28+
2629
## Mutable statics
2730

2831
If a static item is declared with the `mut` keyword, then it is allowed to be
@@ -73,6 +76,7 @@ following are true:
7376
[constant]: constant-items.md
7477
[`drop`]: ../destructors.md
7578
[constant expression]: ../const_eval.md#constant-expressions
79+
[external block]: external-blocks.md
7680
[interior mutable]: ../interior-mutability.md
7781
[IDENTIFIER]: ../identifiers.md
7882
[_Type_]: ../types.md#type-expressions

0 commit comments

Comments
 (0)