Skip to content

Commit 32f83e1

Browse files
Better error message
1 parent dd620aa commit 32f83e1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

compiler/rustc_ty_utils/src/layout.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,22 @@ fn layout_of_uncached<'tcx>(
156156
pointee_metadata,
157157
) {
158158
Ok(metadata_ty) => metadata_ty,
159-
Err(err) => return Err(LayoutError::NormalizationFailure(pointee, err)),
159+
Err(mut err) => {
160+
// Usually `<Ty as Pointee>::Metadata` can't be normalized because
161+
// its struct tail cannot be normalized either, so try to get a
162+
// more descriptive layout error here, which will lead to less confusing
163+
// diagnostics.
164+
match tcx.try_normalize_erasing_regions(
165+
param_env,
166+
tcx.struct_tail_without_normalization(pointee),
167+
) {
168+
Ok(_) => {},
169+
Err(better_err) => {
170+
err = better_err;
171+
}
172+
}
173+
return Err(LayoutError::NormalizationFailure(pointee, err));
174+
},
160175
};
161176

162177
let metadata_layout = cx.layout_of(metadata_ty)?;

tests/ui/layout/cannot-transmute-unnormalizable-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | std::mem::transmute::<Option<()>, Option<&Other>>(None);
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
|
1313
= note: source type: `Option<()>` (8 bits)
14-
= note: target type: `Option<&Other>` (unable to determine layout for `Other` because `<Other as Pointee>::Metadata` cannot be normalized)
14+
= note: target type: `Option<&Other>` (unable to determine layout for `Other` because `<() as Trait>::RefTarget` cannot be normalized)
1515

1616
error: aborting due to 2 previous errors
1717

0 commit comments

Comments
 (0)