diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 2d87d8c84d7c9..bde9d1c39e0ad 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -1,5 +1,5 @@ use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; -use rustc_ast::{self as ast, AttrVec, NodeId, PatKind, attr, token}; +use rustc_ast::{self as ast, AttrVec, GenericBound, NodeId, PatKind, attr, token}; use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute, Features}; use rustc_session::Session; use rustc_session::parse::{feature_err, feature_warn}; @@ -152,7 +152,14 @@ impl<'a> PostExpansionVisitor<'a> { for param in params { if !param.bounds.is_empty() { let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect(); - self.sess.dcx().emit_err(errors::ForbiddenBound { spans }); + if param.bounds.iter().any(|bound| matches!(bound, GenericBound::Trait(_))) { + // Issue #149695 + // Abort immediately otherwise items defined in complex bounds will be lowered into HIR, + // which will cause ICEs when errors of the items visit unlowered parents. + self.sess.dcx().emit_fatal(errors::ForbiddenBound { spans }); + } else { + self.sess.dcx().emit_err(errors::ForbiddenBound { spans }); + } } } } diff --git a/tests/ui/closures/binder/bounds-on-closure-type-binders.rs b/tests/ui/closures/binder/bounds-on-closure-type-binders.rs index cf53241407feb..80f48a1e2b400 100644 --- a/tests/ui/closures/binder/bounds-on-closure-type-binders.rs +++ b/tests/ui/closures/binder/bounds-on-closure-type-binders.rs @@ -10,5 +10,4 @@ fn main() { // Regression test for issue #119067 let _ = for || -> () {}; //~^ ERROR bounds cannot be used in this context - //~| ERROR late-bound type parameter not allowed on closures } diff --git a/tests/ui/closures/binder/bounds-on-closure-type-binders.stderr b/tests/ui/closures/binder/bounds-on-closure-type-binders.stderr index 9cb921f66314c..50fa11ed0ed1c 100644 --- a/tests/ui/closures/binder/bounds-on-closure-type-binders.stderr +++ b/tests/ui/closures/binder/bounds-on-closure-type-binders.stderr @@ -4,11 +4,5 @@ error: bounds cannot be used in this context LL | let _ = for || -> () {}; | ^^^^^ -error: late-bound type parameter not allowed on closures - --> $DIR/bounds-on-closure-type-binders.rs:11:17 - | -LL | let _ = for || -> () {}; - | ^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error diff --git a/tests/ui/traits/non_lifetime_binders/bad-bounds.rs b/tests/ui/traits/non_lifetime_binders/bad-bounds.rs new file mode 100644 index 0000000000000..29d7a21ff9585 --- /dev/null +++ b/tests/ui/traits/non_lifetime_binders/bad-bounds.rs @@ -0,0 +1,11 @@ +//@ edition 2024 + +#![feature(non_lifetime_binders)] +fn produce() -> for; //~ ERROR the name `A` is defined multiple times +}>> Trait {} //~ ERROR cannot find trait `Trait` in this scope + +fn main() {} diff --git a/tests/ui/traits/non_lifetime_binders/bad-bounds.stderr b/tests/ui/traits/non_lifetime_binders/bad-bounds.stderr new file mode 100644 index 0000000000000..6a0e5043a5453 --- /dev/null +++ b/tests/ui/traits/non_lifetime_binders/bad-bounds.stderr @@ -0,0 +1,46 @@ +error[E0428]: the name `A` is defined multiple times + --> $DIR/bad-bounds.rs:8:5 + | +LL | enum A {} + | ------ previous definition of the type `A` here +LL | struct A; + | ^^^^^^^^^^^^ `A` redefined here + | + = note: `A` must be defined only once in the type namespace of this block + +error[E0404]: expected trait, found type parameter `A` + --> $DIR/bad-bounds.rs:4:24 + | +LL | fn produce() -> for; +LL | | }>> Trait {} + | |__^ not a trait + +error[E0405]: cannot find trait `Trait` in this scope + --> $DIR/bad-bounds.rs:9:5 + | +LL | }>> Trait {} + | ^^^^^ not found in this scope + +error: bounds cannot be used in this context + --> $DIR/bad-bounds.rs:4:24 + | +LL | fn produce() -> for; +LL | | }>> Trait {} + | |__^ + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0404, E0405, E0428. +For more information about an error, try `rustc --explain E0404`.