From 99b544c3a73b9ea9df53702c06dcd89266281a3c Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sun, 28 Jun 2026 10:43:11 +0200 Subject: [PATCH 1/2] Add `named_fn_trait_parameters` feature gate --- compiler/rustc_feature/src/unstable.rs | 2 ++ compiler/rustc_span/src/symbol.rs | 1 + .../feature-gate-named-fn-trait-parameters.rs | 7 +++++++ .../feature-gate-named-fn-trait-parameters.stderr | 8 ++++++++ 4 files changed, 18 insertions(+) create mode 100644 tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs create mode 100644 tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index fc38d9de6e143..90236c83aad83 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -662,6 +662,8 @@ declare_features! ( (unstable, naked_functions_rustic_abi, "1.88.0", Some(138997)), /// Allows using `#[target_feature(enable = "...")]` on `#[naked]` on functions. (unstable, naked_functions_target_feature, "1.86.0", Some(138568)), + /// Allows providing names to parameters of `impl Fn` etc + (unstable, named_fn_trait_parameters, "CURRENT_RUSTC_VERSION", Some(158499)), /// Allows specifying the as-needed link modifier (unstable, native_link_modifiers_as_needed, "1.53.0", Some(81490)), /// Allow negative trait implementations. diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c75da250efd5f..8e87c73502c44 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1378,6 +1378,7 @@ symbols! { naked_functions_rustic_abi, naked_functions_target_feature, name, + named_fn_trait_parameters, names, native_link_modifiers, native_link_modifiers_as_needed, diff --git a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs new file mode 100644 index 0000000000000..c679428db3dd2 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs @@ -0,0 +1,7 @@ +fn parse_my_data( + data: &str, + log: impl Fn(msg: String), + //~^ ERROR `Trait(...)` syntax does not support named parameters +) { } + +fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr new file mode 100644 index 0000000000000..8dcd3cc521b6e --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr @@ -0,0 +1,8 @@ +error: `Trait(...)` syntax does not support named parameters + --> $DIR/feature-gate-named-fn-trait-parameters.rs:3:18 + | +LL | log: impl Fn(msg: String), + | ^^^ help: remove the parameter name + +error: aborting due to 1 previous error + From 4d2b63862f5f41bcdbbd8f4c64591ec571417953 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sun, 28 Jun 2026 11:00:12 +0200 Subject: [PATCH 2/2] Implement named fn trait parameters --- compiler/rustc_ast_passes/src/feature_gate.rs | 4 ++ compiler/rustc_parse/src/errors.rs | 8 --- compiler/rustc_parse/src/parser/path.rs | 12 ++-- compiler/rustc_session/src/parse.rs | 2 +- .../feature-gate-named-fn-trait-parameters.rs | 2 +- ...ture-gate-named-fn-trait-parameters.stderr | 9 ++- .../fn-trait-use-named-params-issue-140169.rs | 8 +-- ...trait-use-named-params-issue-140169.stderr | 55 ++++++++++++------- ...hesized-type-arguments-ice-issue-122345.rs | 2 +- ...zed-type-arguments-ice-issue-122345.stderr | 16 ++++-- 10 files changed, 70 insertions(+), 48 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index d2e3c00bb0c07..526400c0ea4d2 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -525,6 +525,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { half_open_range_patterns_in_slices, "half-open range patterns in slices are unstable" ); + gate_all!( + named_fn_trait_parameters, + "named parameters in parenthesized generic argument lists are experimental" + ); // `associated_const_equality` will be stabilized as part of `min_generic_const_args`. for &span in spans.get(&sym::associated_const_equality).into_iter().flatten() { diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 0c850b69ca7f1..b9425ca01fdc4 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -2075,14 +2075,6 @@ pub(crate) struct ExpectedFnPathFoundFnKeyword { pub fn_token_span: Span, } -#[derive(Diagnostic)] -#[diag("`Trait(...)` syntax does not support named parameters")] -pub(crate) struct FnPathFoundNamedParams { - #[primary_span] - #[suggestion("remove the parameter name", applicability = "machine-applicable", code = "")] - pub named_param_span: Span, -} - #[derive(Diagnostic)] #[diag("`Trait(...)` syntax does not support c_variadic parameters")] pub(crate) struct PathFoundCVariadicParams { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index ea86c08915b31..8e86deebc8071 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -17,8 +17,8 @@ use super::{Parser, Restrictions, TokenType}; use crate::ast::{PatKind, TyKind}; use crate::errors::{ self, AttributeOnEmptyType, AttributeOnGenericArg, ConstGenericWithoutBraces, - ConstGenericWithoutBracesSugg, FnPathFoundNamedParams, PathFoundAttributeInParams, - PathFoundCVariadicParams, PathSingleColon, PathTripleColon, + ConstGenericWithoutBracesSugg, PathFoundAttributeInParams, PathFoundCVariadicParams, + PathSingleColon, PathTripleColon, }; use crate::exp; use crate::parser::{ @@ -409,11 +409,11 @@ impl<'a> Parser<'a> { req_body: false, }; let param = p.parse_param_general(&mode, false, false); - param.map(move |param| { + param.map(|param| { if !matches!(param.pat.kind, PatKind::Missing) { - dcx.emit_err(FnPathFoundNamedParams { - named_param_span: param.pat.span, - }); + self.psess + .gated_spans + .gate(sym::named_fn_trait_parameters, param.pat.span); } if matches!(param.ty.kind, TyKind::CVarArgs) { dcx.emit_err(PathFoundCVariadicParams { span: param.pat.span }); diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 54058e9f5f046..0e79d220b3962 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -22,7 +22,7 @@ use crate::Session; use crate::lint::{Lint, LintId}; /// Collected spans during parsing for places where a certain feature was -/// used and should be feature gated accordingly in `check_crate`. +/// used and should be feature gated accordingly in `check_crate` in `rustc_ast_passes`. #[derive(Default)] pub struct GatedSpans { pub spans: Lock>>, diff --git a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs index c679428db3dd2..23307e798950f 100644 --- a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs +++ b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.rs @@ -1,7 +1,7 @@ fn parse_my_data( data: &str, log: impl Fn(msg: String), - //~^ ERROR `Trait(...)` syntax does not support named parameters + //~^ ERROR named parameters in parenthesized generic argument lists are experimental [E0658] ) { } fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr index 8dcd3cc521b6e..d50966c9352b0 100644 --- a/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr +++ b/tests/ui/feature-gates/feature-gate-named-fn-trait-parameters.stderr @@ -1,8 +1,13 @@ -error: `Trait(...)` syntax does not support named parameters +error[E0658]: named parameters in parenthesized generic argument lists are experimental --> $DIR/feature-gate-named-fn-trait-parameters.rs:3:18 | LL | log: impl Fn(msg: String), - | ^^^ help: remove the parameter name + | ^^^ + | + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error: aborting due to 1 previous error +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/fn/fn-trait-use-named-params-issue-140169.rs b/tests/ui/fn/fn-trait-use-named-params-issue-140169.rs index 218450abd4998..036627ae3d913 100644 --- a/tests/ui/fn/fn-trait-use-named-params-issue-140169.rs +++ b/tests/ui/fn/fn-trait-use-named-params-issue-140169.rs @@ -1,9 +1,9 @@ fn f1(_: fn(a: u8)) {} -fn f2(_: impl Fn(u8, vvvv: u8)) {} //~ ERROR `Trait(...)` syntax does not support named parameters -fn f3(_: impl Fn(aaaa: u8, u8)) {} //~ ERROR `Trait(...)` syntax does not support named parameters +fn f2(_: impl Fn(u8, vvvv: u8)) {} //~ ERROR named parameters in parenthesized generic argument lists are experimental +fn f3(_: impl Fn(aaaa: u8, u8)) {} //~ ERROR named parameters in parenthesized generic argument lists are experimental fn f4(_: impl Fn(aaaa: u8, vvvv: u8)) {} -//~^ ERROR `Trait(...)` syntax does not support named parameters -//~| ERROR `Trait(...)` syntax does not support named parameters +//~^ ERROR named parameters in parenthesized generic argument lists are experimental +//~| ERROR named parameters in parenthesized generic argument lists are experimental fn f5(_: impl Fn(u8, ...)) {} //~^ ERROR `Trait(...)` syntax does not support c_variadic parameters fn f6(_: impl Fn(u8, #[allow(unused_attributes)] u8)) {} diff --git a/tests/ui/fn/fn-trait-use-named-params-issue-140169.stderr b/tests/ui/fn/fn-trait-use-named-params-issue-140169.stderr index b72d5b7b3bc48..ac7758e9d5373 100644 --- a/tests/ui/fn/fn-trait-use-named-params-issue-140169.stderr +++ b/tests/ui/fn/fn-trait-use-named-params-issue-140169.stderr @@ -1,38 +1,55 @@ -error: `Trait(...)` syntax does not support named parameters +error: `Trait(...)` syntax does not support c_variadic parameters + --> $DIR/fn-trait-use-named-params-issue-140169.rs:7:22 + | +LL | fn f5(_: impl Fn(u8, ...)) {} + | ^^^ help: remove the `...` + +error: `Trait(...)` syntax does not support attributes in parameters + --> $DIR/fn-trait-use-named-params-issue-140169.rs:9:22 + | +LL | fn f6(_: impl Fn(u8, #[allow(unused_attributes)] u8)) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the attributes + +error[E0658]: named parameters in parenthesized generic argument lists are experimental --> $DIR/fn-trait-use-named-params-issue-140169.rs:2:22 | LL | fn f2(_: impl Fn(u8, vvvv: u8)) {} - | ^^^^ help: remove the parameter name + | ^^^^ + | + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: `Trait(...)` syntax does not support named parameters +error[E0658]: named parameters in parenthesized generic argument lists are experimental --> $DIR/fn-trait-use-named-params-issue-140169.rs:3:18 | LL | fn f3(_: impl Fn(aaaa: u8, u8)) {} - | ^^^^ help: remove the parameter name + | ^^^^ + | + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: `Trait(...)` syntax does not support named parameters +error[E0658]: named parameters in parenthesized generic argument lists are experimental --> $DIR/fn-trait-use-named-params-issue-140169.rs:4:18 | LL | fn f4(_: impl Fn(aaaa: u8, vvvv: u8)) {} - | ^^^^ help: remove the parameter name + | ^^^^ + | + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: `Trait(...)` syntax does not support named parameters +error[E0658]: named parameters in parenthesized generic argument lists are experimental --> $DIR/fn-trait-use-named-params-issue-140169.rs:4:28 | LL | fn f4(_: impl Fn(aaaa: u8, vvvv: u8)) {} - | ^^^^ help: remove the parameter name - -error: `Trait(...)` syntax does not support c_variadic parameters - --> $DIR/fn-trait-use-named-params-issue-140169.rs:7:22 + | ^^^^ | -LL | fn f5(_: impl Fn(u8, ...)) {} - | ^^^ help: remove the `...` - -error: `Trait(...)` syntax does not support attributes in parameters - --> $DIR/fn-trait-use-named-params-issue-140169.rs:9:22 - | -LL | fn f6(_: impl Fn(u8, #[allow(unused_attributes)] u8)) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the attributes + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error: aborting due to 6 previous errors +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs index db25ce440893d..60d87bbe0bfdf 100644 --- a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs +++ b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs @@ -2,7 +2,7 @@ fn main() { unsafe { - dealloc(ptr2, Layout::(x: !)(1, 1)); //~ ERROR `Trait(...)` syntax does not support named parameters + dealloc(ptr2, Layout::(x: !)(1, 1)); //~ ERROR named parameters in parenthesized generic argument lists are experimental //~^ ERROR cannot find function `dealloc` in this scope [E0425] //~| ERROR cannot find value `ptr2` in this scope [E0425] //~| ERROR the `!` type is experimental [E0658] diff --git a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr index a083883af219f..1b76e0cd8abed 100644 --- a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr +++ b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr @@ -1,9 +1,3 @@ -error: `Trait(...)` syntax does not support named parameters - --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:5:32 - | -LL | dealloc(ptr2, Layout::(x: !)(1, 1)); - | ^ help: remove the parameter name - error[E0425]: cannot find function `dealloc` in this scope --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:5:9 | @@ -21,6 +15,16 @@ error[E0425]: cannot find value `ptr2` in this scope LL | dealloc(ptr2, Layout::(x: !)(1, 1)); | ^^^^ not found in this scope +error[E0658]: named parameters in parenthesized generic argument lists are experimental + --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:5:32 + | +LL | dealloc(ptr2, Layout::(x: !)(1, 1)); + | ^ + | + = note: see issue #158499 for more information + = help: add `#![feature(named_fn_trait_parameters)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + error[E0658]: the `!` type is experimental --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:5:35 |