From 1733ac8f0175495a6af4479060ecf6f9326ffb23 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Sat, 15 Aug 2026 12:54:12 +0330 Subject: [PATCH 1/2] fix: avoid ICE when recovering parenthesized type parameters Signed-off-by: Amirhossein Akhlaghpour --- compiler/rustc_ast_lowering/src/lib.rs | 4 ++++ compiler/rustc_ast_lowering/src/path.rs | 6 ++++-- .../typeck/parenthesized-type-params-in-paths.rs | 5 +++++ .../parenthesized-type-params-in-paths.stderr | 14 +++++++++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index cc7e081f2cce3..f957afc147d30 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -411,6 +411,9 @@ enum ImplTraitContext { FeatureGated(ImplTraitPosition, Symbol), /// `impl Trait` is not accepted in this position. Disallowed(ImplTraitPosition), + + /// An error has already been emitted for this type. + AlreadyErrored(ErrorGuaranteed), } /// Position in which `impl Trait` is disallowed. @@ -1735,6 +1738,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }); hir::TyKind::Err(guar) } + ImplTraitContext::AlreadyErrored(guar) => hir::TyKind::Err(guar), } } TyKind::Pat(ty, pat) => { diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index ae260c7d126ab..3937dc95893d6 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -338,12 +338,14 @@ impl<'hir> LoweringContext<'_, 'hir> { } else { None }; - self.dcx().emit_err(GenericTypeWithParentheses { span: data.span, sub }); + let guar = self + .dcx() + .emit_err(GenericTypeWithParentheses { span: data.span, sub }); ( self.lower_angle_bracketed_parameter_data( &data.as_angle_bracketed_args(), param_mode, - itctx, + ImplTraitContext::AlreadyErrored(guar), ) .0, false, diff --git a/tests/ui/typeck/parenthesized-type-params-in-paths.rs b/tests/ui/typeck/parenthesized-type-params-in-paths.rs index 85321cc305384..43500e2609622 100644 --- a/tests/ui/typeck/parenthesized-type-params-in-paths.rs +++ b/tests/ui/typeck/parenthesized-type-params-in-paths.rs @@ -39,3 +39,8 @@ fn foo() { let d : X() = Default::default(); //~^ ERROR parenthesized type parameters may only be used with a `Fn` trait } + +struct A(T); + +fn issue_161062() -> A(impl Sized) {} +//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait diff --git a/tests/ui/typeck/parenthesized-type-params-in-paths.stderr b/tests/ui/typeck/parenthesized-type-params-in-paths.stderr index 7a8a4afbfd113..93d11dcfc06f8 100644 --- a/tests/ui/typeck/parenthesized-type-params-in-paths.stderr +++ b/tests/ui/typeck/parenthesized-type-params-in-paths.stderr @@ -58,6 +58,18 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait LL | let d : X() = Default::default(); | ^^^ only `Fn` traits may use parentheses -error: aborting due to 10 previous errors +error[E0214]: parenthesized type parameters may only be used with a `Fn` trait + --> $DIR/parenthesized-type-params-in-paths.rs:45:22 + | +LL | fn issue_161062() -> A(impl Sized) {} + | ^^^^^^^^^^^^^ only `Fn` traits may use parentheses + | +help: use angle brackets instead + | +LL - fn issue_161062() -> A(impl Sized) {} +LL + fn issue_161062() -> A {} + | + +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0214`. From 5fa346974c49d6fbd81b0e17a5afa731d869cebd Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Mon, 17 Aug 2026 20:41:21 +0330 Subject: [PATCH 2/2] fix: handle parenthesized assoc constraint recovery Signed-off-by: Amirhossein Akhlaghpour --- compiler/rustc_ast_lowering/src/lib.rs | 21 ++++++++++++++----- .../parenthesized-type-params-in-paths.rs | 9 ++++++++ .../parenthesized-type-params-in-paths.stderr | 14 ++++++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index f957afc147d30..5f09614ccce37 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1321,11 +1321,11 @@ impl<'hir> LoweringContext<'_, 'hir> { span: data.span, } } else { - self.emit_bad_parenthesized_trait_in_assoc_ty(data); + let guar = self.emit_bad_parenthesized_trait_in_assoc_ty(data); self.lower_angle_bracketed_parameter_data( &data.as_angle_bracketed_args(), ParamMode::Explicit, - itctx, + ImplTraitContext::AlreadyErrored(guar), ) .0 } @@ -1394,7 +1394,10 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) { + fn emit_bad_parenthesized_trait_in_assoc_ty( + &self, + data: &ParenthesizedArgs, + ) -> ErrorGuaranteed { // Suggest removing empty parentheses: "Trait()" -> "Trait" let sub = if data.inputs.is_empty() { let parentheses_span = @@ -1415,7 +1418,7 @@ impl<'hir> LoweringContext<'_, 'hir> { data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi()); AssocTyParenthesesSub::NotEmpty { open_param, close_param } }; - self.dcx().emit_err(AssocTyParentheses { span: data.span, sub }); + self.dcx().emit_err(AssocTyParentheses { span: data.span, sub }) } #[instrument(level = "debug", skip(self))] @@ -1738,7 +1741,15 @@ impl<'hir> LoweringContext<'_, 'hir> { }); hir::TyKind::Err(guar) } - ImplTraitContext::AlreadyErrored(guar) => hir::TyKind::Err(guar), + ImplTraitContext::AlreadyErrored(guar) => { + // `GenericArgs::Parenthesized` stores its inputs as `Param`s, so the def + // collector visits `impl Trait` in a universal context and creates a + // `DefKind::TyParam`. During recovery we reinterpret these arguments as + // angle-bracketed, where lowering may otherwise expect an opaque type. + // The parenthesized syntax has already been rejected, so avoid lowering + // this `impl Trait` with the inconsistent `DefKind`. + hir::TyKind::Err(guar) + } } } TyKind::Pat(ty, pat) => { diff --git a/tests/ui/typeck/parenthesized-type-params-in-paths.rs b/tests/ui/typeck/parenthesized-type-params-in-paths.rs index 43500e2609622..4bc8142850c83 100644 --- a/tests/ui/typeck/parenthesized-type-params-in-paths.rs +++ b/tests/ui/typeck/parenthesized-type-params-in-paths.rs @@ -44,3 +44,12 @@ struct A(T); fn issue_161062() -> A(impl Sized) {} //~^ ERROR parenthesized type parameters may only be used with a `Fn` trait + +trait Trait { + type Assoc; +} + +fn issue_161062_assoc_constraint() -> &'static dyn Trait { + //~^ ERROR parenthesized generic arguments cannot be used in associated type constraints + loop {} +} diff --git a/tests/ui/typeck/parenthesized-type-params-in-paths.stderr b/tests/ui/typeck/parenthesized-type-params-in-paths.stderr index 93d11dcfc06f8..f22d9c06edead 100644 --- a/tests/ui/typeck/parenthesized-type-params-in-paths.stderr +++ b/tests/ui/typeck/parenthesized-type-params-in-paths.stderr @@ -70,6 +70,18 @@ LL - fn issue_161062() -> A(impl Sized) {} LL + fn issue_161062() -> A {} | -error: aborting due to 11 previous errors +error: parenthesized generic arguments cannot be used in associated type constraints + --> $DIR/parenthesized-type-params-in-paths.rs:52:58 + | +LL | fn issue_161062_assoc_constraint() -> &'static dyn Trait { + | ^^^^^^^^^^^^^^^^^ + | +help: use angle brackets instead + | +LL - fn issue_161062_assoc_constraint() -> &'static dyn Trait { +LL + fn issue_161062_assoc_constraint() -> &'static dyn Trait = usize> { + | + +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0214`.