Skip to content

Commit cac431b

Browse files
committed
Store a DefId instead of an AdtDef in AggregateKind::Adt
The `AggregateKind` enum ends up in the final mir `Body`. Currently, any changes to `AdtDef` (regardless of how significant they are) will legitimately cause the overall result of `optimized_mir` to change, invalidating any codegen re-use involving that mir. This will get worse once we start hashing the `Span` inside `FieldDef` (which is itself contained in `AdtDef`). To try to reduce these kinds of invalidations, this commit changes `AggregateKind::Adt` to store just the `DefId`, instead of the full `AdtDef`. This allows the result of `optimized_mir` to be unchanged if the `AdtDef` changes in a way that doesn't actually affect any of the MIR we build.
1 parent e100ec5 commit cac431b

File tree

11 files changed

+33
-32
lines changed

11 files changed

+33
-32
lines changed

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19161916
let tcx = self.tcx();
19171917

19181918
match *ak {
1919-
AggregateKind::Adt(def, variant_index, substs, _, active_field_index) => {
1919+
AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
1920+
let def = tcx.adt_def(adt_did);
19201921
let variant = &def.variants[variant_index];
19211922
let adj_field_index = active_field_index.unwrap_or(field_index);
19221923
if let Some(field) = variant.fields.get(adj_field_index) {
@@ -2621,8 +2622,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26212622
);
26222623

26232624
let (def_id, instantiated_predicates) = match aggregate_kind {
2624-
AggregateKind::Adt(def, _, substs, _, _) => {
2625-
(def.did, tcx.predicates_of(def.did).instantiate(tcx, substs))
2625+
AggregateKind::Adt(adt_did, _, substs, _, _) => {
2626+
(*adt_did, tcx.predicates_of(*adt_did).instantiate(tcx, substs))
26262627
}
26272628

26282629
// For closures, we have some **extra requirements** we

Diff for: compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
112112

113113
mir::Rvalue::Aggregate(ref kind, ref operands) => {
114114
let (dest, active_field_index) = match **kind {
115-
mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
115+
mir::AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
116116
dest.codegen_set_discr(&mut bx, variant_index);
117-
if adt_def.is_enum() {
117+
if bx.tcx().adt_def(adt_did).is_enum() {
118118
(dest.project_downcast(&mut bx, variant_index), active_field_index)
119119
} else {
120120
(dest, active_field_index)

Diff for: compiler/rustc_const_eval/src/interpret/step.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
199199
Aggregate(ref kind, ref operands) => {
200200
// active_field_index is for union initialization.
201201
let (dest, active_field_index) = match **kind {
202-
mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
202+
mir::AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
203203
self.write_discriminant(variant_index, &dest)?;
204-
if adt_def.is_enum() {
204+
if self.tcx.adt_def(adt_did).is_enum() {
205205
assert!(active_field_index.is_none());
206206
(self.place_downcast(&dest, variant_index)?, None)
207207
} else {

Diff for: compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ where
270270
Rvalue::Aggregate(kind, operands) => {
271271
// Return early if we know that the struct or enum being constructed is always
272272
// qualified.
273-
if let AggregateKind::Adt(def, _, substs, ..) = **kind {
273+
if let AggregateKind::Adt(adt_did, _, substs, ..) = **kind {
274+
let def = cx.tcx.adt_def(adt_did);
274275
if Q::in_adt_inherently(cx, def, substs) {
275276
return true;
276277
}

Diff for: compiler/rustc_const_eval/src/util/aggregate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub fn expand_aggregate<'tcx>(
2222
) -> impl Iterator<Item = Statement<'tcx>> + TrustedLen {
2323
let mut set_discriminant = None;
2424
let active_field_index = match kind {
25-
AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
25+
AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
26+
let adt_def = tcx.adt_def(adt_did);
2627
if adt_def.is_enum() {
2728
set_discriminant = Some(Statement {
2829
kind: StatementKind::SetDiscriminant { place: Box::new(lhs), variant_index },

Diff for: compiler/rustc_middle/src/mir/mod.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ pub enum AggregateKind<'tcx> {
22682268
/// active field number and is present only for union expressions
22692269
/// -- e.g., for a union expression `SomeUnion { c: .. }`, the
22702270
/// active field index would identity the field `c`
2271-
Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
2271+
Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
22722272

22732273
Closure(DefId, SubstsRef<'tcx>),
22742274
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
@@ -2427,28 +2427,26 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24272427
}
24282428
}
24292429

2430-
AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
2431-
let variant_def = &adt_def.variants[variant];
2432-
2433-
let name = ty::tls::with(|tcx| {
2430+
AggregateKind::Adt(adt_did, variant, substs, _user_ty, _) => {
2431+
ty::tls::with(|tcx| {
24342432
let mut name = String::new();
2433+
let variant_def = &tcx.adt_def(adt_did).variants[variant];
24352434
let substs = tcx.lift(substs).expect("could not lift for printing");
24362435
FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
24372436
.print_def_path(variant_def.def_id, substs)?;
2438-
Ok(name)
2439-
})?;
2440-
2441-
match variant_def.ctor_kind {
2442-
CtorKind::Const => fmt.write_str(&name),
2443-
CtorKind::Fn => fmt_tuple(fmt, &name),
2444-
CtorKind::Fictive => {
2445-
let mut struct_fmt = fmt.debug_struct(&name);
2446-
for (field, place) in iter::zip(&variant_def.fields, places) {
2447-
struct_fmt.field(field.ident.as_str(), place);
2437+
2438+
match variant_def.ctor_kind {
2439+
CtorKind::Const => fmt.write_str(&name),
2440+
CtorKind::Fn => fmt_tuple(fmt, &name),
2441+
CtorKind::Fictive => {
2442+
let mut struct_fmt = fmt.debug_struct(&name);
2443+
for (field, place) in iter::zip(&variant_def.fields, places) {
2444+
struct_fmt.field(field.ident.as_str(), place);
2445+
}
2446+
struct_fmt.finish()
24482447
}
2449-
struct_fmt.finish()
24502448
}
2451-
}
2449+
})
24522450
}
24532451

24542452
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {

Diff for: compiler/rustc_middle/src/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'tcx> Rvalue<'tcx> {
200200
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
201201
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
202202
AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),
203-
AggregateKind::Adt(def, _, substs, _, _) => tcx.type_of(def.did).subst(tcx, substs),
203+
AggregateKind::Adt(did, _, substs, _, _) => tcx.type_of(did).subst(tcx, substs),
204204
AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
205205
AggregateKind::Generator(did, substs, movability) => {
206206
tcx.mk_generator(did, substs, movability)

Diff for: compiler/rustc_mir_build/src/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
377377
})
378378
});
379379
let adt = Box::new(AggregateKind::Adt(
380-
adt_def,
380+
adt_def.did,
381381
variant_index,
382382
substs,
383383
user_ty,

Diff for: compiler/rustc_mir_transform/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
117117
match rvalue {
118118
Rvalue::Aggregate(box ref aggregate, _) => match aggregate {
119119
&AggregateKind::Array(..) | &AggregateKind::Tuple => {}
120-
&AggregateKind::Adt(ref def, ..) => {
121-
match self.tcx.layout_scalar_valid_range(def.did) {
120+
&AggregateKind::Adt(adt_did, ..) => {
121+
match self.tcx.layout_scalar_valid_range(adt_did) {
122122
(Bound::Unbounded, Bound::Unbounded) => {}
123123
_ => self.require_unsafe(
124124
UnsafetyViolationKind::General,

Diff for: compiler/rustc_mir_transform/src/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'tcx> TransformVisitor<'tcx> {
243243
val: Operand<'tcx>,
244244
source_info: SourceInfo,
245245
) -> impl Iterator<Item = Statement<'tcx>> {
246-
let kind = AggregateKind::Adt(self.state_adt_ref, idx, self.state_substs, None, None);
246+
let kind = AggregateKind::Adt(self.state_adt_ref.did, idx, self.state_substs, None, None);
247247
assert_eq!(self.state_adt_ref.variants[idx].fields.len(), 1);
248248
let ty = self
249249
.tcx

Diff for: compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
777777
adt_def.variants[variant_index].fields.iter().enumerate().map(|(idx, field_def)| {
778778
(Operand::Move(Place::from(Local::new(idx + 1))), field_def.ty(tcx, substs))
779779
}),
780-
AggregateKind::Adt(adt_def, variant_index, substs, None, None),
780+
AggregateKind::Adt(adt_def.did, variant_index, substs, None, None),
781781
source_info,
782782
tcx,
783783
)

0 commit comments

Comments
 (0)