diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 02deff6474080..c5eeb8b1aa856 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -806,6 +806,10 @@ impl<'tcx> ThirBuildCx<'tcx> { lit: ScalarInt::try_from_uint(val, Size::from_bits(32)).unwrap(), user_ty: None, }; + let mk_usize_kind = |val: u64| ExprKind::NonHirLiteral { + lit: ScalarInt::try_from_target_usize(val, tcx).unwrap(), + user_ty: None, + }; let mk_call = |thir: &mut Thir<'tcx>, ty: Ty<'tcx>, variant: VariantIdx, field: FieldIdx| { let fun_ty = @@ -842,7 +846,7 @@ impl<'tcx> ThirBuildCx<'tcx> { }); } - expr.unwrap_or(mk_u32_kind(0)) + expr.unwrap_or_else(|| mk_usize_kind(0)) } hir::ExprKind::ConstBlock(ref anon_const) => { diff --git a/tests/ui/offset-of/offset-of-error-recovery.rs b/tests/ui/offset-of/offset-of-error-recovery.rs new file mode 100644 index 0000000000000..659140877cd14 --- /dev/null +++ b/tests/ui/offset-of/offset-of-error-recovery.rs @@ -0,0 +1,14 @@ +use std::mem::offset_of; + +struct S { + x: (), +} + +impl S { + fn a() { + offset_of!(Self, Self::x); + //~^ ERROR offset_of expects dot-separated field and variant names + } +} + +fn main() {} diff --git a/tests/ui/offset-of/offset-of-error-recovery.stderr b/tests/ui/offset-of/offset-of-error-recovery.stderr new file mode 100644 index 0000000000000..fce3616c213ec --- /dev/null +++ b/tests/ui/offset-of/offset-of-error-recovery.stderr @@ -0,0 +1,8 @@ +error: offset_of expects dot-separated field and variant names + --> $DIR/offset-of-error-recovery.rs:9:26 + | +LL | offset_of!(Self, Self::x); + | ^^^^^^^ + +error: aborting due to 1 previous error +