Skip to content

Commit b3ee735

Browse files
committed
resolve: Remove struct_field_names_untracked
1 parent 2a716f3 commit b3ee735

File tree

8 files changed

+56
-76
lines changed

8 files changed

+56
-76
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use rustc_session::cstore::{
3030
};
3131
use rustc_session::Session;
3232
use rustc_span::hygiene::ExpnIndex;
33-
use rustc_span::source_map::{respan, Spanned};
3433
use rustc_span::symbol::{kw, Ident, Symbol};
3534
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3635

@@ -1134,20 +1133,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11341133
.decode((self, sess))
11351134
}
11361135

1137-
fn get_struct_field_names(
1138-
self,
1139-
id: DefIndex,
1140-
sess: &'a Session,
1141-
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
1142-
self.root
1143-
.tables
1144-
.children
1145-
.get(self, id)
1146-
.expect("fields not encoded for a struct")
1147-
.decode(self)
1148-
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
1149-
}
1150-
11511136
fn get_inherent_implementations_for_type(
11521137
self,
11531138
tcx: TyCtxt<'tcx>,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_middle::ty::{self, TyCtxt};
1919
use rustc_session::cstore::{CrateSource, CrateStore};
2020
use rustc_session::{Session, StableCrateId};
2121
use rustc_span::hygiene::{ExpnHash, ExpnId};
22-
use rustc_span::source_map::{Span, Spanned};
2322
use rustc_span::symbol::{kw, Symbol};
23+
use rustc_span::Span;
2424

2525
use rustc_data_structures::sync::Lrc;
2626
use std::any::Any;
@@ -507,14 +507,6 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
507507
}
508508

509509
impl CStore {
510-
pub fn struct_field_names_untracked<'a>(
511-
&'a self,
512-
def: DefId,
513-
sess: &'a Session,
514-
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
515-
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
516-
}
517-
518510
pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
519511
self.get_crate_data(def.krate).get_ctor(def.index)
520512
}

compiler/rustc_resolve/src/build_reduced_graph.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_middle::metadata::ModChild;
2929
use rustc_middle::{bug, ty};
3030
use rustc_session::cstore::CrateStore;
3131
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
32-
use rustc_span::source_map::respan;
3332
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3433
use rustc_span::Span;
3534

@@ -327,13 +326,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
327326
}
328327
}
329328

330-
fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
331-
let field_names = vdata
332-
.fields()
333-
.iter()
334-
.map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name)))
335-
.collect();
336-
self.r.field_names.insert(def_id, field_names);
329+
fn insert_field_def_ids(&mut self, def_id: LocalDefId, vdata: &ast::VariantData) {
330+
if vdata.fields().iter().any(|field| field.is_placeholder) {
331+
// The fields are not expanded yet.
332+
return;
333+
}
334+
let def_ids = vdata.fields().iter().map(|field| self.r.local_def_id(field.id).to_def_id());
335+
self.r.field_def_ids.insert(def_id, self.r.tcx.arena.alloc_from_iter(def_ids));
337336
}
338337

339338
fn insert_field_visibilities_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
@@ -345,12 +344,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
345344
self.r.field_visibility_spans.insert(def_id, field_vis);
346345
}
347346

348-
fn insert_field_names_extern(&mut self, def_id: DefId) {
349-
let field_names =
350-
self.r.cstore().struct_field_names_untracked(def_id, self.r.tcx.sess).collect();
351-
self.r.field_names.insert(def_id, field_names);
352-
}
353-
354347
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
355348
// If any statements are items, we need to create an anonymous module
356349
block
@@ -748,7 +741,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
748741
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
749742

750743
// Record field names for error reporting.
751-
self.insert_field_names_local(def_id, vdata);
744+
self.insert_field_def_ids(local_def_id, vdata);
752745
self.insert_field_visibilities_local(def_id, vdata);
753746

754747
// If this is a tuple or unit struct, define a name
@@ -797,7 +790,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
797790
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
798791

799792
// Record field names for error reporting.
800-
self.insert_field_names_local(def_id, vdata);
793+
self.insert_field_def_ids(local_def_id, vdata);
801794
self.insert_field_visibilities_local(def_id, vdata);
802795
}
803796

@@ -1003,12 +996,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1003996
| Res::SelfCtor(..)
1004997
| Res::Err => bug!("unexpected resolution: {:?}", res),
1005998
}
1006-
// Record some extra data for better diagnostics.
1007-
match res {
1008-
Res::Def(DefKind::Struct, def_id) => self.insert_field_names_extern(def_id),
1009-
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
1010-
_ => {}
1011-
}
1012999
}
10131000

10141001
fn add_macro_use_binding(
@@ -1519,7 +1506,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
15191506
}
15201507

15211508
// Record field names for error reporting.
1522-
self.insert_field_names_local(def_id.to_def_id(), &variant.data);
1509+
self.insert_field_def_ids(def_id, &variant.data);
15231510
self.insert_field_visibilities_local(def_id.to_def_id(), &variant.data);
15241511

15251512
visit::walk_variant(self, variant);

compiler/rustc_resolve/src/diagnostics.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15811581
)) = binding.kind
15821582
{
15831583
let def_id = self.tcx.parent(ctor_def_id);
1584-
let fields = self.field_names.get(&def_id)?;
1585-
return fields.iter().map(|name| name.span).reduce(Span::to); // None for `struct Foo()`
1584+
return self
1585+
.field_def_ids(def_id)?
1586+
.iter()
1587+
.map(|&field_id| self.def_span(field_id))
1588+
.reduce(Span::to); // None for `struct Foo()`
15861589
}
15871590
None
15881591
}

compiler/rustc_resolve/src/late/diagnostics.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -1295,19 +1295,23 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
12951295
}
12961296
_ => (": val", "literal", Applicability::HasPlaceholders),
12971297
};
1298-
let (fields, applicability) = match self.r.field_names.get(&def_id) {
1299-
Some(fields) => (
1300-
fields
1298+
1299+
let field_ids = self.r.field_def_ids(def_id);
1300+
let (fields, applicability) = match field_ids {
1301+
Some(field_ids) => (
1302+
field_ids
13011303
.iter()
1302-
.map(|f| format!("{}{}", f.node, tail))
1304+
.map(|&field_id| {
1305+
format!("{}{tail}", self.r.tcx.item_name(field_id))
1306+
})
13031307
.collect::<Vec<String>>()
13041308
.join(", "),
13051309
applicability,
13061310
),
13071311
None => ("/* fields */".to_string(), Applicability::HasPlaceholders),
13081312
};
1309-
let pad = match self.r.field_names.get(&def_id) {
1310-
Some(fields) if fields.is_empty() => "",
1313+
let pad = match field_ids {
1314+
Some(field_ids) if field_ids.is_empty() => "",
13111315
_ => " ",
13121316
};
13131317
err.span_suggestion(
@@ -1451,10 +1455,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
14511455
);
14521456

14531457
// Use spans of the tuple struct definition.
1454-
self.r
1455-
.field_names
1456-
.get(&def_id)
1457-
.map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
1458+
self.r.field_def_ids(def_id).map(|field_ids| {
1459+
field_ids
1460+
.iter()
1461+
.map(|&field_id| self.r.def_span(field_id))
1462+
.collect::<Vec<_>>()
1463+
})
14581464
}
14591465
_ => None,
14601466
};
@@ -1517,9 +1523,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
15171523
(Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => {
15181524
let def_id = self.r.tcx.parent(ctor_def_id);
15191525
err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here"));
1520-
let fields = self.r.field_names.get(&def_id).map_or_else(
1526+
let fields = self.r.field_def_ids(def_id).map_or_else(
15211527
|| "/* fields */".to_string(),
1522-
|fields| vec!["_"; fields.len()].join(", "),
1528+
|field_ids| vec!["_"; field_ids.len()].join(", "),
15231529
);
15241530
err.span_suggestion(
15251531
span,
@@ -1600,8 +1606,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
16001606
if let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) =
16011607
resolution.full_res()
16021608
{
1603-
if let Some(field_names) = self.r.field_names.get(&did) {
1604-
if field_names.iter().any(|&field_name| ident.name == field_name.node) {
1609+
if let Some(field_ids) = self.r.field_def_ids(did) {
1610+
if field_ids
1611+
.iter()
1612+
.any(|&field_id| ident.name == self.r.tcx.item_name(field_id))
1613+
{
16051614
return Some(AssocSuggestion::Field);
16061615
}
16071616
}
@@ -2015,11 +2024,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
20152024
} else {
20162025
let needs_placeholder = |ctor_def_id: DefId, kind: CtorKind| {
20172026
let def_id = self.r.tcx.parent(ctor_def_id);
2018-
let has_no_fields = self.r.field_names.get(&def_id).map_or(false, |f| f.is_empty());
20192027
match kind {
20202028
CtorKind::Const => false,
2021-
CtorKind::Fn if has_no_fields => false,
2022-
_ => true,
2029+
CtorKind::Fn => !self
2030+
.r
2031+
.field_def_ids(def_id)
2032+
.map_or(false, |field_ids| field_ids.is_empty()),
20232033
}
20242034
};
20252035

compiler/rustc_resolve/src/lib.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
5050
use rustc_query_system::ich::StableHashingContext;
5151
use rustc_session::lint::LintBuffer;
5252
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
53-
use rustc_span::source_map::Spanned;
5453
use rustc_span::symbol::{kw, sym, Ident, Symbol};
5554
use rustc_span::{Span, DUMMY_SP};
5655

@@ -881,10 +880,7 @@ pub struct Resolver<'a, 'tcx> {
881880

882881
/// N.B., this is used only for better diagnostics, not name resolution itself.
883882
has_self: LocalDefIdSet,
884-
885-
/// Names of fields of an item `DefId` accessible with dot syntax.
886-
/// Used for hints during error reporting.
887-
field_names: FxHashMap<DefId, Vec<Spanned<Symbol>>>,
883+
field_def_ids: LocalDefIdMap<&'tcx [DefId]>,
888884

889885
/// Span of the privacy modifier in fields of an item `DefId` accessible with dot syntax.
890886
/// Used for hints during error reporting.
@@ -1249,7 +1245,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12491245
extern_prelude,
12501246

12511247
has_self: Default::default(),
1252-
field_names: FxHashMap::default(),
1248+
field_def_ids: Default::default(),
12531249
field_visibility_spans: FxHashMap::default(),
12541250

12551251
determined_imports: Vec::new(),
@@ -1877,6 +1873,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18771873
}
18781874
}
18791875

1876+
fn field_def_ids(&self, def_id: DefId) -> Option<&'tcx [DefId]> {
1877+
match def_id.as_local() {
1878+
Some(def_id) => self.field_def_ids.get(&def_id).copied(),
1879+
None => Some(self.tcx.associated_item_def_ids(def_id)),
1880+
}
1881+
}
1882+
18801883
/// Checks if an expression refers to a function marked with
18811884
/// `#[rustc_legacy_const_generics]` and returns the argument index list
18821885
/// from the attribute.

tests/ui/empty/empty-struct-tuple-pat.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ LL | XEmpty5(),
4646
|
4747
help: use the tuple variant pattern syntax instead
4848
|
49-
LL | XE::XEmpty5(/* fields */) => (),
50-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
49+
LL | XE::XEmpty5() => (),
50+
| ~~~~~~~~~~~~~
5151
help: a unit variant with a similar name exists
5252
|
5353
LL | XE::XEmpty4 => (),

tests/ui/pattern/pat-tuple-field-count-cross.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) }
113113
|
114114
help: use the tuple variant pattern syntax instead
115115
|
116-
LL | E1::Z1(/* fields */) => {}
117-
| ~~~~~~~~~~~~~~~~~~~~
116+
LL | E1::Z1() => {}
117+
| ~~~~~~~~
118118
help: a unit variant with a similar name exists
119119
|
120120
LL | E1::Z0 => {}

0 commit comments

Comments
 (0)