From 966c87c9a3ac4cae40cddb301f7ba60e0c611c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 24 Apr 2026 19:12:25 +0200 Subject: [PATCH 1/4] Fix grammar and notes pertaining to the `safe` and `unsafe` modifiers * `safe` is a syntactically valid function pointer type qualifier next to `unsafe` but it wasn't listed prior. * renamed `ItemSafety` to `Safety` since it's now also used in the grammar of function pointer types * footnote on non-terminal `Safety` in rule `StaticItem` called `safe` & `unsafe` *function* modifiers when they're evidentally more general and can be placed on *static* items * made the footnotes related to `Safety` more precise --- src/items/functions.md | 10 +++++----- src/items/static-items.md | 4 ++-- src/types/function-pointer.md | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/items/functions.md b/src/items/functions.md index 1586adc33c..cdfda69d9c 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -9,9 +9,9 @@ Function -> FunctionReturnType? WhereClause? ( BlockExpression | `;` ) -FunctionQualifiers -> `const`? `async`?[^async-edition] ItemSafety?[^extern-qualifiers] (`extern` Abi?)? +FunctionQualifiers -> `const`? `async`?[^async-edition] Safety?[^extern-qualifiers] (`extern` Abi?)? -ItemSafety -> `safe`[^extern-safe] | `unsafe` +Safety -> `safe`[^safe-semantics] | `unsafe` Abi -> STRING_LITERAL | RAW_STRING_LITERAL @@ -34,12 +34,12 @@ FunctionReturnType -> `->` Type [^async-edition]: The `async` qualifier is not allowed in the 2015 edition. -[^extern-safe]: The `safe` function qualifier is only allowed semantically within `extern` blocks. - -[^extern-qualifiers]: *Relevant to editions earlier than Rust 2024*: Within `extern` blocks, the `safe` or `unsafe` function qualifier is only allowed when the `extern` is qualified as `unsafe`. +[^extern-qualifiers]: The `safe` or `unsafe` qualifiers are only allowed semantically within `extern` blocks. *Relevant to editions earlier than Rust 2024*: More specifically within `extern` blocks that are qualified as `unsafe`. [^fn-param-2015]: Function parameters with only a type are only allowed in an associated function of a [trait item] in the 2015 edition. +[^safe-semantics]: The `safe` qualifier is only allowed semantically on functions and function pointer types within `extern` blocks. + r[items.fn.intro] A _function_ consists of a [block] (that's the _body_ of the function), along with a name, a set of parameters, and an output type. Other than a name, all these are optional. diff --git a/src/items/static-items.md b/src/items/static-items.md index 395d69c3ce..54d03b1a99 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -4,10 +4,10 @@ r[items.static] r[items.static.syntax] ```grammar,items StaticItem -> - ItemSafety?[^extern-safety] `static` `mut`? IDENTIFIER `:` Type ( `=` Expression )? `;` + Safety?[^extern-qualifiers] `static` `mut`? IDENTIFIER `:` Type ( `=` Expression )? `;` ``` -[^extern-safety]: The `safe` and `unsafe` function qualifiers are only allowed semantically within `extern` blocks. +[^extern-qualifiers]: The `safe` and `unsafe` qualifiers are only allowed semantically within `extern` blocks. *Relevant to editions earlier than Rust 2024*: More specifically within `extern` blocks that are qualified as `unsafe`. r[items.static.intro] A *static item* is similar to a [constant], except that it represents an allocation in the program that is initialized with the initializer expression. All references and raw pointers to the static refer to the same allocation. diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 08dfbbf235..77ca58b44a 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -7,7 +7,7 @@ BareFunctionType -> ForLifetimes? FunctionTypeQualifiers `fn` `(` FunctionParametersMaybeNamedVariadic? `)` BareFunctionReturnType? -FunctionTypeQualifiers -> `unsafe`? (`extern` Abi?)? +FunctionTypeQualifiers -> Safety? (`extern` Abi?)? BareFunctionReturnType -> `->` TypeNoBounds From abba35567153712f5cd1f7f173ec65216157ce24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 24 Apr 2026 19:53:32 +0200 Subject: [PATCH 2/4] Fix grammar pertaining to C variadics in function pointer types The grammar was incorrect since * C variadics `...` no longer need to be preceded by a normal parameter * that's the case since * C variadics don't need to be the final element, `fn(..., i32)` is perfectly fine * C variadics can indeed be named (e.g., `fn f(_: ...)` or `fn(v: ...)`) * C variadics can be followed by a comma (e.g., `fn(...,)`) --- src/types/function-pointer.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 77ca58b44a..544861b4ad 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -5,23 +5,17 @@ r[type.fn-pointer.syntax] ```grammar,types BareFunctionType -> ForLifetimes? FunctionTypeQualifiers `fn` - `(` FunctionParametersMaybeNamedVariadic? `)` BareFunctionReturnType? + `(` MaybeNamedFunctionParameters? `)` BareFunctionReturnType? FunctionTypeQualifiers -> Safety? (`extern` Abi?)? BareFunctionReturnType -> `->` TypeNoBounds -FunctionParametersMaybeNamedVariadic -> - MaybeNamedFunctionParameters | MaybeNamedFunctionParametersVariadic - MaybeNamedFunctionParameters -> MaybeNamedParam ( `,` MaybeNamedParam )* `,`? MaybeNamedParam -> - OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? Type - -MaybeNamedFunctionParametersVariadic -> - ( MaybeNamedParam `,` )* MaybeNamedParam `,` OuterAttribute* `...` + OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? ( Type | `...` ) ``` r[type.fn-pointer.intro] From 931911837a61011cc8f2ce49740a4df33fa81410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 24 Apr 2026 20:03:42 +0200 Subject: [PATCH 3/4] Fix grammar to accept self params in function pointer types `fn(&self)`, `fn(&'static mut self)` etc. are syntactically valid function pointer types --- src/types/function-pointer.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 544861b4ad..92a95cc652 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -12,7 +12,8 @@ FunctionTypeQualifiers -> Safety? (`extern` Abi?)? BareFunctionReturnType -> `->` TypeNoBounds MaybeNamedFunctionParameters -> - MaybeNamedParam ( `,` MaybeNamedParam )* `,`? + SelfParam `,`? + | (SelfParam `,`)? MaybeNamedParam ( `,` MaybeNamedParam )* `,`? MaybeNamedParam -> OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? ( Type | `...` ) From 400cb4b933a7c748f0f776441a6c55bcd23f2d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 24 Apr 2026 20:10:12 +0200 Subject: [PATCH 4/4] Fix the grammar of parameter patterns in function pointer types It's not just (common) identifiers or underscores, it's more complex. --- src/types/function-pointer.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 92a95cc652..143912b0c7 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -16,7 +16,10 @@ MaybeNamedFunctionParameters -> | (SelfParam `,`)? MaybeNamedParam ( `,` MaybeNamedParam )* `,`? MaybeNamedParam -> - OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? ( Type | `...` ) + OuterAttribute* ( MaybeNamedPattern `:` )? ( Type | `...` ) + +MaybeNamedPattern -> + `mut`? IDENTIFIER | ( `&` | `&&` )? ( `_` | `false` | `true` ) ``` r[type.fn-pointer.intro]