Skip to content

Commit

Permalink
Store a Symbol instead of an Ident in VariantDef/FieldDef
Browse files Browse the repository at this point in the history
The field is also renamed from `ident` to `name. In most cases,
we don't actually need the `Span`. A new `ident` method is added
to `VariantDef` and `FieldDef`, which constructs the full `Ident`
using `tcx.def_ident_span()`. This method is used in the cases
where we actually need an `Ident`.

This makes incremental compilation properly track changes
to the `Span`, without all of the invalidations caused by storing
a `Span` directly via an `Ident`.
  • Loading branch information
Aaron1011 committed Jan 11, 2022
1 parent e4b1d58 commit 450ef86
Show file tree
Hide file tree
Showing 38 changed files with 120 additions and 107 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} else {
def.non_enum_variant()
};
variant.fields[field.index()].ident.to_string()
variant.fields[field.index()].name.to_string()
}
ty::Tuple(_) => field.index().to_string(),
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'tcx> DebugContext<'tcx> {

field_entry.set(
gimli::DW_AT_name,
AttributeValue::String(field_def.ident.as_str().to_string().into_bytes()),
AttributeValue::String(field_def.name.as_str().to_string().into_bytes()),
);
field_entry.set(
gimli::DW_AT_data_member_location,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLa
(layout.ty.kind(), &layout.variants)
{
if def.is_enum() && !def.variants.is_empty() {
write!(&mut name, "::{}", def.variants[index].ident).unwrap();
write!(&mut name, "::{}", def.variants[index].name).unwrap();
}
}
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {
let name = if self.variant.ctor_kind == CtorKind::Fn {
format!("__{}", i)
} else {
f.ident.to_string()
f.name.to_string()
};
let field = layout.field(cx, i);
MemberDescription {
Expand Down Expand Up @@ -1480,7 +1480,7 @@ impl<'tcx> UnionMemberDescriptionFactory<'tcx> {
.map(|(i, f)| {
let field = self.layout.field(cx, i);
MemberDescription {
name: f.ident.to_string(),
name: f.name.to_string(),
type_metadata: type_metadata(cx, field.ty, self.span),
offset: Size::ZERO,
size: field.size,
Expand Down Expand Up @@ -1950,7 +1950,7 @@ enum VariantInfo<'a, 'tcx> {
impl<'tcx> VariantInfo<'_, 'tcx> {
fn map_struct_name<R>(&self, f: impl FnOnce(&str) -> R) -> R {
match self {
VariantInfo::Adt(variant) => f(variant.ident.as_str()),
VariantInfo::Adt(variant) => f(variant.name.as_str()),
VariantInfo::Generator { variant_index, .. } => {
f(&GeneratorSubsts::variant_name(*variant_index))
}
Expand All @@ -1959,7 +1959,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> {

fn variant_name(&self) -> String {
match self {
VariantInfo::Adt(variant) => variant.ident.to_string(),
VariantInfo::Adt(variant) => variant.name.to_string(),
VariantInfo::Generator { variant_index, .. } => {
// Since GDB currently prints out the raw discriminant along
// with every variant, make each variant name be just the value
Expand All @@ -1973,7 +1973,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> {
fn field_name(&self, i: usize) -> String {
let field_name = match *self {
VariantInfo::Adt(variant) if variant.ctor_kind != CtorKind::Fn => {
Some(variant.fields[i].ident.name)
Some(variant.fields[i].name)
}
VariantInfo::Generator {
generator_layout,
Expand Down Expand Up @@ -2063,7 +2063,7 @@ fn prepare_enum_metadata<'ll, 'tcx>(
let enumerators_metadata: Vec<_> = match enum_type.kind() {
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), &def.variants)
.map(|((_, discr), v)| {
let name = v.ident.as_str();
let name = v.name.as_str();
let is_unsigned = match discr.ty.kind() {
ty::Int(_) => false,
ty::Uint(_) => true,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn uncached_llvm_type<'a, 'tcx>(
(layout.ty.kind(), &layout.variants)
{
if def.is_enum() && !def.variants.is_empty() {
write!(&mut name, "::{}", def.variants[index].ident).unwrap();
write!(&mut name, "::{}", def.variants[index].name).unwrap();
}
}
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,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].ident.as_str();
let dataful_variant_name = def.variants[*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].ident.as_str();
let variant = def.variants[*variant_idx].name.as_str();

output.push_str(&format!(", {}", variant));
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
match layout.variants {
Variants::Single { index } => {
// Inside a variant
PathElem::Field(def.variants[index].fields[field].ident.name)
PathElem::Field(def.variants[index].fields[field].name)
}
Variants::Multiple { .. } => bug!("we handled variants above"),
}
}

// other ADTs
ty::Adt(def, _) => PathElem::Field(def.non_enum_variant().fields[field].ident.name),
ty::Adt(def, _) => PathElem::Field(def.non_enum_variant().fields[field].name),

// arrays/slices
ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
Expand Down Expand Up @@ -726,7 +726,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
new_op: &OpTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
let name = match old_op.layout.ty.kind() {
ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].ident.name),
ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].name),
// Generators also have variants
ty::Generator(..) => PathElem::GeneratorState(variant_id),
_ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.fields
.iter()
.filter(|field| field.vis.is_accessible_from(field.did, self.tcx))
.map(|field| (field.ident.name, field.ty(self.tcx, expected_substs)))
.map(|field| (field.name, field.ty(self.tcx, expected_substs)))
.find(|(_, ty)| same_type_modulo_infer(ty, exp_found.found))
{
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let ctor_did = data.ctor.map(|index| self.local_def_id(index));

ty::VariantDef::new(
self.item_ident(index, sess),
self.item_ident(index, sess).name,
variant_did,
ctor_did,
data.discr,
Expand All @@ -874,7 +874,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.decode(self)
.map(|index| ty::FieldDef {
did: self.local_def_id(index),
ident: self.item_ident(index, sess),
name: self.item_ident(index, sess).name,
vis: self.get_visibility(index),
})
.collect(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
assert!(f.did.is_local());
f.did.index
}));
self.encode_ident_span(def_id, variant.ident);
self.encode_ident_span(def_id, variant.ident(tcx));
self.encode_item_type(def_id);
if variant.ctor_kind == CtorKind::Fn {
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
Expand Down Expand Up @@ -1138,7 +1138,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_field({:?})", def_id);

record!(self.tables.kind[def_id] <- EntryKind::Field);
self.encode_ident_span(def_id, field.ident);
self.encode_ident_span(def_id, field.ident(self.tcx));
self.encode_item_type(def_id);
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
CtorKind::Fictive => {
let mut struct_fmt = fmt.debug_struct(&name);
for (field, place) in iter::zip(&variant_def.fields, places) {
struct_fmt.field(field.ident.as_str(), place);
struct_fmt.field(field.name.as_str(), place);
}
struct_fmt.finish()
}
Expand Down Expand Up @@ -2785,7 +2785,7 @@ impl UserTypeProjection {
field: Field,
) -> Self {
self.projs.push(ProjectionElem::Downcast(
Some(adt_def.variants[variant_index].ident.name),
Some(adt_def.variants[variant_index].name),
variant_index,
));
self.projs.push(ProjectionElem::Field(field, ()));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
};

if let Some(variant) = variant {
write!(f, "{}", variant.ident)?;
write!(f, "{}", variant.name)?;

// Only for Adt we can have `S {...}`,
// which we handle separately here.
Expand All @@ -738,7 +738,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
if let PatKind::Wild = *p.pattern.kind {
continue;
}
let name = variant.fields[p.field.index()].ident;
let name = variant.fields[p.field.index()].name;
write!(f, "{}{}: {}", start_or_comma(), name, p.pattern)?;
printed += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'tcx> CapturedPlace<'tcx> {
write!(
&mut symbol,
"__{}",
def.variants[variant].fields[idx as usize].ident.name.as_str(),
def.variants[variant].fields[idx as usize].name.as_str(),
)
.unwrap();
}
Expand Down Expand Up @@ -344,7 +344,7 @@ pub fn place_to_string_for_capture<'tcx>(tcx: TyCtxt<'tcx>, place: &HirPlace<'tc
curr_string = format!(
"{}.{}",
curr_string,
def.variants[variant].fields[idx as usize].ident.name.as_str()
def.variants[variant].fields[idx as usize].name.as_str()
);
}
ty::Tuple(_) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,7 +2452,7 @@ impl<'tcx> TyCtxt<'tcx> {
) -> Place<'tcx> {
self.mk_place_elem(
place,
PlaceElem::Downcast(Some(adt_def.variants[variant_index].ident.name), variant_index),
PlaceElem::Downcast(Some(adt_def.variants[variant_index].name), variant_index),
)
}

Expand Down
15 changes: 7 additions & 8 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
use rustc_session::{config::OptLevel, DataTypeKind, FieldInfo, SizeKind, VariantInfo};
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::call::{
ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, Conv, FnAbi, PassMode, Reg, RegKind,
Expand Down Expand Up @@ -1810,7 +1810,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
let adt_kind = adt_def.adt_kind();
let adt_packed = adt_def.repr.pack.is_some();

let build_variant_info = |n: Option<Ident>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
let build_variant_info = |n: Option<Symbol>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
let mut min_size = Size::ZERO;
let field_info: Vec<_> = flds
.iter()
Expand Down Expand Up @@ -1845,15 +1845,15 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
if !adt_def.variants.is_empty() && layout.fields != FieldsShape::Primitive {
debug!(
"print-type-size `{:#?}` variant {}",
layout, adt_def.variants[index].ident
layout, adt_def.variants[index].name
);
let variant_def = &adt_def.variants[index];
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.ident.name).collect();
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
record(
adt_kind.into(),
adt_packed,
None,
vec![build_variant_info(Some(variant_def.ident), &fields, layout)],
vec![build_variant_info(Some(variant_def.name), &fields, layout)],
);
} else {
// (This case arises for *empty* enums; so give it
Expand All @@ -1872,10 +1872,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
.variants
.iter_enumerated()
.map(|(i, variant_def)| {
let fields: Vec<_> =
variant_def.fields.iter().map(|f| f.ident.name).collect();
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
build_variant_info(
Some(variant_def.ident),
Some(variant_def.name),
&fields,
layout.for_variant(self, i),
)
Expand Down
29 changes: 20 additions & 9 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,7 @@ pub struct VariantDef {
/// If this variant is a struct variant, then this is `None`.
pub ctor_def_id: Option<DefId>,
/// Variant or struct name.
#[stable_hasher(project(name))]
pub ident: Ident,
pub name: Symbol,
/// Discriminant of this variant.
pub discr: VariantDiscr,
/// Fields of this variant.
Expand Down Expand Up @@ -1534,7 +1533,7 @@ impl VariantDef {
/// If someone speeds up attribute loading to not be a performance concern, they can
/// remove this hack and use the constructor `DefId` everywhere.
pub fn new(
ident: Ident,
name: Symbol,
variant_did: Option<DefId>,
ctor_def_id: Option<DefId>,
discr: VariantDiscr,
Expand All @@ -1546,9 +1545,9 @@ impl VariantDef {
is_field_list_non_exhaustive: bool,
) -> Self {
debug!(
"VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
"VariantDef::new(name = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
fields = {:?}, ctor_kind = {:?}, adt_kind = {:?}, parent_did = {:?})",
ident, variant_did, ctor_def_id, discr, fields, ctor_kind, adt_kind, parent_did,
name, variant_did, ctor_def_id, discr, fields, ctor_kind, adt_kind, parent_did,
);

let mut flags = VariantFlags::NO_VARIANT_FLAGS;
Expand All @@ -1563,7 +1562,7 @@ impl VariantDef {
VariantDef {
def_id: variant_did.unwrap_or(parent_did),
ctor_def_id,
ident,
name,
discr,
fields,
ctor_kind,
Expand All @@ -1582,6 +1581,11 @@ impl VariantDef {
pub fn is_recovered(&self) -> bool {
self.flags.intersects(VariantFlags::IS_RECOVERED)
}

/// Computes the `Ident` of this variant by looking up the `Span`
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
Expand All @@ -1600,8 +1604,7 @@ pub enum VariantDiscr {
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
pub struct FieldDef {
pub did: DefId,
#[stable_hasher(project(name))]
pub ident: Ident,
pub name: Symbol,
pub vis: Visibility,
}

Expand Down Expand Up @@ -1776,6 +1779,11 @@ impl<'tcx> FieldDef {
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
tcx.type_of(self.did).subst(tcx, subst)
}

/// Computes the `Ident` of this variant by looking up the `Span`
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
Ident::new(self.name, tcx.def_ident_span(self.did).unwrap())
}
}

pub type Attributes<'tcx> = &'tcx [ast::Attribute];
Expand Down Expand Up @@ -1892,7 +1900,10 @@ impl<'tcx> TyCtxt<'tcx> {
}

pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
variant.fields.iter().position(|field| self.hygienic_eq(ident, field.ident, variant.def_id))
variant
.fields
.iter()
.position(|field| self.hygienic_eq(ident, field.ident(self), variant.def_id))
}

/// Returns `true` if the impls are the same polarity and the trait either
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ pub trait PrettyPrinter<'tcx>:
if !first {
p!(", ");
}
p!(write("{}: ", field_def.ident), print(field));
p!(write("{}: ", field_def.name), print(field));
first = false;
}
p!(" }}");
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,7 @@ impl<'tcx> PlaceBuilder<'tcx> {
}

crate fn downcast(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx) -> Self {
self.project(PlaceElem::Downcast(
Some(adt_def.variants[variant_index].ident.name),
variant_index,
))
self.project(PlaceElem::Downcast(Some(adt_def.variants[variant_index].name), variant_index))
}

fn index(self, index: Local) -> Self {
Expand Down
Loading

0 comments on commit 450ef86

Please sign in to comment.