Skip to content

Commit 145697c

Browse files
committed
Switch baack to Spanned<Name> instead of Ident
While the fields are equivalent, we we constructing `Ident`s for things that weren't really identifiers (e.g `kw::Invalid` + the span of one of the types in a tuple struct).
1 parent 2daf5c6 commit 145697c

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::u32;
3535

3636
use log::debug;
3737
use proc_macro::bridge::client::ProcMacro;
38-
use rustc_ast::ast::{self, Ident};
38+
use rustc_ast::ast::{self, Ident, Name};
3939
use rustc_attr as attr;
4040
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
4141
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
@@ -47,6 +47,7 @@ use rustc_span::{self, hygiene::MacroKind, BytePos, Pos, Span, DUMMY_SP};
4747
use crate::creader::{ImportedSourceFile, SourceMapImportInfo};
4848

4949
pub use cstore_impl::{provide, provide_extern};
50+
use rustc_span::source_map::{respan, Spanned};
5051

5152
mod cstore_impl;
5253

@@ -1314,14 +1315,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13141315
)
13151316
}
13161317

1317-
fn get_struct_field_names(&self, id: DefIndex, sess: &Session) -> Vec<Ident> {
1318+
fn get_struct_field_names(&self, id: DefIndex, sess: &Session) -> Vec<Spanned<Name>> {
13181319
self.root
13191320
.per_def
13201321
.children
13211322
.get(self, id)
13221323
.unwrap_or(Lazy::empty())
13231324
.decode((self, sess))
1324-
.map(|index| self.item_name(index))
1325+
.map(|index| {
1326+
let ident = self.item_name(index);
1327+
respan(ident.span, ident.name)
1328+
})
13251329
.collect()
13261330
}
13271331

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ use rustc_ast::expand::allocator::AllocatorKind;
2020
use rustc_data_structures::svh::Svh;
2121
use rustc_hir as hir;
2222
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
23-
use rustc_span::source_map::{self, Span};
24-
use rustc_span::symbol::{Ident, Symbol};
23+
use rustc_span::source_map::{self, Span, Spanned};
24+
use rustc_span::symbol::Symbol;
2525

26+
use rustc_ast::ast::Name;
2627
use rustc_data_structures::sync::Lrc;
2728
use smallvec::SmallVec;
2829
use std::any::Any;
@@ -386,7 +387,7 @@ pub fn provide(providers: &mut Providers<'_>) {
386387
}
387388

388389
impl CStore {
389-
pub fn struct_field_names_untracked(&self, def: DefId, sess: &Session) -> Vec<Ident> {
390+
pub fn struct_field_names_untracked(&self, def: DefId, sess: &Session) -> Vec<Spanned<Name>> {
390391
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
391392
}
392393

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use rustc::bug;
1919
use rustc::hir::exports::Export;
2020
use rustc::middle::cstore::CrateStore;
2121
use rustc::ty;
22-
use rustc_ast::ast::Ident;
2322
use rustc_ast::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
2423
use rustc_ast::ast::{AssocItem, AssocItemKind, MetaItemKind, StmtKind};
24+
use rustc_ast::ast::{Ident, Name};
2525
use rustc_ast::token::{self, Token};
2626
use rustc_ast::visit::{self, AssocCtxt, Visitor};
2727
use rustc_attr as attr;
@@ -37,6 +37,7 @@ use rustc_span::symbol::{kw, sym};
3737
use rustc_span::{Span, DUMMY_SP};
3838

3939
use log::debug;
40+
use rustc_span::source_map::{respan, Spanned};
4041
use std::cell::Cell;
4142
use std::ptr;
4243

@@ -287,12 +288,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
287288
let field_names = vdata
288289
.fields()
289290
.iter()
290-
.map(|field| field.ident.unwrap_or_else(|| Ident::new(kw::Invalid, field.span)))
291+
.map(|field| {
292+
field
293+
.ident
294+
// For unnamed fields, use the span of the field with `kw::Invalid`
295+
// For named fields, just convert the `Ident` to a `Spanned<Name>` directly
296+
.map_or_else(|| respan(field.span, kw::Invalid), |i| respan(i.span, i.name))
297+
})
291298
.collect();
292299
self.insert_field_names(def_id, field_names);
293300
}
294301

295-
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Ident>) {
302+
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Spanned<Name>>) {
296303
if !field_names.is_empty() {
297304
self.r.field_names.insert(def_id, field_names);
298305
}

src/librustc_resolve/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
624624
if let Some(field_names) = self.r.field_names.get(&did) {
625625
if field_names
626626
.iter()
627-
.any(|&field_name| ident.name == field_name.name)
627+
.any(|&field_name| ident.name == field_name.node)
628628
{
629629
return Some(AssocSuggestion::Field);
630630
}

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use diagnostics::{ImportSuggestion, Suggestion};
5858
use imports::{Import, ImportKind, ImportResolver, NameResolution};
5959
use late::{HasGenericParams, PathSource, Rib, RibKind::*};
6060
use macros::{MacroRulesBinding, MacroRulesScope};
61+
use rustc_span::source_map::Spanned;
6162

6263
type Res = def::Res<NodeId>;
6364

@@ -834,7 +835,7 @@ pub struct Resolver<'a> {
834835

835836
/// Names of fields of an item `DefId` accessible with dot syntax.
836837
/// Used for hints during error reporting.
837-
field_names: FxHashMap<DefId, Vec<Ident>>,
838+
field_names: FxHashMap<DefId, Vec<Spanned<Name>>>,
838839

839840
/// All imports known to succeed or fail.
840841
determined_imports: Vec<&'a Import<'a>>,

0 commit comments

Comments
 (0)