}
}
- 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)
}
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;
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") }
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...");
}
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...");
}
}
}
+ 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)
},
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);
}
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)
}
}
}
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;
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)
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,
})
}
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;
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,
/// 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,
}
}
+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()
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));
};
(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.
}
}
+ 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>)
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| {
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| {
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(),
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(),
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(),
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(),
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 {
/// 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(¯o_def.body)
- };
Entry {
- kind: EntryKind::MacroDef(self.lazy(¯o_def)),
+ kind: EntryKind::MacroDef(self.lazy(&MacroDef {
+ body: ::syntax::print::pprust::tts_to_string(¯o_def.body)
+ })),
visibility: ty::Visibility::Public,
+ span: self.lazy(¯o_def.span),
def_key: self.encode_def_key(def_id),
- attributes: LazySeq::empty(),
+ attributes: self.encode_attributes(¯o_def.attrs),
children: LazySeq::empty(),
stability: None,
deprecation: None,
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(),
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(),
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(),
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))
}
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>,
pub reexports: LazySeq<def::Export>,
}
+#[derive(RustcEncodable, RustcDecodable)]
+pub struct MacroDef {
+ pub body: String,
+}
+
#[derive(RustcEncodable, RustcDecodable)]
pub struct FnData {
pub constness: hir::Constness,
};
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)
});
// 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)
}
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!(
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
_ => {
// 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);
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);
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.
}
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 `{}`",
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.
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);
}
}
-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
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);
}
}
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};
}
ResolvingClosure(did) |
ResolvingAnonTy(did) => {
- tcx.map.def_id_span(did, DUMMY_SP)
+ tcx.def_span(did)
}
ResolvingDeferredObligation(span) => span
}