Skip to content

Commit

Permalink
Improve AdtDef interning.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
nnethercote committed Mar 11, 2022
1 parent 5f4e067 commit ca5525d
Show file tree
Hide file tree
Showing 169 changed files with 702 additions and 687 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Expand Up @@ -138,7 +138,7 @@ impl BorrowExplanation {
let mut ty = local_decl.ty;
if local_decl.source_info.span.desugaring_kind() == Some(DesugaringKind::ForLoop) {
if let ty::Adt(adt, substs) = local_decl.ty.kind() {
if tcx.is_diagnostic_item(sym::Option, adt.did) {
if tcx.is_diagnostic_item(sym::Option, adt.did()) {
// in for loop desugaring, only look at the `Some(..)` inner type
ty = substs.type_at(0);
}
Expand All @@ -148,7 +148,7 @@ impl BorrowExplanation {
// If type is an ADT that implements Drop, then
// simplify output by reporting just the ADT name.
ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() => {
("`Drop` code", format!("type `{}`", tcx.def_path_str(adt.did)))
("`Drop` code", format!("type `{}`", tcx.def_path_str(adt.did())))
}

// Otherwise, just report the whole type (and use
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Expand Up @@ -370,7 +370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
ty::Adt(def, _) => {
let variant = if let Some(idx) = variant_index {
assert!(def.is_enum());
&def.variants[idx]
&def.variant(idx)
} else {
def.non_enum_variant()
};
Expand Down Expand Up @@ -701,7 +701,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
BorrowedContentSource::OverloadedDeref(ty) => ty
.ty_adt_def()
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
_ => None,
})
Expand Down Expand Up @@ -731,7 +731,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
}
BorrowedContentSource::OverloadedDeref(ty) => ty
.ty_adt_def()
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
_ => None,
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Expand Up @@ -388,7 +388,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
};
let ty = move_place.ty(self.body, self.infcx.tcx).ty;
let def_id = match *ty.kind() {
ty::Adt(self_def, _) => self_def.did,
ty::Adt(self_def, _) => self_def.did(),
ty::Foreign(def_id)
| ty::FnDef(def_id, _)
| ty::Closure(def_id, _)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Expand Up @@ -345,8 +345,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
ty::Adt(adt, substs) => {
let generic_arg = substs[param_index as usize];
let identity_substs =
InternalSubsts::identity_for_item(self.infcx.tcx, adt.did);
let base_ty = self.infcx.tcx.mk_adt(adt, identity_substs);
InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
let base_generic_arg = identity_substs[param_index as usize];
let adt_desc = adt.descr();

Expand Down Expand Up @@ -410,7 +410,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
"returns a closure that contains a reference to a captured variable, which then \
escapes the closure body"
}
ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did) => {
ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) => {
"returns an `async` block that contains a reference to a captured variable, which then \
escapes the closure body"
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Expand Up @@ -736,13 +736,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
}
ProjectionElem::Downcast(maybe_name, index) => match base_ty.kind() {
ty::Adt(adt_def, _substs) if adt_def.is_enum() => {
if index.as_usize() >= adt_def.variants.len() {
if index.as_usize() >= adt_def.variants().len() {
PlaceTy::from_ty(span_mirbug_and_err!(
self,
place,
"cast to variant #{:?} but enum only has {:?}",
index,
adt_def.variants.len()
adt_def.variants().len()
))
} else {
PlaceTy { ty: base_ty, variant_index: Some(index) }
Expand Down Expand Up @@ -816,7 +816,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {

let (variant, substs) = match base_ty {
PlaceTy { ty, variant_index: Some(variant_index) } => match *ty.kind() {
ty::Adt(adt_def, substs) => (&adt_def.variants[variant_index], substs),
ty::Adt(adt_def, substs) => (adt_def.variant(variant_index), substs),
ty::Generator(def_id, substs, _) => {
let mut variants = substs.as_generator().state_tys(def_id, tcx);
let Some(mut variant) = variants.nth(variant_index.into()) else {
Expand All @@ -835,7 +835,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
},
PlaceTy { ty, variant_index: None } => match *ty.kind() {
ty::Adt(adt_def, substs) if !adt_def.is_enum() => {
(&adt_def.variants[VariantIdx::new(0)], substs)
(adt_def.variant(VariantIdx::new(0)), substs)
}
ty::Closure(_, substs) => {
return match substs
Expand Down Expand Up @@ -1449,7 +1449,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
}
};
if variant_index.as_usize() >= adt.variants.len() {
if variant_index.as_usize() >= adt.variants().len() {
span_bug!(
stmt.source_info.span,
"bad set discriminant ({:?} = {:?}): value of of range",
Expand Down Expand Up @@ -1928,7 +1928,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
match *ak {
AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
let def = tcx.adt_def(adt_did);
let variant = &def.variants[variant_index];
let variant = &def.variant(variant_index);
let adj_field_index = active_field_index.unwrap_or(field_index);
if let Some(field) = variant.fields.get(adj_field_index) {
Ok(self.normalize(field.ty(tcx, substs), location))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/common.rs
Expand Up @@ -67,7 +67,7 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
pointer_ty(tcx)
}
}
ty::Adt(adt_def, _) if adt_def.repr.simd() => {
ty::Adt(adt_def, _) if adt_def.repr().simd() => {
let (element, count) = match &tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().abi
{
Abi::Vector { element, count } => (element.clone(), *count),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/unsize.rs
Expand Up @@ -127,7 +127,7 @@ pub(crate) fn coerce_unsized_into<'tcx>(
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
assert_eq!(def_a, def_b);

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

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

// Packed types ignore the alignment of their fields.
if let ty::Adt(def, _) = layout.ty.kind() {
if def.repr.packed() {
if def.repr().packed() {
unsized_align = sized_align;
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/value_and_place.rs
Expand Up @@ -24,7 +24,7 @@ fn codegen_field<'tcx>(
}
match field_layout.ty.kind() {
ty::Slice(..) | ty::Str | ty::Foreign(..) => simple(fx),
ty::Adt(def, _) if def.repr.packed() => {
ty::Adt(def, _) if def.repr().packed() => {
assert_eq!(layout.align.abi.bytes(), 1);
simple(fx)
}
Expand Down Expand Up @@ -816,7 +816,7 @@ pub(crate) fn assert_assignable<'tcx>(
// dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
}
(&ty::Adt(adt_def_a, substs_a), &ty::Adt(adt_def_b, substs_b))
if adt_def_a.did == adt_def_b.did =>
if adt_def_a.did() == adt_def_b.did() =>
{
let mut types_a = substs_a.types();
let mut types_b = substs_b.types();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/src/type_of.rs
Expand Up @@ -56,8 +56,8 @@ pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLa
if let (&ty::Adt(def, _), &Variants::Single { index }) =
(layout.ty.kind(), &layout.variants)
{
if def.is_enum() && !def.variants.is_empty() {
write!(&mut name, "::{}", def.variants[index].name).unwrap();
if def.is_enum() && !def.variants().is_empty() {
write!(&mut name, "::{}", def.variant(index).name).unwrap();
}
}
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Expand Up @@ -639,7 +639,7 @@ pub fn type_metadata<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll
AdtKind::Struct => prepare_struct_metadata(cx, t, unique_type_id).finalize(cx),
AdtKind::Union => prepare_union_metadata(cx, t, unique_type_id).finalize(cx),
AdtKind::Enum => {
prepare_enum_metadata(cx, t, def.did, unique_type_id, vec![]).finalize(cx)
prepare_enum_metadata(cx, t, def.did(), unique_type_id, vec![]).finalize(cx)
}
},
ty::Tuple(tys) => {
Expand Down Expand Up @@ -1207,7 +1207,7 @@ fn prepare_struct_metadata<'ll, 'tcx>(
let struct_name = compute_debuginfo_type_name(cx.tcx, struct_type, false);

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

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

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

Expand Down Expand Up @@ -1466,7 +1466,7 @@ impl<'ll, 'tcx> EnumMemberDescriptionFactory<'ll, 'tcx> {
};

let variant_info_for = |index: VariantIdx| match *self.enum_type.kind() {
ty::Adt(adt, _) => VariantInfo::Adt(&adt.variants[index], index),
ty::Adt(adt, _) => VariantInfo::Adt(&adt.variant(index), index),
ty::Generator(def_id, _, _) => {
let (generator_layout, generator_saved_local_names) =
generator_variant_info_data.as_ref().unwrap();
Expand All @@ -1490,7 +1490,7 @@ impl<'ll, 'tcx> EnumMemberDescriptionFactory<'ll, 'tcx> {
match self.layout.variants {
Variants::Single { index } => {
if let ty::Adt(adt, _) = self.enum_type.kind() {
if adt.variants.is_empty() {
if adt.variants().is_empty() {
return vec![];
}
}
Expand Down Expand Up @@ -1940,7 +1940,7 @@ fn prepare_enum_metadata<'ll, 'tcx>(

let discriminant_type_metadata = |discr: Primitive| {
let enumerators_metadata: Vec<_> = match enum_type.kind() {
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), &def.variants)
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), def.variants())
.map(|((_, discr), v)| {
let name = v.name.as_str();
let is_unsigned = match discr.ty.kind() {
Expand Down Expand Up @@ -2311,7 +2311,7 @@ fn set_members_of_composite_type<'ll, 'tcx>(
fn compute_type_parameters<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIArray {
if let ty::Adt(def, substs) = *ty.kind() {
if substs.types().next().is_some() {
let generics = cx.tcx.generics_of(def.did);
let generics = cx.tcx.generics_of(def.did());
let names = get_parameter_names(cx, generics);
let template_params: Vec<_> = iter::zip(substs, names)
.filter_map(|(kind, name)| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Expand Up @@ -524,7 +524,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
{
Some(type_metadata(cx, impl_self_ty))
} else {
Some(namespace::item_namespace(cx, def.did))
Some(namespace::item_namespace(cx, def.did()))
}
}
_ => None,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/type_of.rs
Expand Up @@ -53,8 +53,8 @@ fn uncached_llvm_type<'a, 'tcx>(
if let (&ty::Adt(def, _), &Variants::Single { index }) =
(layout.ty.kind(), &layout.variants)
{
if def.is_enum() && !def.variants.is_empty() {
write!(&mut name, "::{}", def.variants[index].name).unwrap();
if def.is_enum() && !def.variants().is_empty() {
write!(&mut name, "::{}", def.variant(index).name).unwrap();
}
}
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Expand Up @@ -255,7 +255,7 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
assert_eq!(def_a, def_b);

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

Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Expand Up @@ -74,7 +74,7 @@ fn push_debuginfo_type_name<'tcx>(
if def.is_enum() && cpp_like_debuginfo {
msvc_enum_fallback(tcx, t, def, substs, output, visited);
} else {
push_item_name(tcx, def.did, qualified, output);
push_item_name(tcx, def.did(), qualified, output);
push_generic_params_internal(tcx, substs, output, visited);
}
}
Expand Down Expand Up @@ -405,15 +405,15 @@ fn push_debuginfo_type_name<'tcx>(
fn msvc_enum_fallback<'tcx>(
tcx: TyCtxt<'tcx>,
ty: Ty<'tcx>,
def: &AdtDef,
def: AdtDef<'tcx>,
substs: SubstsRef<'tcx>,
output: &mut String,
visited: &mut FxHashSet<Ty<'tcx>>,
) {
let layout = tcx.layout_of(tcx.param_env(def.did).and(ty)).expect("layout error");
let layout = tcx.layout_of(tcx.param_env(def.did()).and(ty)).expect("layout error");

output.push_str("enum$<");
push_item_name(tcx, def.did, true, output);
push_item_name(tcx, def.did(), true, output);
push_generic_params_internal(tcx, substs, output, visited);

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

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

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

output.push_str(&format!(", {}", variant));
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/glue.rs
Expand Up @@ -74,7 +74,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

// Packed types ignore the alignment of their fields.
if let ty::Adt(def, _) = t.kind() {
if def.repr.packed() {
if def.repr().packed() {
unsized_align = sized_align;
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/place.rs
Expand Up @@ -149,7 +149,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
_ if !field.is_unsized() => return simple(),
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
ty::Adt(def, _) => {
if def.repr.packed() {
if def.repr().packed() {
// FIXME(eddyb) generalize the adjustment when we
// start supporting packing to larger alignments.
assert_eq!(self.layout.align.abi.bytes(), 1);
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Expand Up @@ -105,13 +105,13 @@ fn const_to_valtree_inner<'tcx>(
ty::Array(_, len) => branches(usize::try_from(len.eval_usize(ecx.tcx.tcx, ecx.param_env)).unwrap(), None),

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

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

branches(def.variants[variant].fields.len(), def.is_enum().then_some(variant))
branches(def.variant(variant).fields.len(), def.is_enum().then_some(variant))
}

ty::Never
Expand Down Expand Up @@ -150,11 +150,11 @@ pub(crate) fn try_destructure_const<'tcx>(
// Checks if we have any variants, to avoid downcasting to a non-existing variant (when
// there are no variants `read_discriminant` successfully returns a non-existing variant
// index).
ty::Adt(def, _) if def.variants.is_empty() => throw_ub!(Unreachable),
ty::Adt(def, _) if def.variants().is_empty() => throw_ub!(Unreachable),
ty::Adt(def, _) => {
let variant = ecx.read_discriminant(&op)?.1;
let down = ecx.operand_downcast(&op, variant)?;
(def.variants[variant].fields.len(), Some(variant), down)
(def.variant(variant).fields.len(), Some(variant), down)
}
ty::Tuple(substs) => (substs.len(), None, op),
_ => bug!("cannot destructure constant {:?}", val),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/intern.rs
Expand Up @@ -174,7 +174,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
}

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

0 comments on commit ca5525d

Please sign in to comment.