diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index c5ebc2d26a794..6bca15e0c76b8 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -41,9 +41,9 @@ fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable> + Par // fresh `InferCtxt`. If this assert does trigger, it will give // us a test case. debug_assert_eq!(normalized_value, resolved_value); + let erased = infcx.tcx.erase_regions(resolved_value); - debug_assert!(!erased.has_infer(), "{erased:?}"); - Ok(erased) + if !erased.has_infer() { Ok(erased) } else { Err(NoSolution) } } Err(NoSolution) => Err(NoSolution), } diff --git a/tests/crashes/126942.rs b/tests/crashes/126942.rs deleted file mode 100644 index e4adc8fab287e..0000000000000 --- a/tests/crashes/126942.rs +++ /dev/null @@ -1,11 +0,0 @@ -//@ known-bug: rust-lang/rust#126942 -struct Thing; - -pub trait Every { - type Assoc; -} -impl Every for Thing { - type Assoc = T; -} - -static I: ::Assoc = 3; diff --git a/tests/ui/layout/ice-layout-of-invalid-type-126942.rs b/tests/ui/layout/ice-layout-of-invalid-type-126942.rs new file mode 100644 index 0000000000000..999a7496acaed --- /dev/null +++ b/tests/ui/layout/ice-layout-of-invalid-type-126942.rs @@ -0,0 +1,25 @@ +// Regression test for #126942 and #127804 + +// Tests that we do not ICE when a projection +// type cannot be fully normalized + +struct Thing; + +pub trait Every { + type Assoc; +} +impl Every for Thing { +//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates + type Assoc = T; +} + + // The type of this static normalizes to `Infer` + // thanks to the `?Sized` constraint in the impl above +static I: ::Assoc = 3; +//~^ ERROR type annotations needed + +fn foo(_: ::Assoc) {} +//~^ ERROR type annotations needed +//~| ERROR type annotations needed + +fn main() {} diff --git a/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr b/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr new file mode 100644 index 0000000000000..db7947314678b --- /dev/null +++ b/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr @@ -0,0 +1,31 @@ +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/ice-layout-of-invalid-type-126942.rs:11:6 + | +LL | impl Every for Thing { + | ^ unconstrained type parameter + +error[E0283]: type annotations needed + --> $DIR/ice-layout-of-invalid-type-126942.rs:18:11 + | +LL | static I: ::Assoc = 3; + | ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` + | + = note: cannot satisfy `_: Sync` + = note: shared static variables must have a type that implements `Sync` + +error[E0282]: type annotations needed + --> $DIR/ice-layout-of-invalid-type-126942.rs:21:11 + | +LL | fn foo(_: ::Assoc) {} + | ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` + +error[E0282]: type annotations needed + --> $DIR/ice-layout-of-invalid-type-126942.rs:21:8 + | +LL | fn foo(_: ::Assoc) {} + | ^ cannot infer type for type parameter `T` + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0207, E0282, E0283. +For more information about an error, try `rustc --explain E0207`.