Skip to content

Commit 61f23e0

Browse files
committed
intern offsetof fields
1 parent 511e457 commit 61f23e0

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
719719

720720
let mut current_ty = *container;
721721

722-
for &field in fields {
722+
for field in fields.iter() {
723723
match current_ty.kind() {
724724
ty::Tuple(fields) => {
725725
let Some(&f_ty) = fields.get(field.as_usize()) else {

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ pub enum Rvalue<'tcx> {
11151115
CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
11161116

11171117
/// Computes a value as described by the operation.
1118-
NullaryOp(NullOp, Ty<'tcx>),
1118+
NullaryOp(NullOp<'tcx>, Ty<'tcx>),
11191119

11201120
/// Exactly like `BinaryOp`, but less operands.
11211121
///
@@ -1212,13 +1212,13 @@ pub enum AggregateKind<'tcx> {
12121212
}
12131213

12141214
#[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1215-
pub enum NullOp {
1215+
pub enum NullOp<'tcx> {
12161216
/// Returns the size of a value of that type
12171217
SizeOf,
12181218
/// Returns the minimum alignment of a type
12191219
AlignOf,
12201220
/// Returns the offset of a field
1221-
OffsetOf(Vec<FieldIdx>),
1221+
OffsetOf(&'tcx List<FieldIdx>),
12221222
}
12231223

12241224
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -1288,6 +1288,6 @@ mod size_asserts {
12881288
static_assert_size!(Operand<'_>, 24);
12891289
static_assert_size!(Place<'_>, 16);
12901290
static_assert_size!(PlaceElem<'_>, 24);
1291-
static_assert_size!(Rvalue<'_>, 48);
1291+
static_assert_size!(Rvalue<'_>, 40);
12921292
// tidy-alphabetical-end
12931293
}

compiler/rustc_middle/src/mir/type_foldable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ TrivialTypeTraversalAndLiftImpls! {
1616
UserTypeAnnotationIndex,
1717
BorrowKind,
1818
CastKind,
19-
NullOp,
2019
hir::Movability,
2120
BasicBlock,
2221
SwitchTargets,
@@ -26,6 +25,7 @@ TrivialTypeTraversalAndLiftImpls! {
2625

2726
TrivialTypeTraversalImpls! {
2827
ConstValue<'tcx>,
28+
NullOp<'tcx>,
2929
}
3030

3131
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [InlineAsmTemplatePiece] {

compiler/rustc_middle/src/thir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::mir::interpret::AllocId;
2020
use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Mutability, UnOp};
2121
use rustc_middle::ty::adjustment::PointerCast;
2222
use rustc_middle::ty::subst::SubstsRef;
23-
use rustc_middle::ty::{self, AdtDef, FnSig, Ty, UpvarSubsts};
23+
use rustc_middle::ty::{self, AdtDef, FnSig, List, Ty, UpvarSubsts};
2424
use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation};
2525
use rustc_span::def_id::LocalDefId;
2626
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
@@ -484,7 +484,7 @@ pub enum ExprKind<'tcx> {
484484
/// Field offset (`offset_of!`)
485485
OffsetOf {
486486
container: Ty<'tcx>,
487-
fields: Vec<FieldIdx>,
487+
fields: &'tcx List<FieldIdx>,
488488
},
489489
/// An expression taking a reference to a thread local.
490490
ThreadLocalRef(DefId),

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_data_structures::fx::FxHashMap;
1919
use rustc_middle::ty::TyCtxt;
2020
use rustc_serialize::{Decodable, Encodable};
2121
use rustc_span::Span;
22+
use rustc_target::abi::FieldIdx;
2223
pub use rustc_type_ir::{TyDecoder, TyEncoder};
2324
use std::hash::Hash;
2425
use std::intrinsics;
@@ -401,6 +402,15 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<ty
401402
}
402403
}
403404

405+
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<FieldIdx> {
406+
fn decode(decoder: &mut D) -> &'tcx Self {
407+
let len = decoder.read_usize();
408+
decoder
409+
.interner()
410+
.mk_fields_from_iter((0..len).map::<FieldIdx, _>(|_| Decodable::decode(decoder)))
411+
}
412+
}
413+
404414
impl_decodable_via_ref! {
405415
&'tcx ty::TypeckResults<'tcx>,
406416
&'tcx ty::List<Ty<'tcx>>,
@@ -412,6 +422,7 @@ impl_decodable_via_ref! {
412422
&'tcx mir::coverage::CodeRegion,
413423
&'tcx ty::List<ty::BoundVariableKind>,
414424
&'tcx ty::List<ty::Predicate<'tcx>>,
425+
&'tcx ty::List<FieldIdx>,
415426
}
416427

417428
#[macro_export]

compiler/rustc_middle/src/ty/context.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub struct CtxtInterners<'tcx> {
155155
layout: InternedSet<'tcx, LayoutS>,
156156
adt_def: InternedSet<'tcx, AdtDefData>,
157157
external_constraints: InternedSet<'tcx, ExternalConstraintsData<'tcx>>,
158+
fields: InternedSet<'tcx, List<FieldIdx>>,
158159
}
159160

160161
impl<'tcx> CtxtInterners<'tcx> {
@@ -178,6 +179,7 @@ impl<'tcx> CtxtInterners<'tcx> {
178179
layout: Default::default(),
179180
adt_def: Default::default(),
180181
external_constraints: Default::default(),
182+
fields: Default::default(),
181183
}
182184
}
183185

@@ -1585,6 +1587,7 @@ slice_interners!(
15851587
projs: pub mk_projs(ProjectionKind),
15861588
place_elems: pub mk_place_elems(PlaceElem<'tcx>),
15871589
bound_variable_kinds: pub mk_bound_variable_kinds(ty::BoundVariableKind),
1590+
fields: pub mk_fields(FieldIdx),
15881591
);
15891592

15901593
impl<'tcx> TyCtxt<'tcx> {
@@ -2253,6 +2256,14 @@ impl<'tcx> TyCtxt<'tcx> {
22532256
T::collect_and_apply(iter, |xs| self.mk_place_elems(xs))
22542257
}
22552258

2259+
pub fn mk_fields_from_iter<I, T>(self, iter: I) -> T::Output
2260+
where
2261+
I: Iterator<Item = T>,
2262+
T: CollectAndApply<FieldIdx, &'tcx List<FieldIdx>>,
2263+
{
2264+
T::collect_and_apply(iter, |xs| self.mk_fields(xs))
2265+
}
2266+
22562267
pub fn mk_substs_trait(
22572268
self,
22582269
self_ty: Ty<'tcx>,

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
481481
}))))
482482
}
483483

484-
ExprKind::OffsetOf { container, ref fields } => {
485-
block.and(Rvalue::NullaryOp(NullOp::OffsetOf(fields.clone()), container))
484+
ExprKind::OffsetOf { container, fields } => {
485+
block.and(Rvalue::NullaryOp(NullOp::OffsetOf(fields), container))
486486
}
487487

488488
ExprKind::Literal { .. }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ impl<'tcx> Cx<'tcx> {
667667
hir::ExprKind::OffsetOf(_, _) => {
668668
let data = self.typeck_results.offset_of_data();
669669
let &(container, ref indices) = data.get(expr.hir_id).unwrap();
670-
let fields = indices.iter().copied().collect();
670+
let fields = tcx.mk_fields_from_iter(indices.iter().copied());
671671

672672
ExprKind::OffsetOf { container, fields }
673673
}

0 commit comments

Comments
 (0)