Skip to content

Commit 1bd8591

Browse files
committed
Auto merge of rust-lang#148151 - cjgillot:no-offset-of, r=scottmcm
Replace OffsetOf by an actual sum of calls to intrinsic. This PR changes the way we compute the value of the `offset_of!` macro in MIR. The current implementation uses a dedicated MIR rvalue. This PR proposes to replace it by an inline constant which sums calls to a new intrinsic `offset_of(variant index, field index)`. The desugaring is done at THIR building time, easier that doing it on MIR. The new intrinsic is only meant to be used by const-eval. LLVM codegen will refuse to generate code for it. We replace: ```rust a = offset_of!(T, Variant1.Field1.Variant2.Field2); ``` By: ```rust a = const {constant#n}; {constant#n}: usize = { _1 = offset_of::<T>(index of Variant1, index of Field1); _2 = offset_of::<U>(index of Variant2, index of Field2); // Where T::Variant1::Field1 has type U _0 = _1 + _2 } ``` The second commit modifies intrinsic const checking to take `allow_internal_unstable` into account. The new intrinsic should only be called from stable `offset_of!` macro. The intrinsic itself is unstable, const-unstable, but `rustc_intrinsic_const_stable_indirect`. Fixes rust-lang#123959 Fixes rust-lang#125680 Fixes rust-lang#129425 Fixes rust-lang#136175 r? `@ghost`
2 parents 0486301 + 4b4b2d0 commit 1bd8591

File tree

4 files changed

+9
-19
lines changed

4 files changed

+9
-19
lines changed

compiler/rustc_public/src/mir/body.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub enum Rvalue {
588588
ThreadLocalRef(crate::CrateItem),
589589

590590
/// Computes a value as described by the operation.
591-
NullaryOp(NullOp, Ty),
591+
NullaryOp(NullOp),
592592

593593
/// Exactly like `BinaryOp`, but less operands.
594594
///
@@ -641,8 +641,7 @@ impl Rvalue {
641641
.discriminant_ty()
642642
.ok_or_else(|| error!("Expected a `RigidTy` but found: {place_ty:?}"))
643643
}
644-
Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => Ok(Ty::usize_ty()),
645-
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => Ok(Ty::bool_ty()),
644+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => Ok(Ty::bool_ty()),
646645
Rvalue::Aggregate(ak, ops) => match *ak {
647646
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
648647
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1021,8 +1020,6 @@ pub enum CastKind {
10211020

10221021
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
10231022
pub enum NullOp {
1024-
/// Returns the offset of a field.
1025-
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
10261023
/// Codegen conditions for runtime checks.
10271024
RuntimeChecks(RuntimeChecks),
10281025
}

compiler/rustc_public/src/mir/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
386386
Rvalue::ThreadLocalRef(item) => {
387387
write!(writer, "thread_local_ref{item:?}")
388388
}
389-
Rvalue::NullaryOp(nul, ty) => {
390-
write!(writer, "{nul:?}::<{ty}>() \" \"")
389+
Rvalue::NullaryOp(nul) => {
390+
write!(writer, "{nul:?}() \" \"")
391391
}
392392
Rvalue::UnaryOp(un, op) => {
393393
write!(writer, "{:?}({})", un, pretty_operand(op))

compiler/rustc_public/src/mir/visit.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ macro_rules! make_mir_visitor {
282282
self.visit_operand(op, location)
283283
}
284284
Rvalue::ThreadLocalRef(_) => {}
285-
Rvalue::NullaryOp(_, ty) => {
286-
self.visit_ty(ty, location);
287-
}
285+
Rvalue::NullaryOp(_) => {}
288286
Rvalue::UnaryOp(_, op) | Rvalue::Use(op) => {
289287
self.visit_operand(op, location);
290288
}

compiler/rustc_public/src/unstable/convert/stable/mir.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
232232
)
233233
}
234234
}
235-
NullaryOp(null_op, ty) => {
236-
crate::mir::Rvalue::NullaryOp(null_op.stable(tables, cx), ty.stable(tables, cx))
237-
}
235+
NullaryOp(null_op) => crate::mir::Rvalue::NullaryOp(null_op.stable(tables, cx)),
238236
UnaryOp(un_op, op) => {
239237
crate::mir::Rvalue::UnaryOp(un_op.stable(tables, cx), op.stable(tables, cx))
240238
}
@@ -314,19 +312,16 @@ impl<'tcx> Stable<'tcx> for mir::FakeBorrowKind {
314312
}
315313
}
316314

317-
impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
315+
impl<'tcx> Stable<'tcx> for mir::NullOp {
318316
type T = crate::mir::NullOp;
319317
fn stable<'cx>(
320318
&self,
321-
tables: &mut Tables<'cx, BridgeTys>,
322-
cx: &CompilerCtxt<'cx, BridgeTys>,
319+
_: &mut Tables<'cx, BridgeTys>,
320+
_: &CompilerCtxt<'cx, BridgeTys>,
323321
) -> Self::T {
324322
use rustc_middle::mir::NullOp::*;
325323
use rustc_middle::mir::RuntimeChecks::*;
326324
match self {
327-
OffsetOf(indices) => crate::mir::NullOp::OffsetOf(
328-
indices.iter().map(|idx| idx.stable(tables, cx)).collect(),
329-
),
330325
RuntimeChecks(op) => crate::mir::NullOp::RuntimeChecks(match op {
331326
UbChecks => crate::mir::RuntimeChecks::UbChecks,
332327
ContractChecks => crate::mir::RuntimeChecks::ContractChecks,

0 commit comments

Comments
 (0)