Skip to content

Commit

Permalink
Fix ICE when ADT tail has type error
Browse files Browse the repository at this point in the history
  • Loading branch information
gurry committed Apr 18, 2024
1 parent 7e3ba5b commit bdfddb3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ impl<'tcx> SizeSkeleton<'tcx> {
ty::Ref(_, pointee, _) | ty::RawPtr(pointee, _) => {
let non_zero = !ty.is_unsafe_ptr();
let tail = tcx.struct_tail_erasing_lifetimes(pointee, param_env);

// Check if tail has type error. Fixes ICE #124031
if let Err(guar) = tail.error_reported() {
return Err(tcx.arena.alloc(LayoutError::ReferencesError(guar)));
}

match tail.kind() {
ty::Param(_) | ty::Alias(ty::Projection | ty::Inherent, _) => {
debug_assert!(tail.has_non_region_param());
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/layout/ice-type-error-in-tail-124031.rs
Original file line number Diff line number Diff line change
@@ -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<()>, Option<&Other>>(None);
}
}
12 changes: 12 additions & 0 deletions tests/ui/layout/ice-type-error-in-tail-124031.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit bdfddb3

Please sign in to comment.