Skip to content

Commit b18ade5

Browse files
committed
Update extern block grammar to be before validation.
The grammar in the reference defines the grammar before ast validation, since that is the grammar accepted by macros. Any restrictions imposed by validation are described in prose (using the term "semantically", but I don't feel like that is the greatest term to use).
1 parent 5e02c27 commit b18ade5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/items/external-blocks.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
> _ExternalItem_ :\
1111
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
1212
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
13-
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( (`safe` | `unsafe`)<sup>?</sup>[^static-qualifiers] [_StaticItem_] | [_Function_] ) )\
13+
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_StaticItem_] | [_Function_] ) )\
1414
> &nbsp;&nbsp; )
1515
>
1616
> [^unsafe-2024]: Starting with the 2024 Edition, the `unsafe` keyword is required semantically.
17-
>
18-
> [^static-qualifiers]: *Relevant to editions earlier than Rust 2024*: The `safe` or `unsafe` qualifier is only allowed when the `extern` is qualified as `unsafe`.
1917
2018
External blocks provide _declarations_ of items that are not _defined_ in the
2119
current crate and are the basis of Rust's foreign function interface. These are

src/items/functions.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_BlockExpression_] | `;` )
99
>
1010
> _FunctionQualifiers_ :\
11-
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> (`safe`[^extern-safe] | `unsafe`)<sup>?</sup>[^extern-qualifiers] (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
11+
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> _ItemSafety_<sup>?</sup>[^extern-qualifiers] (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
12+
>
13+
> _ItemSafety_ :\
14+
> &nbsp;&nbsp; `safe`[^extern-safe] | `unsafe`
1215
>
1316
> _Abi_ :\
1417
> &nbsp;&nbsp; [STRING_LITERAL] | [RAW_STRING_LITERAL]
@@ -39,7 +42,7 @@
3942
>
4043
> [^async-edition]: The `async` qualifier is not allowed in the 2015 edition.
4144
>
42-
> [^extern-safe]: The `safe` function qualifier is only allowed within
45+
> [^extern-safe]: The `safe` function qualifier is only allowed semantically within
4346
> `extern` blocks.
4447
>
4548
> [^extern-qualifiers]: *Relevant to editions earlier than Rust 2024*: Within
@@ -65,6 +68,8 @@ fn answer_to_life_the_universe_and_everything() -> i32 {
6568
}
6669
```
6770

71+
The `safe` function is semantically only allowed when used in an [`extern` block].
72+
6873
## Function parameters
6974

7075
Function parameters are irrefutable [patterns], so any pattern that is valid in
@@ -423,3 +428,4 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
423428
[associated function]: associated-items.md#associated-functions-and-methods
424429
[implementation]: implementations.md
425430
[variadic function]: external-blocks.md#variadic-functions
431+
[`extern` block]: external-blocks.md

src/items/static-items.md

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

33
> **<sup>Syntax</sup>**\
44
> _StaticItem_ :\
5-
> &nbsp;&nbsp; `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_]
5+
> &nbsp;&nbsp; [_ItemSafety_]<sup>?</sup>[^extern-safety] `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_]
66
> ( `=` [_Expression_] )<sup>?</sup> `;`
7+
>
8+
> [^extern-safety]: The `safe` and `unsafe` function qualifiers are only
9+
> allowed semantically within `extern` blocks.
710
811
A *static item* is similar to a [constant], except that it represents a precise
912
memory location in the program. All references to the static refer to the same
@@ -26,6 +29,8 @@ statics:
2629
The initializer expression must be omitted in an [external block], and must be
2730
provided for free static items.
2831

32+
The `safe` and `unsafe` qualifiers are semantically only allowed when used in an [external block].
33+
2934
## Statics & generics
3035

3136
A static item defined in a generic scope (for example in a blanket or default
@@ -129,3 +134,4 @@ following are true:
129134
[IDENTIFIER]: ../identifiers.md
130135
[_Type_]: ../types.md#type-expressions
131136
[_Expression_]: ../expressions.md
137+
[_ItemSafety_]: functions.md

0 commit comments

Comments
 (0)