Skip to content

Commit 450ef86

Browse files
committed
Store a Symbol instead of an Ident in VariantDef/FieldDef
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`.
1 parent e4b1d58 commit 450ef86

File tree

38 files changed

+120
-107
lines changed

38 files changed

+120
-107
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
372372
} else {
373373
def.non_enum_variant()
374374
};
375-
variant.fields[field.index()].ident.to_string()
375+
variant.fields[field.index()].name.to_string()
376376
}
377377
ty::Tuple(_) => field.index().to_string(),
378378
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'tcx> DebugContext<'tcx> {
174174

175175
field_entry.set(
176176
gimli::DW_AT_name,
177-
AttributeValue::String(field_def.ident.as_str().to_string().into_bytes()),
177+
AttributeValue::String(field_def.name.as_str().to_string().into_bytes()),
178178
);
179179
field_entry.set(
180180
gimli::DW_AT_data_member_location,

compiler/rustc_codegen_gcc/src/type_of.rs

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

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {
13001300
let name = if self.variant.ctor_kind == CtorKind::Fn {
13011301
format!("__{}", i)
13021302
} else {
1303-
f.ident.to_string()
1303+
f.name.to_string()
13041304
};
13051305
let field = layout.field(cx, i);
13061306
MemberDescription {
@@ -1480,7 +1480,7 @@ impl<'tcx> UnionMemberDescriptionFactory<'tcx> {
14801480
.map(|(i, f)| {
14811481
let field = self.layout.field(cx, i);
14821482
MemberDescription {
1483-
name: f.ident.to_string(),
1483+
name: f.name.to_string(),
14841484
type_metadata: type_metadata(cx, field.ty, self.span),
14851485
offset: Size::ZERO,
14861486
size: field.size,
@@ -1950,7 +1950,7 @@ enum VariantInfo<'a, 'tcx> {
19501950
impl<'tcx> VariantInfo<'_, 'tcx> {
19511951
fn map_struct_name<R>(&self, f: impl FnOnce(&str) -> R) -> R {
19521952
match self {
1953-
VariantInfo::Adt(variant) => f(variant.ident.as_str()),
1953+
VariantInfo::Adt(variant) => f(variant.name.as_str()),
19541954
VariantInfo::Generator { variant_index, .. } => {
19551955
f(&GeneratorSubsts::variant_name(*variant_index))
19561956
}
@@ -1959,7 +1959,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> {
19591959

19601960
fn variant_name(&self) -> String {
19611961
match self {
1962-
VariantInfo::Adt(variant) => variant.ident.to_string(),
1962+
VariantInfo::Adt(variant) => variant.name.to_string(),
19631963
VariantInfo::Generator { variant_index, .. } => {
19641964
// Since GDB currently prints out the raw discriminant along
19651965
// with every variant, make each variant name be just the value
@@ -1973,7 +1973,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> {
19731973
fn field_name(&self, i: usize) -> String {
19741974
let field_name = match *self {
19751975
VariantInfo::Adt(variant) if variant.ctor_kind != CtorKind::Fn => {
1976-
Some(variant.fields[i].ident.name)
1976+
Some(variant.fields[i].name)
19771977
}
19781978
VariantInfo::Generator {
19791979
generator_layout,
@@ -2063,7 +2063,7 @@ fn prepare_enum_metadata<'ll, 'tcx>(
20632063
let enumerators_metadata: Vec<_> = match enum_type.kind() {
20642064
ty::Adt(def, _) => iter::zip(def.discriminants(tcx), &def.variants)
20652065
.map(|((_, discr), v)| {
2066-
let name = v.ident.as_str();
2066+
let name = v.name.as_str();
20672067
let is_unsigned = match discr.ty.kind() {
20682068
ty::Int(_) => false,
20692069
ty::Uint(_) => true,

compiler/rustc_codegen_llvm/src/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn uncached_llvm_type<'a, 'tcx>(
4949
(layout.ty.kind(), &layout.variants)
5050
{
5151
if def.is_enum() && !def.variants.is_empty() {
52-
write!(&mut name, "::{}", def.variants[index].ident).unwrap();
52+
write!(&mut name, "::{}", def.variants[index].name).unwrap();
5353
}
5454
}
5555
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,14 @@ fn push_debuginfo_type_name<'tcx>(
409409
let max = dataful_discriminant_range.end;
410410
let max = tag.value.size(&tcx).truncate(max);
411411

412-
let dataful_variant_name = def.variants[*dataful_variant].ident.as_str();
412+
let dataful_variant_name = def.variants[*dataful_variant].name.as_str();
413413

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

421421
output.push_str(&format!(", {}", variant));
422422
}

compiler/rustc_const_eval/src/interpret/validity.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
267267
match layout.variants {
268268
Variants::Single { index } => {
269269
// Inside a variant
270-
PathElem::Field(def.variants[index].fields[field].ident.name)
270+
PathElem::Field(def.variants[index].fields[field].name)
271271
}
272272
Variants::Multiple { .. } => bug!("we handled variants above"),
273273
}
274274
}
275275

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

279279
// arrays/slices
280280
ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),
@@ -726,7 +726,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
726726
new_op: &OpTy<'tcx, M::PointerTag>,
727727
) -> InterpResult<'tcx> {
728728
let name = match old_op.layout.ty.kind() {
729-
ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].ident.name),
729+
ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].name),
730730
// Generators also have variants
731731
ty::Generator(..) => PathElem::GeneratorState(variant_id),
732732
_ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19241924
.fields
19251925
.iter()
19261926
.filter(|field| field.vis.is_accessible_from(field.did, self.tcx))
1927-
.map(|field| (field.ident.name, field.ty(self.tcx, expected_substs)))
1927+
.map(|field| (field.name, field.ty(self.tcx, expected_substs)))
19281928
.find(|(_, ty)| same_type_modulo_infer(ty, exp_found.found))
19291929
{
19301930
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {

compiler/rustc_metadata/src/rmeta/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
862862
let ctor_did = data.ctor.map(|index| self.local_def_id(index));
863863

864864
ty::VariantDef::new(
865-
self.item_ident(index, sess),
865+
self.item_ident(index, sess).name,
866866
variant_did,
867867
ctor_did,
868868
data.discr,
@@ -874,7 +874,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
874874
.decode(self)
875875
.map(|index| ty::FieldDef {
876876
did: self.local_def_id(index),
877-
ident: self.item_ident(index, sess),
877+
name: self.item_ident(index, sess).name,
878878
vis: self.get_visibility(index),
879879
})
880880
.collect(),

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10521052
assert!(f.did.is_local());
10531053
f.did.index
10541054
}));
1055-
self.encode_ident_span(def_id, variant.ident);
1055+
self.encode_ident_span(def_id, variant.ident(tcx));
10561056
self.encode_item_type(def_id);
10571057
if variant.ctor_kind == CtorKind::Fn {
10581058
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
@@ -1138,7 +1138,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11381138
debug!("EncodeContext::encode_field({:?})", def_id);
11391139

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

compiler/rustc_middle/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24392439
CtorKind::Fictive => {
24402440
let mut struct_fmt = fmt.debug_struct(&name);
24412441
for (field, place) in iter::zip(&variant_def.fields, places) {
2442-
struct_fmt.field(field.ident.as_str(), place);
2442+
struct_fmt.field(field.name.as_str(), place);
24432443
}
24442444
struct_fmt.finish()
24452445
}
@@ -2785,7 +2785,7 @@ impl UserTypeProjection {
27852785
field: Field,
27862786
) -> Self {
27872787
self.projs.push(ProjectionElem::Downcast(
2788-
Some(adt_def.variants[variant_index].ident.name),
2788+
Some(adt_def.variants[variant_index].name),
27892789
variant_index,
27902790
));
27912791
self.projs.push(ProjectionElem::Field(field, ()));

compiler/rustc_middle/src/thir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
726726
};
727727

728728
if let Some(variant) = variant {
729-
write!(f, "{}", variant.ident)?;
729+
write!(f, "{}", variant.name)?;
730730

731731
// Only for Adt we can have `S {...}`,
732732
// which we handle separately here.
@@ -738,7 +738,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
738738
if let PatKind::Wild = *p.pattern.kind {
739739
continue;
740740
}
741-
let name = variant.fields[p.field.index()].ident;
741+
let name = variant.fields[p.field.index()].name;
742742
write!(f, "{}{}: {}", start_or_comma(), name, p.pattern)?;
743743
printed += 1;
744744
}

compiler/rustc_middle/src/ty/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'tcx> CapturedPlace<'tcx> {
178178
write!(
179179
&mut symbol,
180180
"__{}",
181-
def.variants[variant].fields[idx as usize].ident.name.as_str(),
181+
def.variants[variant].fields[idx as usize].name.as_str(),
182182
)
183183
.unwrap();
184184
}
@@ -344,7 +344,7 @@ pub fn place_to_string_for_capture<'tcx>(tcx: TyCtxt<'tcx>, place: &HirPlace<'tc
344344
curr_string = format!(
345345
"{}.{}",
346346
curr_string,
347-
def.variants[variant].fields[idx as usize].ident.name.as_str()
347+
def.variants[variant].fields[idx as usize].name.as_str()
348348
);
349349
}
350350
ty::Tuple(_) => {

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2452,7 +2452,7 @@ impl<'tcx> TyCtxt<'tcx> {
24522452
) -> Place<'tcx> {
24532453
self.mk_place_elem(
24542454
place,
2455-
PlaceElem::Downcast(Some(adt_def.variants[variant_index].ident.name), variant_index),
2455+
PlaceElem::Downcast(Some(adt_def.variants[variant_index].name), variant_index),
24562456
)
24572457
}
24582458

compiler/rustc_middle/src/ty/layout.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::lang_items::LangItem;
1010
use rustc_index::bit_set::BitSet;
1111
use rustc_index::vec::{Idx, IndexVec};
1212
use rustc_session::{config::OptLevel, DataTypeKind, FieldInfo, SizeKind, VariantInfo};
13-
use rustc_span::symbol::{Ident, Symbol};
13+
use rustc_span::symbol::Symbol;
1414
use rustc_span::{Span, DUMMY_SP};
1515
use rustc_target::abi::call::{
1616
ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, Conv, FnAbi, PassMode, Reg, RegKind,
@@ -1810,7 +1810,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
18101810
let adt_kind = adt_def.adt_kind();
18111811
let adt_packed = adt_def.repr.pack.is_some();
18121812

1813-
let build_variant_info = |n: Option<Ident>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
1813+
let build_variant_info = |n: Option<Symbol>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
18141814
let mut min_size = Size::ZERO;
18151815
let field_info: Vec<_> = flds
18161816
.iter()
@@ -1845,15 +1845,15 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
18451845
if !adt_def.variants.is_empty() && layout.fields != FieldsShape::Primitive {
18461846
debug!(
18471847
"print-type-size `{:#?}` variant {}",
1848-
layout, adt_def.variants[index].ident
1848+
layout, adt_def.variants[index].name
18491849
);
18501850
let variant_def = &adt_def.variants[index];
1851-
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.ident.name).collect();
1851+
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
18521852
record(
18531853
adt_kind.into(),
18541854
adt_packed,
18551855
None,
1856-
vec![build_variant_info(Some(variant_def.ident), &fields, layout)],
1856+
vec![build_variant_info(Some(variant_def.name), &fields, layout)],
18571857
);
18581858
} else {
18591859
// (This case arises for *empty* enums; so give it
@@ -1872,10 +1872,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
18721872
.variants
18731873
.iter_enumerated()
18741874
.map(|(i, variant_def)| {
1875-
let fields: Vec<_> =
1876-
variant_def.fields.iter().map(|f| f.ident.name).collect();
1875+
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
18771876
build_variant_info(
1878-
Some(variant_def.ident),
1877+
Some(variant_def.name),
18791878
&fields,
18801879
layout.for_variant(self, i),
18811880
)

compiler/rustc_middle/src/ty/mod.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,7 @@ pub struct VariantDef {
15041504
/// If this variant is a struct variant, then this is `None`.
15051505
pub ctor_def_id: Option<DefId>,
15061506
/// Variant or struct name.
1507-
#[stable_hasher(project(name))]
1508-
pub ident: Ident,
1507+
pub name: Symbol,
15091508
/// Discriminant of this variant.
15101509
pub discr: VariantDiscr,
15111510
/// Fields of this variant.
@@ -1534,7 +1533,7 @@ impl VariantDef {
15341533
/// If someone speeds up attribute loading to not be a performance concern, they can
15351534
/// remove this hack and use the constructor `DefId` everywhere.
15361535
pub fn new(
1537-
ident: Ident,
1536+
name: Symbol,
15381537
variant_did: Option<DefId>,
15391538
ctor_def_id: Option<DefId>,
15401539
discr: VariantDiscr,
@@ -1546,9 +1545,9 @@ impl VariantDef {
15461545
is_field_list_non_exhaustive: bool,
15471546
) -> Self {
15481547
debug!(
1549-
"VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
1548+
"VariantDef::new(name = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
15501549
fields = {:?}, ctor_kind = {:?}, adt_kind = {:?}, parent_did = {:?})",
1551-
ident, variant_did, ctor_def_id, discr, fields, ctor_kind, adt_kind, parent_did,
1550+
name, variant_did, ctor_def_id, discr, fields, ctor_kind, adt_kind, parent_did,
15521551
);
15531552

15541553
let mut flags = VariantFlags::NO_VARIANT_FLAGS;
@@ -1563,7 +1562,7 @@ impl VariantDef {
15631562
VariantDef {
15641563
def_id: variant_did.unwrap_or(parent_did),
15651564
ctor_def_id,
1566-
ident,
1565+
name,
15671566
discr,
15681567
fields,
15691568
ctor_kind,
@@ -1582,6 +1581,11 @@ impl VariantDef {
15821581
pub fn is_recovered(&self) -> bool {
15831582
self.flags.intersects(VariantFlags::IS_RECOVERED)
15841583
}
1584+
1585+
/// Computes the `Ident` of this variant by looking up the `Span`
1586+
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
1587+
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
1588+
}
15851589
}
15861590

15871591
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
@@ -1600,8 +1604,7 @@ pub enum VariantDiscr {
16001604
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
16011605
pub struct FieldDef {
16021606
pub did: DefId,
1603-
#[stable_hasher(project(name))]
1604-
pub ident: Ident,
1607+
pub name: Symbol,
16051608
pub vis: Visibility,
16061609
}
16071610

@@ -1776,6 +1779,11 @@ impl<'tcx> FieldDef {
17761779
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
17771780
tcx.type_of(self.did).subst(tcx, subst)
17781781
}
1782+
1783+
/// Computes the `Ident` of this variant by looking up the `Span`
1784+
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
1785+
Ident::new(self.name, tcx.def_ident_span(self.did).unwrap())
1786+
}
17791787
}
17801788

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

18941902
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
1895-
variant.fields.iter().position(|field| self.hygienic_eq(ident, field.ident, variant.def_id))
1903+
variant
1904+
.fields
1905+
.iter()
1906+
.position(|field| self.hygienic_eq(ident, field.ident(self), variant.def_id))
18961907
}
18971908

18981909
/// Returns `true` if the impls are the same polarity and the trait either

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ pub trait PrettyPrinter<'tcx>:
14751475
if !first {
14761476
p!(", ");
14771477
}
1478-
p!(write("{}: ", field_def.ident), print(field));
1478+
p!(write("{}: ", field_def.name), print(field));
14791479
first = false;
14801480
}
14811481
p!(" }}");

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,7 @@ impl<'tcx> PlaceBuilder<'tcx> {
336336
}
337337

338338
crate fn downcast(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx) -> Self {
339-
self.project(PlaceElem::Downcast(
340-
Some(adt_def.variants[variant_index].ident.name),
341-
variant_index,
342-
))
339+
self.project(PlaceElem::Downcast(Some(adt_def.variants[variant_index].name), variant_index))
343340
}
344341

345342
fn index(self, index: Local) -> Self {

0 commit comments

Comments
 (0)