diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 6381bd190ac0a..897d6f5662f60 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -338,6 +338,10 @@ impl<'tcx> SizeSkeleton<'tcx> { debug_assert!(tail.has_non_region_param()); Ok(SizeSkeleton::Pointer { non_zero, tail: tcx.erase_regions(tail) }) } + ty::Error(guar) => { + // Fixes ICE #124031 + return Err(tcx.arena.alloc(LayoutError::ReferencesError(*guar))); + } _ => bug!( "SizeSkeleton::compute({ty}): layout errored ({err:?}), yet \ tail `{tail}` is not a type parameter or a projection", diff --git a/tests/ui/layout/ice-type-error-in-tail-124031.rs b/tests/ui/layout/ice-type-error-in-tail-124031.rs new file mode 100644 index 0000000000000..0a2be11740358 --- /dev/null +++ b/tests/ui/layout/ice-type-error-in-tail-124031.rs @@ -0,0 +1,20 @@ +// Regression test for issue #124031 +// Checks that we don't ICE when the tail +// of an ADT has a type error + +trait Trait { + type RefTarget; +} + +impl Trait for () {} +//~^ ERROR not all trait items implemented, missing: `RefTarget` + +struct Other { + data: <() as Trait>::RefTarget, +} + +fn main() { + unsafe { + std::mem::transmute::, Option<&Other>>(None); + } +} diff --git a/tests/ui/layout/ice-type-error-in-tail-124031.stderr b/tests/ui/layout/ice-type-error-in-tail-124031.stderr new file mode 100644 index 0000000000000..57dc83f92dfda --- /dev/null +++ b/tests/ui/layout/ice-type-error-in-tail-124031.stderr @@ -0,0 +1,12 @@ +error[E0046]: not all trait items implemented, missing: `RefTarget` + --> $DIR/ice-type-error-in-tail-124031.rs:9:1 + | +LL | type RefTarget; + | -------------- `RefTarget` from trait +... +LL | impl Trait for () {} + | ^^^^^^^^^^^^^^^^^ missing `RefTarget` in implementation + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0046`.