Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions compiler/rustc_hir_typeck/src/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ty<'tcx>, 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.
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/layout/normalization-failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ fn opaque<T>() -> impl Project2 {
fn check<T: Project1>() {
unsafe {
std::mem::transmute::<_, ()>(opaque::<T>().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
}
}

Expand Down
6 changes: 1 addition & 5 deletions tests/ui/layout/normalization-failure.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
error: unable to determine layout for `<impl Project2 as Project2>::Assoc2` because `<impl Project2 as Project2>::Assoc2` cannot be normalized
--> $DIR/normalization-failure.rs:50:9
|
LL | std::mem::transmute::<_, ()>(opaque::<T>().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`.
Loading