diff --git a/compiler/rustc_hir_typeck/src/intrinsicck.rs b/compiler/rustc_hir_typeck/src/intrinsicck.rs index 430f4d828b91f..5453575cf12c4 100644 --- a/compiler/rustc_hir_typeck/src/intrinsicck.rs +++ b/compiler/rustc_hir_typeck/src/intrinsicck.rs @@ -75,20 +75,16 @@ fn check_transmute<'tcx>( hir_id: HirId, ) -> Result<(), ErrorGuaranteed> { let span = tcx.hir_span(hir_id); - let normalize = |ty| { - if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)) { - ty - } else { - Ty::new_error_with_message( - tcx, - span, - format!("tried to normalize non-wf type {ty:#?} in check_transmute"), - ) - } + let normalize = |ty| -> Result, ErrorGuaranteed> { + tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)).map_err(|err| { + tcx.dcx() + .struct_span_err(span, LayoutError::NormalizationFailure(ty, err).to_string()) + .emit() + }) }; - let from = normalize(from); - let to = normalize(to); + let from = normalize(from)?; + let to = normalize(to)?; trace!(?from, ?to); // Transmutes that are only changing lifetimes are always ok. diff --git a/tests/ui/layout/normalization-failure.rs b/tests/ui/layout/normalization-failure.rs index a0195224d8ce1..19afe9bc06922 100644 --- a/tests/ui/layout/normalization-failure.rs +++ b/tests/ui/layout/normalization-failure.rs @@ -48,9 +48,7 @@ fn opaque() -> impl Project2 { fn check() { unsafe { std::mem::transmute::<_, ()>(opaque::().get()); - //~^ ERROR: cannot transmute - //~| NOTE: source type: `{type error}` (the type has an unknown layout) - //~| NOTE: target type: `()` (0 bits) + //~^ ERROR: unable to determine layout } } diff --git a/tests/ui/layout/normalization-failure.stderr b/tests/ui/layout/normalization-failure.stderr index 1c78fc6ac41c4..364694f610047 100644 --- a/tests/ui/layout/normalization-failure.stderr +++ b/tests/ui/layout/normalization-failure.stderr @@ -1,12 +1,8 @@ -error[E0512]: cannot transmute between types of different sizes, or dependently-sized types +error: unable to determine layout for `::Assoc2` because `::Assoc2` cannot be normalized --> $DIR/normalization-failure.rs:50:9 | LL | std::mem::transmute::<_, ()>(opaque::().get()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: source type: `{type error}` (the type has an unknown layout) - = note: target type: `()` (0 bits) error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0512`.