rustc: track the Span's of definitions across crates.

This commit is contained in:
Eduard-Mihai Burtescu 2016-11-24 01:39:13 +02:00
parent fa0005f2d5
commit 177913b49c
18 changed files with 123 additions and 107 deletions

View file

@ -760,47 +760,40 @@ impl<'ast> Map<'ast> {
}
}
pub fn opt_span(&self, id: NodeId) -> Option<Span> {
let sp = match self.find(id) {
Some(NodeItem(item)) => item.span,
Some(NodeForeignItem(foreign_item)) => foreign_item.span,
Some(NodeTraitItem(trait_method)) => trait_method.span,
Some(NodeImplItem(ref impl_item)) => impl_item.span,
Some(NodeVariant(variant)) => variant.span,
Some(NodeField(field)) => field.span,
Some(NodeExpr(expr)) => expr.span,
Some(NodeStmt(stmt)) => stmt.span,
Some(NodeTy(ty)) => ty.span,
Some(NodeTraitRef(tr)) => tr.path.span,
Some(NodeLocal(pat)) => pat.span,
Some(NodePat(pat)) => pat.span,
Some(NodeBlock(block)) => block.span,
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
Some(NodeTyParam(ty_param)) => ty_param.span,
Some(NodeVisibility(&Visibility::Restricted { ref path, .. })) => path.span,
_ => return None,
};
Some(sp)
}
pub fn span(&self, id: NodeId) -> Span {
self.read(id); // reveals span from node
self.opt_span(id)
.unwrap_or_else(|| bug!("AstMap.span: could not find span for id {:?}", id))
match self.find_entry(id) {
Some(EntryItem(_, item)) => item.span,
Some(EntryForeignItem(_, foreign_item)) => foreign_item.span,
Some(EntryTraitItem(_, trait_method)) => trait_method.span,
Some(EntryImplItem(_, impl_item)) => impl_item.span,
Some(EntryVariant(_, variant)) => variant.span,
Some(EntryField(_, field)) => field.span,
Some(EntryExpr(_, expr)) => expr.span,
Some(EntryStmt(_, stmt)) => stmt.span,
Some(EntryTy(_, ty)) => ty.span,
Some(EntryTraitRef(_, tr)) => tr.path.span,
Some(EntryLocal(_, pat)) => pat.span,
Some(EntryPat(_, pat)) => pat.span,
Some(EntryBlock(_, block)) => block.span,
Some(EntryStructCtor(_, _)) => self.expect_item(self.get_parent(id)).span,
Some(EntryLifetime(_, lifetime)) => lifetime.span,
Some(EntryTyParam(_, ty_param)) => ty_param.span,
Some(EntryVisibility(_, &Visibility::Restricted { ref path, .. })) => path.span,
Some(EntryVisibility(_, v)) => bug!("unexpected Visibility {:?}", v),
Some(RootCrate) => self.krate().span,
Some(RootInlinedParent(parent)) => parent.body.span,
Some(NotPresent) | None => {
bug!("hir::map::Map::span: id not in map: {:?}", id)
}
}
}
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
self.as_local_node_id(id).map(|id| self.span(id))
}
pub fn def_id_span(&self, def_id: DefId, fallback: Span) -> Span {
if let Some(node_id) = self.as_local_node_id(def_id) {
self.opt_span(node_id).unwrap_or(fallback)
} else {
fallback
}
}
pub fn node_to_string(&self, id: NodeId) -> String {
node_id_to_string(self, id, true)
}

View file

@ -260,6 +260,7 @@ pub struct ExternCrate {
pub trait CrateStore<'tcx> {
// item info
fn describe_def(&self, def: DefId) -> Option<Def>;
fn def_span(&self, sess: &Session, def: DefId) -> Span;
fn stability(&self, def: DefId) -> Option<attr::Stability>;
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
fn visibility(&self, def: DefId) -> ty::Visibility;
@ -404,6 +405,7 @@ pub struct DummyCrateStore;
impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
// item info
fn describe_def(&self, def: DefId) -> Option<Def> { bug!("describe_def") }
fn def_span(&self, sess: &Session, def: DefId) -> Span { bug!("def_span") }
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }

View file

@ -291,10 +291,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
expected.ty,
found.ty));
match
self.map.as_local_node_id(expected.def_id)
.and_then(|node_id| self.map.opt_span(node_id))
{
match self.map.span_if_local(expected.def_id) {
Some(span) => {
db.span_note(span, "a default was defined here...");
}
@ -308,10 +305,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
expected.origin_span,
"...that was applied to an unconstrained type variable here");
match
self.map.as_local_node_id(found.def_id)
.and_then(|node_id| self.map.opt_span(node_id))
{
match self.map.span_if_local(found.def_id) {
Some(span) => {
db.span_note(span, "a second default was defined here...");
}

View file

@ -2370,6 +2370,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}
pub fn def_span(self, def_id: DefId) -> Span {
if let Some(id) = self.map.as_local_node_id(def_id) {
self.map.span(id)
} else {
self.sess.cstore.def_span(&self.sess, def_id)
}
}
pub fn item_name(self, id: DefId) -> ast::Name {
if let Some(id) = self.map.as_local_node_id(id) {
self.map.name(id)

View file

@ -496,8 +496,8 @@ fn add_fragment_siblings_for_extension<'a, 'tcx>(this: &MoveData<'tcx>,
},
ref ty => {
let opt_span = origin_id.and_then(|id|tcx.map.opt_span(id));
span_bug!(opt_span.unwrap_or(DUMMY_SP),
let span = origin_id.map_or(DUMMY_SP, |id| tcx.map.span(id));
span_bug!(span,
"type {:?} ({:?}) is not fragmentable",
parent_ty, ty);
}

View file

@ -1006,11 +1006,7 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
got {:?}",
node);
// Point to what was found, if there's an accessible span.
match tcx.map.opt_span(nodeid) {
Some(sp) => tcx.sess.span_fatal(sp, &message),
None => tcx.sess.fatal(&message),
}
tcx.sess.span_fatal(tcx.map.span(nodeid), &message)
}
}
}

View file

@ -32,7 +32,7 @@ use syntax::ast;
use syntax::attr;
use syntax::parse::new_parser_from_source_str;
use syntax::symbol::Symbol;
use syntax_pos::mk_sp;
use syntax_pos::{mk_sp, Span};
use rustc::hir::svh::Svh;
use rustc_back::target::Target;
use rustc::hir;
@ -43,6 +43,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def.krate).get_def(def.index)
}
fn def_span(&self, sess: &Session, def: DefId) -> Span {
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).get_span(def.index, sess)
}
fn stability(&self, def: DefId) -> Option<attr::Stability> {
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).get_stability(def.index)
@ -383,20 +388,23 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
let local_span = mk_sp(lo, parser.prev_span.hi);
// Mark the attrs as used
for attr in &def.attrs {
let attrs = data.get_item_attrs(id.index);
for attr in &attrs {
attr::mark_used(attr);
}
let name = data.def_key(id.index).disambiguated_data.data
.get_opt_name().expect("no name in load_macro");
sess.imported_macro_spans.borrow_mut()
.insert(local_span, (def.name.as_str().to_string(), def.span));
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
LoadedMacro::MacroRules(ast::MacroDef {
ident: ast::Ident::with_empty_ctxt(def.name),
ident: ast::Ident::with_empty_ctxt(name),
id: ast::DUMMY_NODE_ID,
span: local_span,
imported_from: None, // FIXME
allow_internal_unstable: attr::contains_name(&def.attrs, "allow_internal_unstable"),
attrs: def.attrs,
allow_internal_unstable: attr::contains_name(&attrs, "allow_internal_unstable"),
attrs: attrs,
body: body,
})
}

View file

@ -25,6 +25,7 @@ use rustc::middle::cstore::{InlinedItem, LinkagePreference};
use rustc::hir::def::{self, Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::middle::lang_items;
use rustc::session::Session;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
@ -47,8 +48,9 @@ use syntax_pos::{self, Span, BytePos, Pos};
pub struct DecodeContext<'a, 'tcx: 'a> {
opaque: opaque::Decoder<'a>,
tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
cdata: Option<&'a CrateMetadata>,
sess: Option<&'a Session>,
tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
from_id_range: IdRange,
to_id_range: IdRange,
@ -61,22 +63,21 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
/// Abstract over the various ways one can create metadata decoders.
pub trait Metadata<'a, 'tcx>: Copy {
fn raw_bytes(self) -> &'a [u8];
fn cdata(self) -> Option<&'a CrateMetadata> {
None
}
fn tcx(self) -> Option<TyCtxt<'a, 'tcx, 'tcx>> {
None
}
fn cdata(self) -> Option<&'a CrateMetadata> { None }
fn sess(self) -> Option<&'a Session> { None }
fn tcx(self) -> Option<TyCtxt<'a, 'tcx, 'tcx>> { None }
fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
let id_range = IdRange {
min: NodeId::from_u32(u32::MIN),
max: NodeId::from_u32(u32::MAX),
};
let tcx = self.tcx();
DecodeContext {
opaque: opaque::Decoder::new(self.raw_bytes(), pos),
cdata: self.cdata(),
tcx: self.tcx(),
sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
tcx: tcx,
from_id_range: id_range,
to_id_range: id_range,
last_filemap_index: 0,
@ -104,6 +105,18 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadata {
}
}
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, &'a Session) {
fn raw_bytes(self) -> &'a [u8] {
self.0.raw_bytes()
}
fn cdata(self) -> Option<&'a CrateMetadata> {
Some(self.0)
}
fn sess(self) -> Option<&'a Session> {
Some(&self.1)
}
}
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'a, 'tcx, 'tcx>) {
fn raw_bytes(self) -> &'a [u8] {
self.0.raw_bytes()
@ -280,8 +293,8 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
let lo = BytePos::decode(self)?;
let hi = BytePos::decode(self)?;
let tcx = if let Some(tcx) = self.tcx {
tcx
let sess = if let Some(sess) = self.sess {
sess
} else {
return Ok(syntax_pos::mk_sp(lo, hi));
};
@ -299,7 +312,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
(lo, hi)
};
let imported_filemaps = self.cdata().imported_filemaps(&tcx.sess.codemap());
let imported_filemaps = self.cdata().imported_filemaps(&sess.codemap());
let filemap = {
// Optimize for the case that most spans within a translated item
// originate from the same filemap.
@ -528,6 +541,10 @@ impl<'a, 'tcx> CrateMetadata {
}
}
pub fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
self.entry(index).span.decode((self, sess))
}
pub fn get_trait_def(&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)

View file

@ -275,6 +275,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: EntryKind::Variant(self.lazy(&data)),
visibility: enum_vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
children: self.lazy_seq(variant.fields.iter().map(|f| {
@ -313,6 +314,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: EntryKind::Mod(self.lazy(&data)),
visibility: vis.simplify(),
span: self.lazy(&md.inner),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(attrs),
children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
@ -393,6 +395,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: EntryKind::Field,
visibility: field.vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
children: LazySeq::empty(),
@ -426,6 +429,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: EntryKind::Struct(self.lazy(&data)),
visibility: struct_vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: LazySeq::empty(),
children: LazySeq::empty(),
@ -492,6 +496,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: kind,
visibility: trait_item.vis.simplify(),
span: self.lazy(&ast_item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&ast_item.attrs),
children: LazySeq::empty(),
@ -580,6 +585,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: kind,
visibility: impl_item.vis.simplify(),
span: self.lazy(&ast_item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&ast_item.attrs),
children: LazySeq::empty(),
@ -743,6 +749,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: kind,
visibility: item.vis.simplify(),
span: self.lazy(&item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&item.attrs),
children: match item.node {
@ -850,18 +857,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
/// Serialize the text of exported macros
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
let def_id = self.tcx.map.local_def_id(macro_def.id);
let macro_def = MacroDef {
name: macro_def.name,
attrs: macro_def.attrs.to_vec(),
span: macro_def.span,
body: ::syntax::print::pprust::tts_to_string(&macro_def.body)
};
Entry {
kind: EntryKind::MacroDef(self.lazy(&macro_def)),
kind: EntryKind::MacroDef(self.lazy(&MacroDef {
body: ::syntax::print::pprust::tts_to_string(&macro_def.body)
})),
visibility: ty::Visibility::Public,
span: self.lazy(&macro_def.span),
def_key: self.encode_def_key(def_id),
attributes: LazySeq::empty(),
attributes: self.encode_attributes(&macro_def.attrs),
children: LazySeq::empty(),
stability: None,
deprecation: None,
@ -960,6 +964,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: kind,
visibility: nitem.vis.simplify(),
span: self.lazy(&nitem.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&nitem.attrs),
children: LazySeq::empty(),
@ -1038,9 +1043,11 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_info_for_anon_ty(&mut self, def_id: DefId) -> Entry<'tcx> {
let tcx = self.tcx;
Entry {
kind: EntryKind::Type,
visibility: ty::Visibility::Public,
span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: LazySeq::empty(),
children: LazySeq::empty(),
@ -1069,6 +1076,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Entry {
kind: EntryKind::Closure(self.lazy(&data)),
visibility: ty::Visibility::Public,
span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
children: LazySeq::empty(),
@ -1163,11 +1171,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let all_filemaps = codemap.files.borrow();
self.lazy_seq_ref(all_filemaps.iter()
.filter(|filemap| {
// No need to export empty filemaps, as they can't contain spans
// that need translation.
// Also no need to re-export imported filemaps, as any downstream
// No need to re-export imported filemaps, as any downstream
// crate will import them from their original source.
!filemap.lines.borrow().is_empty() && !filemap.is_imported()
!filemap.is_imported()
})
.map(|filemap| &**filemap))
}

View file

@ -197,18 +197,11 @@ pub struct TraitImpls {
pub impls: LazySeq<DefIndex>,
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct MacroDef {
pub name: ast::Name,
pub attrs: Vec<ast::Attribute>,
pub span: Span,
pub body: String,
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct Entry<'tcx> {
pub kind: EntryKind<'tcx>,
pub visibility: ty::Visibility,
pub span: Lazy<Span>,
pub def_key: Lazy<hir::map::DefKey>,
pub attributes: LazySeq<ast::Attribute>,
pub children: LazySeq<DefIndex>,
@ -257,6 +250,11 @@ pub struct ModData {
pub reexports: LazySeq<def::Export>,
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct MacroDef {
pub body: String,
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct FnData {
pub constness: hir::Constness,

View file

@ -69,7 +69,7 @@ pub fn item_namespace(ccx: &CrateContext, def_id: DefId) -> DIScope {
};
let namespace_name = CString::new(namespace_name.as_bytes()).unwrap();
let span = ccx.tcx().map.def_id_span(def_id, DUMMY_SP);
let span = ccx.tcx().def_span(def_id);
let (file, line) = if span != DUMMY_SP {
let loc = span_start(ccx, span);
(file_metadata(ccx, &loc.file.name, &loc.file.abs_path), loc.line as c_uint)

View file

@ -79,7 +79,7 @@ pub fn get_namespace_and_span_for_item(cx: &CrateContext, def_id: DefId)
});
// Try to get some span information, if we have an inlined item.
let definition_span = cx.tcx().map.def_id_span(def_id, syntax_pos::DUMMY_SP);
let definition_span = cx.tcx().def_span(def_id);
(containing_scope, definition_span)
}

View file

@ -1222,8 +1222,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
self.tcx().associated_items(b.def_id()).find(|item| {
item.kind == ty::AssociatedKind::Type && item.name == assoc_name
})
.and_then(|item| self.tcx().map.as_local_node_id(item.def_id))
.and_then(|node_id| self.tcx().map.opt_span(node_id))
.and_then(|item| self.tcx().map.span_if_local(item.def_id))
});
let mut err = struct_span_err!(

View file

@ -21,7 +21,7 @@ use rustc::traits::{self, ObligationCause, Reveal};
use util::nodemap::FxHashSet;
use syntax::ast;
use syntax_pos::{self, Span};
use syntax_pos::Span;
/// check_drop_impl confirms that the Drop implementation identfied by
/// `drop_impl_did` is not any more specialized than the type it is
@ -59,7 +59,7 @@ pub fn check_drop_impl(ccx: &CrateCtxt, drop_impl_did: DefId) -> Result<(), ()>
_ => {
// Destructors only work on nominal types. This was
// already checked by coherence, so we can panic here.
let span = ccx.tcx.map.def_id_span(drop_impl_did, syntax_pos::DUMMY_SP);
let span = ccx.tcx.def_span(drop_impl_did);
span_bug!(span,
"should have been rejected by coherence check: {}",
dtor_self_type);
@ -88,7 +88,7 @@ fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
let named_type = tcx.item_type(self_type_did);
let named_type = named_type.subst(tcx, &infcx.parameter_environment.free_substs);
let drop_impl_span = tcx.map.def_id_span(drop_impl_did, syntax_pos::DUMMY_SP);
let drop_impl_span = tcx.def_span(drop_impl_did);
let fresh_impl_substs =
infcx.fresh_substs_for_item(drop_impl_span, drop_impl_did);
let fresh_impl_self_ty = drop_impl_ty.subst(tcx, fresh_impl_substs);
@ -173,7 +173,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'a, 'tcx>(
let self_type_node_id = tcx.map.as_local_node_id(self_type_did).unwrap();
let drop_impl_span = tcx.map.def_id_span(drop_impl_did, syntax_pos::DUMMY_SP);
let drop_impl_span = tcx.def_span(drop_impl_did);
// We can assume the predicates attached to struct/enum definition
// hold.

View file

@ -124,7 +124,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
CandidateSource::TraitSource(trait_did) => {
let item = self.associated_item(trait_did, item_name).unwrap();
let item_span = self.tcx.map.def_id_span(item.def_id, span);
let item_span = self.tcx.def_span(item.def_id);
span_note!(err,
item_span,
"candidate #{} is defined in the trait `{}`",

View file

@ -1754,7 +1754,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let item_predicates = self.tcx.item_predicates(def_id);
let bounds = item_predicates.instantiate(self.tcx, substs);
let span = self.tcx.map.def_id_span(def_id, codemap::DUMMY_SP);
let span = self.tcx.def_span(def_id);
for predicate in bounds.predicates {
// Change the predicate to refer to the type variable,
// which will be the concrete type, instead of the TyAnon.

View file

@ -188,7 +188,7 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
fcx.register_wf_obligation(ty, span, code.clone());
}
ty::AssociatedKind::Method => {
reject_shadowing_type_parameters(fcx.tcx, span, item.def_id);
reject_shadowing_type_parameters(fcx.tcx, item.def_id);
let method_ty = fcx.tcx.item_type(item.def_id);
let method_ty = fcx.instantiate_type_scheme(span, free_substs, &method_ty);
let predicates = fcx.instantiate_bounds(span, item.def_id, free_substs);
@ -581,7 +581,7 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
}
}
fn reject_shadowing_type_parameters(tcx: TyCtxt, span: Span, def_id: DefId) {
fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
let generics = tcx.item_generics(def_id);
let parent = tcx.item_generics(generics.parent.unwrap());
let impl_params: FxHashMap<_, _> = parent.types
@ -592,17 +592,12 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, span: Span, def_id: DefId) {
for method_param in &generics.types {
if impl_params.contains_key(&method_param.name) {
// Tighten up the span to focus on only the shadowing type
let shadow_node_id = tcx.map.as_local_node_id(method_param.def_id).unwrap();
let type_span = match tcx.map.opt_span(shadow_node_id) {
Some(osp) => osp,
None => span
};
let type_span = tcx.def_span(method_param.def_id);
// The expectation here is that the original trait declaration is
// local so it should be okay to just unwrap everything.
let trait_def_id = impl_params.get(&method_param.name).unwrap();
let trait_node_id = tcx.map.as_local_node_id(*trait_def_id).unwrap();
let trait_decl_span = tcx.map.opt_span(trait_node_id).unwrap();
let trait_def_id = impl_params[&method_param.name];
let trait_decl_span = tcx.def_span(trait_def_id);
error_194(tcx, type_span, trait_decl_span, method_param.name);
}
}

View file

@ -24,7 +24,7 @@ use rustc::util::nodemap::DefIdMap;
use std::cell::Cell;
use syntax::ast;
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;
use rustc::hir::print::pat_to_string;
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
@ -542,7 +542,7 @@ impl<'a, 'gcx, 'tcx> ResolveReason {
}
ResolvingClosure(did) |
ResolvingAnonTy(did) => {
tcx.map.def_id_span(did, DUMMY_SP)
tcx.def_span(did)
}
ResolvingDeferredObligation(span) => span
}