Skip to content

Commit a595f32

Browse files
committed
Auto merge of #126150 - RalfJung:offset_of_slice, r=compiler-errors
offset_of: allow (unstably) taking the offset of slice tail fields Fields of type `[T]` have a statically known offset, so there is no reason to forbid them in `offset_of!`. This PR adds the `offset_of_slice` feature to allow them. I created a tracking issue: #126151.
2 parents 6c4755d + eb584a2 commit a595f32

File tree

17 files changed

+215
-46
lines changed

17 files changed

+215
-46
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,10 @@ fn codegen_stmt<'tcx>(
832832
let val = match null_op {
833833
NullOp::SizeOf => layout.size.bytes(),
834834
NullOp::AlignOf => layout.align.abi.bytes(),
835-
NullOp::OffsetOf(fields) => {
836-
layout.offset_of_subfield(fx, fields.iter()).bytes()
837-
}
835+
NullOp::OffsetOf(fields) => fx
836+
.tcx
837+
.offset_of_subfield(ParamEnv::reveal_all(), layout, fields.iter())
838+
.bytes(),
838839
NullOp::UbChecks => {
839840
let val = fx.tcx.sess.ub_checks();
840841
let val = CValue::by_val(

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
680680
bx.cx().const_usize(val)
681681
}
682682
mir::NullOp::OffsetOf(fields) => {
683-
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
683+
let val = bx
684+
.tcx()
685+
.offset_of_subfield(bx.param_env(), layout, fields.iter())
686+
.bytes();
684687
bx.cx().const_usize(val)
685688
}
686689
mir::NullOp::UbChecks => {

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
253253
Scalar::from_target_usize(val, self)
254254
}
255255
mir::NullOp::OffsetOf(fields) => {
256-
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
256+
let val = self
257+
.tcx
258+
.offset_of_subfield(self.param_env, layout, fields.iter())
259+
.bytes();
257260
Scalar::from_target_usize(val, self)
258261
}
259262
mir::NullOp::UbChecks => Scalar::from_bool(self.tcx.sess.ub_checks()),

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ declare_features! (
559559
(unstable, offset_of_enum, "1.75.0", Some(120141)),
560560
/// Allows using multiple nested field accesses in offset_of!
561561
(unstable, offset_of_nested, "1.77.0", Some(120140)),
562+
/// Allows using fields with slice type in offset_of!
563+
(unstable, offset_of_slice, "CURRENT_RUSTC_VERSION", Some(126151)),
562564
/// Allows using `#[optimize(X)]`.
563565
(unstable, optimize_attribute, "1.34.0", Some(54882)),
564566
/// Allows postfix match `expr.match { ... }`

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,7 +3363,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33633363

33643364
let field_ty = self.field_ty(expr.span, field, args);
33653365

3366-
// FIXME: DSTs with static alignment should be allowed
3366+
// Enums are anyway always sized. But just to safeguard against future
3367+
// language extensions, let's double-check.
33673368
self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc);
33683369

33693370
if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
@@ -3391,8 +3392,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33913392
{
33923393
let field_ty = self.field_ty(expr.span, field, args);
33933394

3394-
// FIXME: DSTs with static alignment should be allowed
3395-
self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc);
3395+
if self.tcx.features().offset_of_slice {
3396+
self.require_type_has_static_alignment(
3397+
field_ty,
3398+
expr.span,
3399+
ObligationCauseCode::Misc,
3400+
);
3401+
} else {
3402+
self.require_type_is_sized(
3403+
field_ty,
3404+
expr.span,
3405+
ObligationCauseCode::Misc,
3406+
);
3407+
}
33963408

33973409
if field.vis.is_accessible_from(def_scope, self.tcx) {
33983410
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
@@ -3412,10 +3424,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34123424
if let Ok(index) = field.as_str().parse::<usize>()
34133425
&& field.name == sym::integer(index)
34143426
{
3415-
for ty in tys.iter().take(index + 1) {
3416-
self.require_type_is_sized(ty, expr.span, ObligationCauseCode::Misc);
3417-
}
34183427
if let Some(&field_ty) = tys.get(index) {
3428+
if self.tcx.features().offset_of_slice {
3429+
self.require_type_has_static_alignment(
3430+
field_ty,
3431+
expr.span,
3432+
ObligationCauseCode::Misc,
3433+
);
3434+
} else {
3435+
self.require_type_is_sized(
3436+
field_ty,
3437+
expr.span,
3438+
ObligationCauseCode::Misc,
3439+
);
3440+
}
3441+
34193442
field_indices.push((FIRST_VARIANT, index.into()));
34203443
current_container = field_ty;
34213444

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
386386
}
387387
}
388388

389+
pub fn require_type_has_static_alignment(
390+
&self,
391+
ty: Ty<'tcx>,
392+
span: Span,
393+
code: traits::ObligationCauseCode<'tcx>,
394+
) {
395+
if !ty.references_error() {
396+
let tail =
397+
self.tcx.struct_tail_with_normalize(ty, |ty| self.normalize(span, ty), || {});
398+
// Sized types have static alignment, and so do slices.
399+
if tail.is_trivially_sized(self.tcx) || matches!(tail.kind(), ty::Slice(..)) {
400+
// Nothing else is required here.
401+
} else {
402+
// We can't be sure, let's required full `Sized`.
403+
let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
404+
self.require_type_meets(ty, span, code, lang_item);
405+
}
406+
}
407+
}
408+
389409
pub fn register_bound(
390410
&self,
391411
ty: Ty<'tcx>,

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,3 +1351,37 @@ pub trait FnAbiOf<'tcx>: FnAbiOfHelpers<'tcx> {
13511351
}
13521352

13531353
impl<'tcx, C: FnAbiOfHelpers<'tcx>> FnAbiOf<'tcx> for C {}
1354+
1355+
impl<'tcx> TyCtxt<'tcx> {
1356+
pub fn offset_of_subfield<I>(
1357+
self,
1358+
param_env: ty::ParamEnv<'tcx>,
1359+
mut layout: TyAndLayout<'tcx>,
1360+
indices: I,
1361+
) -> Size
1362+
where
1363+
I: Iterator<Item = (VariantIdx, FieldIdx)>,
1364+
{
1365+
let cx = LayoutCx { tcx: self, param_env };
1366+
let mut offset = Size::ZERO;
1367+
1368+
for (variant, field) in indices {
1369+
layout = layout.for_variant(&cx, variant);
1370+
let index = field.index();
1371+
offset += layout.fields.offset(index);
1372+
layout = layout.field(&cx, index);
1373+
if !layout.is_sized() {
1374+
// If it is not sized, then the tail must still have at least a known static alignment.
1375+
let tail = self.struct_tail_erasing_lifetimes(layout.ty, param_env);
1376+
if !matches!(tail.kind(), ty::Slice(..)) {
1377+
bug!(
1378+
"offset of not-statically-aligned field (type {:?}) cannot be computed statically",
1379+
layout.ty
1380+
);
1381+
}
1382+
}
1383+
}
1384+
1385+
offset
1386+
}
1387+
}

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::bug;
1010
use rustc_middle::mir::interpret::{InterpResult, Scalar};
1111
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
1212
use rustc_middle::mir::*;
13-
use rustc_middle::ty::layout::LayoutOf;
13+
use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_mir_dataflow::value_analysis::{
1616
Map, PlaceIndex, State, TrackElem, ValueAnalysis, ValueAnalysisWrapper, ValueOrPlace,
@@ -285,9 +285,11 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
285285
let val = match null_op {
286286
NullOp::SizeOf if layout.is_sized() => layout.size.bytes(),
287287
NullOp::AlignOf if layout.is_sized() => layout.align.abi.bytes(),
288-
NullOp::OffsetOf(fields) => {
289-
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
290-
}
288+
NullOp::OffsetOf(fields) => self
289+
.ecx
290+
.tcx
291+
.offset_of_subfield(self.ecx.param_env(), layout, fields.iter())
292+
.bytes(),
291293
_ => return ValueOrPlace::Value(FlatSet::Top),
292294
};
293295
FlatSet::Elem(Scalar::from_target_usize(val, &self.tcx))

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use rustc_middle::bug;
9595
use rustc_middle::mir::interpret::GlobalAlloc;
9696
use rustc_middle::mir::visit::*;
9797
use rustc_middle::mir::*;
98-
use rustc_middle::ty::layout::LayoutOf;
98+
use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
9999
use rustc_middle::ty::{self, Ty, TyCtxt};
100100
use rustc_span::def_id::DefId;
101101
use rustc_span::DUMMY_SP;
@@ -484,9 +484,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
484484
let val = match null_op {
485485
NullOp::SizeOf => layout.size.bytes(),
486486
NullOp::AlignOf => layout.align.abi.bytes(),
487-
NullOp::OffsetOf(fields) => {
488-
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
489-
}
487+
NullOp::OffsetOf(fields) => self
488+
.ecx
489+
.tcx
490+
.offset_of_subfield(self.ecx.param_env(), layout, fields.iter())
491+
.bytes(),
490492
NullOp::UbChecks => return None,
491493
};
492494
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();

compiler/rustc_mir_transform/src/known_panics_lint.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
625625
let val = match null_op {
626626
NullOp::SizeOf => op_layout.size.bytes(),
627627
NullOp::AlignOf => op_layout.align.abi.bytes(),
628-
NullOp::OffsetOf(fields) => {
629-
op_layout.offset_of_subfield(self, fields.iter()).bytes()
630-
}
628+
NullOp::OffsetOf(fields) => self
629+
.tcx
630+
.offset_of_subfield(self.param_env, op_layout, fields.iter())
631+
.bytes(),
631632
NullOp::UbChecks => return None,
632633
};
633634
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()

0 commit comments

Comments
 (0)