Skip to content

Commit ac9bd09

Browse files
Rollup merge of rust-lang#153495 - TaKO8Ki:fix-153236-offset-of-recovery, r=petrochenkov
Fix ICE in `offset_of!` error recovery Fixes rust-lang#153236. `offset_of!` was changed in rust-lang#148151 to lower through THIR as a sum of calls to the `offset_of` intrinsic. In the error-recovery case, when no valid field indices are recorded, that lowering synthesized `0` as a `u32` even though the overall `offset_of!` expression has type `usize`. On 64-bit targets, const-eval then tried to write a 4-byte immediate into an 8-byte destination, which caused the ICE.
2 parents 6b42067 + e62f17c commit ac9bd09

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ impl<'tcx> ThirBuildCx<'tcx> {
806806
lit: ScalarInt::try_from_uint(val, Size::from_bits(32)).unwrap(),
807807
user_ty: None,
808808
};
809+
let mk_usize_kind = |val: u64| ExprKind::NonHirLiteral {
810+
lit: ScalarInt::try_from_target_usize(val, tcx).unwrap(),
811+
user_ty: None,
812+
};
809813
let mk_call =
810814
|thir: &mut Thir<'tcx>, ty: Ty<'tcx>, variant: VariantIdx, field: FieldIdx| {
811815
let fun_ty =
@@ -842,7 +846,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
842846
});
843847
}
844848

845-
expr.unwrap_or(mk_u32_kind(0))
849+
expr.unwrap_or_else(|| mk_usize_kind(0))
846850
}
847851

848852
hir::ExprKind::ConstBlock(ref anon_const) => {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::mem::offset_of;
2+
3+
struct S {
4+
x: (),
5+
}
6+
7+
impl S {
8+
fn a() {
9+
offset_of!(Self, Self::x);
10+
//~^ ERROR offset_of expects dot-separated field and variant names
11+
}
12+
}
13+
14+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: offset_of expects dot-separated field and variant names
2+
--> $DIR/offset-of-error-recovery.rs:9:26
3+
|
4+
LL | offset_of!(Self, Self::x);
5+
| ^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)