Skip to content

Commit

Permalink
resolve: Keep field spans for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov authored and da-x committed Oct 9, 2019
1 parent 7870050 commit 5d8af38
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use syntax::source_map;
use syntax::edition::Edition;
use syntax::parse::source_file_to_stream;
use syntax::parse::parser::emit_unclosed_delims;
use syntax::source_map::Spanned;
use syntax::symbol::Symbol;
use syntax_pos::{Span, FileName};
use rustc_index::bit_set::BitSet;
Expand Down Expand Up @@ -420,8 +421,8 @@ impl cstore::CStore {
self.get_crate_data(cnum).root.edition
}

pub fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name> {
self.get_crate_data(def.krate).get_struct_field_names(def.index)
pub fn struct_field_names_untracked(&self, def: DefId, sess: &Session) -> Vec<Spanned<Symbol>> {
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
}

pub fn ctor_kind_untracked(&self, def: DefId) -> def::CtorKind {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::u32;
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use syntax::attr;
use syntax::ast::{self, Ident};
use syntax::source_map;
use syntax::source_map::{self, respan, Spanned};
use syntax::symbol::{Symbol, sym};
use syntax::ext::base::{MacroKind, SyntaxExtensionKind, SyntaxExtension};
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, symbol::{InternedString}};
Expand Down Expand Up @@ -1021,11 +1021,11 @@ impl<'a, 'tcx> CrateMetadata {
Lrc::from(self.get_attributes(&item, sess))
}

pub fn get_struct_field_names(&self, id: DefIndex) -> Vec<ast::Name> {
pub fn get_struct_field_names(&self, id: DefIndex, sess: &Session) -> Vec<Spanned<ast::Name>> {
self.entry(id)
.children
.decode(self)
.map(|index| self.item_name(index))
.map(|index| respan(self.get_span(index, sess), self.item_name(index)))
.collect()
}

Expand Down
14 changes: 8 additions & 6 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use syntax::ext::hygiene::ExpnId;
use syntax::feature_gate::is_builtin_attr;
use syntax::parse::token::{self, Token};
use syntax::{span_err, struct_span_err};
use syntax::source_map::{respan, Spanned};
use syntax::symbol::{kw, sym};
use syntax::visit::{self, Visitor};

Expand Down Expand Up @@ -301,7 +302,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
}

fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Spanned<Name>>) {
if !field_names.is_empty() {
self.r.field_names.insert(def_id, field_names);
}
Expand Down Expand Up @@ -752,12 +753,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

// Record field names for error reporting.
let field_names = struct_def.fields().iter().filter_map(|field| {
let field_names = struct_def.fields().iter().map(|field| {
let field_vis = self.resolve_visibility(&field.vis);
if ctor_vis.is_at_least(field_vis, &*self.r) {
ctor_vis = field_vis;
}
field.ident.map(|ident| ident.name)
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
}).collect();
let item_def_id = self.r.definitions.local_def_id(item.id);
self.insert_field_names(item_def_id, field_names);
Expand All @@ -779,9 +780,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
let field_names = vdata.fields().iter().filter_map(|field| {
let field_names = vdata.fields().iter().map(|field| {
self.resolve_visibility(&field.vis);
field.ident.map(|ident| ident.name)
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
}).collect();
let item_def_id = self.r.definitions.local_def_id(item.id);
self.insert_field_names(item_def_id, field_names);
Expand Down Expand Up @@ -895,7 +896,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// Record some extra data for better diagnostics.
match res {
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
let field_names = self.r.cstore.struct_field_names_untracked(def_id);
let field_names =
self.r.cstore.struct_field_names_untracked(def_id, self.r.session);
self.insert_field_names(def_id, field_names);
}
Res::Def(DefKind::Method, def_id) => {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ impl<'a> LateResolutionVisitor<'a, '_> {
Res::Def(DefKind::Struct, did) | Res::Def(DefKind::Union, did)
if resolution.unresolved_segments() == 0 => {
if let Some(field_names) = self.r.field_names.get(&did) {
if field_names.iter().any(|&field_name| ident.name == field_name) {
if field_names.iter()
.any(|&field_name| ident.name == field_name.node) {
return Some(AssocSuggestion::Field);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use syntax::attr;
use syntax::ast::{CRATE_NODE_ID, Crate};
use syntax::ast::{ItemKind, Path};
use syntax::{struct_span_err, unwrap_or};
use syntax::source_map::Spanned;

use syntax_pos::{Span, DUMMY_SP};
use errors::{Applicability, DiagnosticBuilder};
Expand Down Expand Up @@ -840,7 +841,7 @@ pub struct Resolver<'a> {

/// Names of fields of an item `DefId` accessible with dot syntax.
/// Used for hints during error reporting.
field_names: FxHashMap<DefId, Vec<Name>>,
field_names: FxHashMap<DefId, Vec<Spanned<Name>>>,

/// All imports known to succeed or fail.
determined_imports: Vec<&'a ImportDirective<'a>>,
Expand Down

0 comments on commit 5d8af38

Please sign in to comment.