Skip to content

Commit

Permalink
Handle normalization failure in struct_tail_erasing_lifetimes
Browse files Browse the repository at this point in the history
Fixes an ICE that occurred when the struct in question has an error
  • Loading branch information
gurry committed Apr 30, 2024
1 parent 9084601 commit e82e830
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
17 changes: 15 additions & 2 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ impl<'tcx> TyCtxt<'tcx> {
param_env: ty::ParamEnv<'tcx>,
) -> Ty<'tcx> {
let tcx = self;
tcx.struct_tail_with_normalize(ty, |ty| tcx.normalize_erasing_regions(param_env, ty), || {})
tcx.struct_tail_with_normalize(
ty,
|ty| match tcx.try_normalize_erasing_regions(param_env, ty) {
Ok(ty) => ty,
Err(_e) => {
if let Some(guar) = tcx.dcx().has_errors() {
Ty::new_error(tcx, guar)
} else {
bug!("normalization failed, but no errors reported");
}
}
},
|| {},
)
}

/// Returns the deeply last field of nested structures, or the same type if
Expand Down Expand Up @@ -252,7 +265,7 @@ impl<'tcx> TyCtxt<'tcx> {

ty::Alias(..) => {
let normalized = normalize(ty);
if ty == normalized {
if ty == normalized || normalized.references_error() {
return ty;
} else {
ty = normalized;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//@ known-bug: #113272
trait Trait {
type RefTarget;
}

impl Trait for () where Missing: Trait {}
//~^ ERROR cannot find type `Missing` in this scope
//~| ERROR not all trait items implemented, missing: `RefTarget`

struct Other {
data: <() as Trait>::RefTarget,
Expand All @@ -12,5 +13,6 @@ struct Other {
fn main() {
unsafe {
std::mem::transmute::<Option<()>, Option<&Other>>(None);
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
}
}
28 changes: 28 additions & 0 deletions tests/ui/structs/ice-struct-tail-normalization-113272.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0412]: cannot find type `Missing` in this scope
--> $DIR/ice-struct-tail-normalization-113272.rs:5:25
|
LL | impl Trait for () where Missing: Trait {}
| ^^^^^^^ not found in this scope

error[E0046]: not all trait items implemented, missing: `RefTarget`
--> $DIR/ice-struct-tail-normalization-113272.rs:5:1
|
LL | type RefTarget;
| -------------- `RefTarget` from trait
...
LL | impl Trait for () where Missing: Trait {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `RefTarget` in implementation

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> $DIR/ice-struct-tail-normalization-113272.rs:15:9
|
LL | std::mem::transmute::<Option<()>, Option<&Other>>(None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: `Option<()>` (8 bits)
= note: target type: `Option<&Other>` (unable to determine layout for `Other` because `<() as Trait>::RefTarget` cannot be normalized)

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0046, E0412, E0512.
For more information about an error, try `rustc --explain E0046`.

0 comments on commit e82e830

Please sign in to comment.