Skip to content

Commit ca5525d

Browse files
committed
Improve AdtDef interning.
This commit makes `AdtDef` use `Interned`. Much the commit is tedious changes to introduce getter functions. The interesting changes are in `compiler/rustc_middle/src/ty/adt.rs`.
1 parent 5f4e067 commit ca5525d

File tree

169 files changed

+702
-687
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+702
-687
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl BorrowExplanation {
138138
let mut ty = local_decl.ty;
139139
if local_decl.source_info.span.desugaring_kind() == Some(DesugaringKind::ForLoop) {
140140
if let ty::Adt(adt, substs) = local_decl.ty.kind() {
141-
if tcx.is_diagnostic_item(sym::Option, adt.did) {
141+
if tcx.is_diagnostic_item(sym::Option, adt.did()) {
142142
// in for loop desugaring, only look at the `Some(..)` inner type
143143
ty = substs.type_at(0);
144144
}
@@ -148,7 +148,7 @@ impl BorrowExplanation {
148148
// If type is an ADT that implements Drop, then
149149
// simplify output by reporting just the ADT name.
150150
ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() => {
151-
("`Drop` code", format!("type `{}`", tcx.def_path_str(adt.did)))
151+
("`Drop` code", format!("type `{}`", tcx.def_path_str(adt.did())))
152152
}
153153

154154
// Otherwise, just report the whole type (and use

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
370370
ty::Adt(def, _) => {
371371
let variant = if let Some(idx) = variant_index {
372372
assert!(def.is_enum());
373-
&def.variants[idx]
373+
&def.variant(idx)
374374
} else {
375375
def.non_enum_variant()
376376
};
@@ -701,7 +701,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
701701
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
702702
BorrowedContentSource::OverloadedDeref(ty) => ty
703703
.ty_adt_def()
704-
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
704+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
705705
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
706706
_ => None,
707707
})
@@ -731,7 +731,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
731731
}
732732
BorrowedContentSource::OverloadedDeref(ty) => ty
733733
.ty_adt_def()
734-
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
734+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
735735
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
736736
_ => None,
737737
})

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
388388
};
389389
let ty = move_place.ty(self.body, self.infcx.tcx).ty;
390390
let def_id = match *ty.kind() {
391-
ty::Adt(self_def, _) => self_def.did,
391+
ty::Adt(self_def, _) => self_def.did(),
392392
ty::Foreign(def_id)
393393
| ty::FnDef(def_id, _)
394394
| ty::Closure(def_id, _)

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
345345
ty::Adt(adt, substs) => {
346346
let generic_arg = substs[param_index as usize];
347347
let identity_substs =
348-
InternalSubsts::identity_for_item(self.infcx.tcx, adt.did);
349-
let base_ty = self.infcx.tcx.mk_adt(adt, identity_substs);
348+
InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
349+
let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
350350
let base_generic_arg = identity_substs[param_index as usize];
351351
let adt_desc = adt.descr();
352352

@@ -410,7 +410,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
410410
"returns a closure that contains a reference to a captured variable, which then \
411411
escapes the closure body"
412412
}
413-
ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did) => {
413+
ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) => {
414414
"returns an `async` block that contains a reference to a captured variable, which then \
415415
escapes the closure body"
416416
}

compiler/rustc_borrowck/src/type_check/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
736736
}
737737
ProjectionElem::Downcast(maybe_name, index) => match base_ty.kind() {
738738
ty::Adt(adt_def, _substs) if adt_def.is_enum() => {
739-
if index.as_usize() >= adt_def.variants.len() {
739+
if index.as_usize() >= adt_def.variants().len() {
740740
PlaceTy::from_ty(span_mirbug_and_err!(
741741
self,
742742
place,
743743
"cast to variant #{:?} but enum only has {:?}",
744744
index,
745-
adt_def.variants.len()
745+
adt_def.variants().len()
746746
))
747747
} else {
748748
PlaceTy { ty: base_ty, variant_index: Some(index) }
@@ -816,7 +816,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
816816

817817
let (variant, substs) = match base_ty {
818818
PlaceTy { ty, variant_index: Some(variant_index) } => match *ty.kind() {
819-
ty::Adt(adt_def, substs) => (&adt_def.variants[variant_index], substs),
819+
ty::Adt(adt_def, substs) => (adt_def.variant(variant_index), substs),
820820
ty::Generator(def_id, substs, _) => {
821821
let mut variants = substs.as_generator().state_tys(def_id, tcx);
822822
let Some(mut variant) = variants.nth(variant_index.into()) else {
@@ -835,7 +835,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
835835
},
836836
PlaceTy { ty, variant_index: None } => match *ty.kind() {
837837
ty::Adt(adt_def, substs) if !adt_def.is_enum() => {
838-
(&adt_def.variants[VariantIdx::new(0)], substs)
838+
(adt_def.variant(VariantIdx::new(0)), substs)
839839
}
840840
ty::Closure(_, substs) => {
841841
return match substs
@@ -1449,7 +1449,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14491449
);
14501450
}
14511451
};
1452-
if variant_index.as_usize() >= adt.variants.len() {
1452+
if variant_index.as_usize() >= adt.variants().len() {
14531453
span_bug!(
14541454
stmt.source_info.span,
14551455
"bad set discriminant ({:?} = {:?}): value of of range",
@@ -1928,7 +1928,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19281928
match *ak {
19291929
AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
19301930
let def = tcx.adt_def(adt_did);
1931-
let variant = &def.variants[variant_index];
1931+
let variant = &def.variant(variant_index);
19321932
let adj_field_index = active_field_index.unwrap_or(field_index);
19331933
if let Some(field) = variant.fields.get(adj_field_index) {
19341934
Ok(self.normalize(field.ty(tcx, substs), location))

compiler/rustc_codegen_cranelift/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
6767
pointer_ty(tcx)
6868
}
6969
}
70-
ty::Adt(adt_def, _) if adt_def.repr.simd() => {
70+
ty::Adt(adt_def, _) if adt_def.repr().simd() => {
7171
let (element, count) = match &tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().abi
7272
{
7373
Abi::Vector { element, count } => (element.clone(), *count),

compiler/rustc_codegen_cranelift/src/unsize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub(crate) fn coerce_unsized_into<'tcx>(
127127
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
128128
assert_eq!(def_a, def_b);
129129

130-
for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
130+
for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
131131
let src_f = src.value_field(fx, mir::Field::new(i));
132132
let dst_f = dst.place_field(fx, mir::Field::new(i));
133133

@@ -200,7 +200,7 @@ pub(crate) fn size_and_align_of_dst<'tcx>(
200200

201201
// Packed types ignore the alignment of their fields.
202202
if let ty::Adt(def, _) = layout.ty.kind() {
203-
if def.repr.packed() {
203+
if def.repr().packed() {
204204
unsized_align = sized_align;
205205
}
206206
}

compiler/rustc_codegen_cranelift/src/value_and_place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn codegen_field<'tcx>(
2424
}
2525
match field_layout.ty.kind() {
2626
ty::Slice(..) | ty::Str | ty::Foreign(..) => simple(fx),
27-
ty::Adt(def, _) if def.repr.packed() => {
27+
ty::Adt(def, _) if def.repr().packed() => {
2828
assert_eq!(layout.align.abi.bytes(), 1);
2929
simple(fx)
3030
}
@@ -816,7 +816,7 @@ pub(crate) fn assert_assignable<'tcx>(
816816
// dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
817817
}
818818
(&ty::Adt(adt_def_a, substs_a), &ty::Adt(adt_def_b, substs_b))
819-
if adt_def_a.did == adt_def_b.did =>
819+
if adt_def_a.did() == adt_def_b.did() =>
820820
{
821821
let mut types_a = substs_a.types();
822822
let mut types_b = substs_b.types();

compiler/rustc_codegen_gcc/src/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLa
5656
if let (&ty::Adt(def, _), &Variants::Single { index }) =
5757
(layout.ty.kind(), &layout.variants)
5858
{
59-
if def.is_enum() && !def.variants.is_empty() {
60-
write!(&mut name, "::{}", def.variants[index].name).unwrap();
59+
if def.is_enum() && !def.variants().is_empty() {
60+
write!(&mut name, "::{}", def.variant(index).name).unwrap();
6161
}
6262
}
6363
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub fn type_metadata<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll
639639
AdtKind::Struct => prepare_struct_metadata(cx, t, unique_type_id).finalize(cx),
640640
AdtKind::Union => prepare_union_metadata(cx, t, unique_type_id).finalize(cx),
641641
AdtKind::Enum => {
642-
prepare_enum_metadata(cx, t, def.did, unique_type_id, vec![]).finalize(cx)
642+
prepare_enum_metadata(cx, t, def.did(), unique_type_id, vec![]).finalize(cx)
643643
}
644644
},
645645
ty::Tuple(tys) => {
@@ -1207,7 +1207,7 @@ fn prepare_struct_metadata<'ll, 'tcx>(
12071207
let struct_name = compute_debuginfo_type_name(cx.tcx, struct_type, false);
12081208

12091209
let (struct_def_id, variant) = match struct_type.kind() {
1210-
ty::Adt(def, _) => (def.did, def.non_enum_variant()),
1210+
ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
12111211
_ => bug!("prepare_struct_metadata on a non-ADT"),
12121212
};
12131213

@@ -1384,7 +1384,7 @@ fn prepare_union_metadata<'ll, 'tcx>(
13841384
let union_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
13851385

13861386
let (union_def_id, variant) = match union_type.kind() {
1387-
ty::Adt(def, _) => (def.did, def.non_enum_variant()),
1387+
ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
13881388
_ => bug!("prepare_union_metadata on a non-ADT"),
13891389
};
13901390

@@ -1466,7 +1466,7 @@ impl<'ll, 'tcx> EnumMemberDescriptionFactory<'ll, 'tcx> {
14661466
};
14671467

14681468
let variant_info_for = |index: VariantIdx| match *self.enum_type.kind() {
1469-
ty::Adt(adt, _) => VariantInfo::Adt(&adt.variants[index], index),
1469+
ty::Adt(adt, _) => VariantInfo::Adt(&adt.variant(index), index),
14701470
ty::Generator(def_id, _, _) => {
14711471
let (generator_layout, generator_saved_local_names) =
14721472
generator_variant_info_data.as_ref().unwrap();
@@ -1490,7 +1490,7 @@ impl<'ll, 'tcx> EnumMemberDescriptionFactory<'ll, 'tcx> {
14901490
match self.layout.variants {
14911491
Variants::Single { index } => {
14921492
if let ty::Adt(adt, _) = self.enum_type.kind() {
1493-
if adt.variants.is_empty() {
1493+
if adt.variants().is_empty() {
14941494
return vec![];
14951495
}
14961496
}
@@ -1940,7 +1940,7 @@ fn prepare_enum_metadata<'ll, 'tcx>(
19401940

19411941
let discriminant_type_metadata = |discr: Primitive| {
19421942
let enumerators_metadata: Vec<_> = match enum_type.kind() {
1943-
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), &def.variants)
1943+
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), def.variants())
19441944
.map(|((_, discr), v)| {
19451945
let name = v.name.as_str();
19461946
let is_unsigned = match discr.ty.kind() {
@@ -2311,7 +2311,7 @@ fn set_members_of_composite_type<'ll, 'tcx>(
23112311
fn compute_type_parameters<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIArray {
23122312
if let ty::Adt(def, substs) = *ty.kind() {
23132313
if substs.types().next().is_some() {
2314-
let generics = cx.tcx.generics_of(def.did);
2314+
let generics = cx.tcx.generics_of(def.did());
23152315
let names = get_parameter_names(cx, generics);
23162316
let template_params: Vec<_> = iter::zip(substs, names)
23172317
.filter_map(|(kind, name)| {

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
524524
{
525525
Some(type_metadata(cx, impl_self_ty))
526526
} else {
527-
Some(namespace::item_namespace(cx, def.did))
527+
Some(namespace::item_namespace(cx, def.did()))
528528
}
529529
}
530530
_ => None,

compiler/rustc_codegen_llvm/src/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ fn uncached_llvm_type<'a, 'tcx>(
5353
if let (&ty::Adt(def, _), &Variants::Single { index }) =
5454
(layout.ty.kind(), &layout.variants)
5555
{
56-
if def.is_enum() && !def.variants.is_empty() {
57-
write!(&mut name, "::{}", def.variants[index].name).unwrap();
56+
if def.is_enum() && !def.variants().is_empty() {
57+
write!(&mut name, "::{}", def.variant(index).name).unwrap();
5858
}
5959
}
6060
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
255255
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
256256
assert_eq!(def_a, def_b);
257257

258-
for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
258+
for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
259259
let src_f = src.project_field(bx, i);
260260
let dst_f = dst.project_field(bx, i);
261261

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn push_debuginfo_type_name<'tcx>(
7474
if def.is_enum() && cpp_like_debuginfo {
7575
msvc_enum_fallback(tcx, t, def, substs, output, visited);
7676
} else {
77-
push_item_name(tcx, def.did, qualified, output);
77+
push_item_name(tcx, def.did(), qualified, output);
7878
push_generic_params_internal(tcx, substs, output, visited);
7979
}
8080
}
@@ -405,15 +405,15 @@ fn push_debuginfo_type_name<'tcx>(
405405
fn msvc_enum_fallback<'tcx>(
406406
tcx: TyCtxt<'tcx>,
407407
ty: Ty<'tcx>,
408-
def: &AdtDef,
408+
def: AdtDef<'tcx>,
409409
substs: SubstsRef<'tcx>,
410410
output: &mut String,
411411
visited: &mut FxHashSet<Ty<'tcx>>,
412412
) {
413-
let layout = tcx.layout_of(tcx.param_env(def.did).and(ty)).expect("layout error");
413+
let layout = tcx.layout_of(tcx.param_env(def.did()).and(ty)).expect("layout error");
414414

415415
output.push_str("enum$<");
416-
push_item_name(tcx, def.did, true, output);
416+
push_item_name(tcx, def.did(), true, output);
417417
push_generic_params_internal(tcx, substs, output, visited);
418418

419419
if let Variants::Multiple {
@@ -435,14 +435,14 @@ fn push_debuginfo_type_name<'tcx>(
435435
let max = dataful_discriminant_range.end;
436436
let max = tag.value.size(&tcx).truncate(max);
437437

438-
let dataful_variant_name = def.variants[*dataful_variant].name.as_str();
438+
let dataful_variant_name = def.variant(*dataful_variant).name.as_str();
439439

440440
output.push_str(&format!(", {}, {}, {}", min, max, dataful_variant_name));
441441
} else if let Variants::Single { index: variant_idx } = &layout.variants {
442442
// Uninhabited enums can't be constructed and should never need to be visualized so
443443
// skip this step for them.
444-
if def.variants.len() != 0 {
445-
let variant = def.variants[*variant_idx].name.as_str();
444+
if def.variants().len() != 0 {
445+
let variant = def.variant(*variant_idx).name.as_str();
446446

447447
output.push_str(&format!(", {}", variant));
448448
}

compiler/rustc_codegen_ssa/src/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
7474

7575
// Packed types ignore the alignment of their fields.
7676
if let ty::Adt(def, _) = t.kind() {
77-
if def.repr.packed() {
77+
if def.repr().packed() {
7878
unsized_align = sized_align;
7979
}
8080
}

compiler/rustc_codegen_ssa/src/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
149149
_ if !field.is_unsized() => return simple(),
150150
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
151151
ty::Adt(def, _) => {
152-
if def.repr.packed() {
152+
if def.repr().packed() {
153153
// FIXME(eddyb) generalize the adjustment when we
154154
// start supporting packing to larger alignments.
155155
assert_eq!(self.layout.align.abi.bytes(), 1);

compiler/rustc_const_eval/src/const_eval/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ fn const_to_valtree_inner<'tcx>(
105105
ty::Array(_, len) => branches(usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),
106106

107107
ty::Adt(def, _) => {
108-
if def.variants.is_empty() {
108+
if def.variants().is_empty() {
109109
bug!("uninhabited types should have errored and never gotten converted to valtree")
110110
}
111111

112112
let variant = ecx.read_discriminant(&place.into()).unwrap().1;
113113

114-
branches(def.variants[variant].fields.len(), def.is_enum().then_some(variant))
114+
branches(def.variant(variant).fields.len(), def.is_enum().then_some(variant))
115115
}
116116

117117
ty::Never
@@ -150,11 +150,11 @@ pub(crate) fn try_destructure_const<'tcx>(
150150
// Checks if we have any variants, to avoid downcasting to a non-existing variant (when
151151
// there are no variants `read_discriminant` successfully returns a non-existing variant
152152
// index).
153-
ty::Adt(def, _) if def.variants.is_empty() => throw_ub!(Unreachable),
153+
ty::Adt(def, _) if def.variants().is_empty() => throw_ub!(Unreachable),
154154
ty::Adt(def, _) => {
155155
let variant = ecx.read_discriminant(&op)?.1;
156156
let down = ecx.operand_downcast(&op, variant)?;
157-
(def.variants[variant].fields.len(), Some(variant), down)
157+
(def.variant(variant).fields.len(), Some(variant), down)
158158
}
159159
ty::Tuple(substs) => (substs.len(), None, op),
160160
_ => bug!("cannot destructure constant {:?}", val),

compiler/rustc_const_eval/src/interpret/intern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
174174
}
175175

176176
if let Some(def) = mplace.layout.ty.ty_adt_def() {
177-
if Some(def.did) == self.ecx.tcx.lang_items().unsafe_cell_type() {
177+
if Some(def.did()) == self.ecx.tcx.lang_items().unsafe_cell_type() {
178178
// We are crossing over an `UnsafeCell`, we can mutate again. This means that
179179
// References we encounter inside here are interned as pointing to mutable
180180
// allocations.

0 commit comments

Comments
 (0)