Skip to content

Commit dd620aa

Browse files
Don't ICE on unnormalized struct tail in layout computation
1 parent 3b2073f commit dd620aa

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

compiler/rustc_middle/src/ty/layout.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,13 @@ impl<'tcx> SizeSkeleton<'tcx> {
318318
Ok(layout) => {
319319
return Ok(SizeSkeleton::Known(layout.size));
320320
}
321-
Err(err) => err,
321+
Err(err @ LayoutError::Unknown(_)) => err,
322+
// We can't extract SizeSkeleton info from other layout errors
323+
Err(
324+
e @ LayoutError::Cycle
325+
| e @ LayoutError::SizeOverflow(_)
326+
| e @ LayoutError::NormalizationFailure(..),
327+
) => return Err(e),
322328
};
323329

324330
match *ty.kind() {

compiler/rustc_ty_utils/src/layout.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,34 @@ fn layout_of_uncached<'tcx>(
145145
return Ok(tcx.mk_layout(LayoutS::scalar(cx, data_ptr)));
146146
}
147147

148-
let unsized_part = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
149-
150148
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
151149
// Projection eagerly bails out when the pointee references errors,
152150
// fall back to structurally deducing metadata.
153151
&& !pointee.references_error()
154152
{
155-
let metadata_ty = tcx.normalize_erasing_regions(
153+
let pointee_metadata = tcx.mk_projection(metadata_def_id, [pointee]);
154+
let metadata_ty = match tcx.try_normalize_erasing_regions(
156155
param_env,
157-
tcx.mk_projection(metadata_def_id, [pointee]),
158-
);
156+
pointee_metadata,
157+
) {
158+
Ok(metadata_ty) => metadata_ty,
159+
Err(err) => return Err(LayoutError::NormalizationFailure(pointee, err)),
160+
};
161+
159162
let metadata_layout = cx.layout_of(metadata_ty)?;
160163
// If the metadata is a 1-zst, then the pointer is thin.
161164
if metadata_layout.is_zst() && metadata_layout.align.abi.bytes() == 1 {
162165
return Ok(tcx.mk_layout(LayoutS::scalar(cx, data_ptr)));
163166
}
164167

165168
let Abi::Scalar(metadata) = metadata_layout.abi else {
166-
return Err(LayoutError::Unknown(unsized_part));
169+
return Err(LayoutError::Unknown(pointee));
167170
};
171+
168172
metadata
169173
} else {
174+
let unsized_part = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
175+
170176
match unsized_part.kind() {
171177
ty::Foreign(..) => {
172178
return Ok(tcx.mk_layout(LayoutS::scalar(cx, data_ptr)));
@@ -178,7 +184,7 @@ fn layout_of_uncached<'tcx>(
178184
vtable
179185
}
180186
_ => {
181-
return Err(LayoutError::Unknown(unsized_part));
187+
return Err(LayoutError::Unknown(pointee));
182188
}
183189
}
184190
};

tests/ui/const-generics/issue-112505-overflow.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
44
LL | unsafe { std::mem::transmute(v) }
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[[u32; 8888888]; 9999999]; 777777777]` are too big for the current architecture)
8-
= note: target type: `[[[u32; 9999999]; 777777777]; 239]` (59484438436515561504 bits)
7+
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[u32; 8888888]; 9999999]` are too big for the current architecture)
8+
= note: target type: `[[[u32; 9999999]; 777777777]; 239]` (values of the type `[[u32; 9999999]; 777777777]` are too big for the current architecture)
99

1010
error: aborting due to previous error
1111

tests/ui/const-generics/transmute-fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
4343
LL | std::mem::transmute(v)
4444
| ^^^^^^^^^^^^^^^^^^^
4545
|
46-
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[[u32; 8888888]; 9999999]; 777777777]` are too big for the current architecture)
47-
= note: target type: `[[[u32; 9999999]; 777777777]; 8888888]` (values of the type `[[[u32; 9999999]; 777777777]; 8888888]` are too big for the current architecture)
46+
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[u32; 8888888]; 9999999]` are too big for the current architecture)
47+
= note: target type: `[[[u32; 9999999]; 777777777]; 8888888]` (values of the type `[[u32; 9999999]; 777777777]` are too big for the current architecture)
4848

4949
error: aborting due to 6 previous errors
5050

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
trait Trait {
2+
type RefTarget;
3+
}
4+
5+
impl Trait for ()
6+
where
7+
Missing: Trait,
8+
//~^ ERROR cannot find type `Missing` in this scope
9+
{
10+
type RefTarget = ();
11+
}
12+
13+
struct Other {
14+
data: <() as Trait>::RefTarget,
15+
}
16+
17+
fn main() {
18+
unsafe {
19+
std::mem::transmute::<Option<()>, Option<&Other>>(None);
20+
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0412]: cannot find type `Missing` in this scope
2+
--> $DIR/cannot-transmute-unnormalizable-type.rs:7:5
3+
|
4+
LL | Missing: Trait,
5+
| ^^^^^^^ not found in this scope
6+
7+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
8+
--> $DIR/cannot-transmute-unnormalizable-type.rs:19:9
9+
|
10+
LL | std::mem::transmute::<Option<()>, Option<&Other>>(None);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
|
13+
= 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)
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0412, E0512.
19+
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)