From ca2178dd9fe7f036537414def4593258ca4adb7b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 6 Mar 2026 22:19:11 +0900 Subject: [PATCH 1/3] add UI regression test for offset_of! recovery --- tests/ui/offset-of/offset-of-error-recovery.rs | 14 ++++++++++++++ tests/ui/offset-of/offset-of-error-recovery.stderr | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/ui/offset-of/offset-of-error-recovery.rs create mode 100644 tests/ui/offset-of/offset-of-error-recovery.stderr 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 + From ed12fe81b07cf7c2371306a3400bccc9e2b117e8 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 6 Mar 2026 22:19:23 +0900 Subject: [PATCH 2/3] fix ICE in offset_of! error recovery --- compiler/rustc_mir_build/src/thir/cx/expr.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 02deff6474080..8cc5cda59ab0b 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_uint(val, tcx.data_layout.pointer_size()).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) => { From e62f17cfc4c3fa13fef99b90df8e1c87d2ea8f89 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 6 Mar 2026 22:31:01 +0900 Subject: [PATCH 3/3] use try_from_target_usize instead of try_from_uint --- compiler/rustc_mir_build/src/thir/cx/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 8cc5cda59ab0b..c5eeb8b1aa856 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -807,7 +807,7 @@ impl<'tcx> ThirBuildCx<'tcx> { user_ty: None, }; let mk_usize_kind = |val: u64| ExprKind::NonHirLiteral { - lit: ScalarInt::try_from_uint(val, tcx.data_layout.pointer_size()).unwrap(), + lit: ScalarInt::try_from_target_usize(val, tcx).unwrap(), user_ty: None, }; let mk_call =