1
Fork 0

HirIdify hir::Def

This commit is contained in:
ljedrz 2019-04-03 09:07:45 +02:00 committed by John Kåre Alsaker
parent ee621f4232
commit 3b99a48c4d
31 changed files with 300 additions and 118 deletions

View file

@ -2,10 +2,12 @@ use crate::hir::def_id::DefId;
use crate::util::nodemap::{NodeMap, DefIdMap}; use crate::util::nodemap::{NodeMap, DefIdMap};
use syntax::ast; use syntax::ast;
use syntax::ext::base::MacroKind; use syntax::ext::base::MacroKind;
use syntax::ast::NodeId;
use syntax_pos::Span; use syntax_pos::Span;
use rustc_macros::HashStable; use rustc_macros::HashStable;
use crate::hir; use crate::hir;
use crate::ty; use crate::ty;
use std::fmt::Debug;
use self::Namespace::*; use self::Namespace::*;
@ -43,7 +45,7 @@ pub enum NonMacroAttrKind {
} }
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)] #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
pub enum Def { pub enum Def<Id = hir::HirId> {
// Type namespace // Type namespace
Mod(DefId), Mod(DefId),
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists. /// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
@ -78,8 +80,8 @@ pub enum Def {
Method(DefId), Method(DefId),
AssociatedConst(DefId), AssociatedConst(DefId),
Local(ast::NodeId), Local(Id),
Upvar(ast::NodeId, // `NodeId` of closed over local Upvar(Id, // `HirId` of closed over local
usize, // index in the `freevars` list of the closure usize, // index in the `freevars` list of the closure
ast::NodeId), // expr node that creates the closure ast::NodeId), // expr node that creates the closure
Label(ast::NodeId), Label(ast::NodeId),
@ -108,22 +110,22 @@ pub enum Def {
/// ``` /// ```
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct PathResolution { pub struct PathResolution {
base_def: Def, base_def: Def<NodeId>,
unresolved_segments: usize, unresolved_segments: usize,
} }
impl PathResolution { impl PathResolution {
pub fn new(def: Def) -> Self { pub fn new(def: Def<NodeId>) -> Self {
PathResolution { base_def: def, unresolved_segments: 0 } PathResolution { base_def: def, unresolved_segments: 0 }
} }
pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self { pub fn with_unresolved_segments(def: Def<NodeId>, mut unresolved_segments: usize) -> Self {
if def == Def::Err { unresolved_segments = 0 } if def == Def::Err { unresolved_segments = 0 }
PathResolution { base_def: def, unresolved_segments: unresolved_segments } PathResolution { base_def: def, unresolved_segments: unresolved_segments }
} }
#[inline] #[inline]
pub fn base_def(&self) -> Def { pub fn base_def(&self) -> Def<NodeId> {
self.base_def self.base_def
} }
@ -215,18 +217,18 @@ pub type DefMap = NodeMap<PathResolution>;
/// This is the replacement export map. It maps a module to all of the exports /// This is the replacement export map. It maps a module to all of the exports
/// within. /// within.
pub type ExportMap = DefIdMap<Vec<Export>>; pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
/// Map used to track the `use` statements within a scope, matching it with all the items in every /// Map used to track the `use` statements within a scope, matching it with all the items in every
/// namespace. /// namespace.
pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>; pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)] #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct Export { pub struct Export<Id> {
/// The name of the target. /// The name of the target.
pub ident: ast::Ident, pub ident: ast::Ident,
/// The definition of the target. /// The definition of the target.
pub def: Def, pub def: Def<Id>,
/// The span of the target definition. /// The span of the target definition.
pub span: Span, pub span: Span,
/// The visibility of the export. /// The visibility of the export.
@ -234,6 +236,17 @@ pub struct Export {
pub vis: ty::Visibility, pub vis: ty::Visibility,
} }
impl<Id> Export<Id> {
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
Export {
ident: self.ident,
def: self.def.map_id(map),
span: self.span,
vis: self.vis,
}
}
}
impl CtorKind { impl CtorKind {
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind { pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
match *vdata { match *vdata {
@ -264,9 +277,12 @@ impl NonMacroAttrKind {
} }
} }
impl Def { impl<Id> Def<Id> {
/// Return the `DefId` of this `Def` if it has an id, else panic. /// Return the `DefId` of this `Def` if it has an id, else panic.
pub fn def_id(&self) -> DefId { pub fn def_id(&self) -> DefId
where
Id: Debug,
{
self.opt_def_id().unwrap_or_else(|| { self.opt_def_id().unwrap_or_else(|| {
bug!("attempted .def_id() on invalid def: {:?}", self) bug!("attempted .def_id() on invalid def: {:?}", self)
}) })
@ -358,4 +374,43 @@ impl Def {
_ => "a", _ => "a",
} }
} }
pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Def<R> {
match self {
Def::Fn(id) => Def::Fn(id),
Def::Mod(id) => Def::Mod(id),
Def::Static(id, is_mutbl) => Def::Static(id, is_mutbl),
Def::Enum(id) => Def::Enum(id),
Def::Variant(id) => Def::Variant(id),
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
Def::Struct(id) => Def::Struct(id),
Def::Existential(id) => Def::Existential(id),
Def::TyAlias(id) => Def::TyAlias(id),
Def::TraitAlias(id) => Def::TraitAlias(id),
Def::AssociatedTy(id) => Def::AssociatedTy(id),
Def::AssociatedExistential(id) => Def::AssociatedExistential(id),
Def::SelfCtor(id) => Def::SelfCtor(id),
Def::Union(id) => Def::Union(id),
Def::Trait(id) => Def::Trait(id),
Def::ForeignTy(id) => Def::ForeignTy(id),
Def::Method(id) => Def::Method(id),
Def::Const(id) => Def::Const(id),
Def::AssociatedConst(id) => Def::AssociatedConst(id),
Def::TyParam(id) => Def::TyParam(id),
Def::ConstParam(id) => Def::ConstParam(id),
Def::PrimTy(id) => Def::PrimTy(id),
Def::Local(id) => Def::Local(map(id)),
Def::Upvar(id, index, closure) => Def::Upvar(
map(id),
index,
closure
),
Def::Label(id) => Def::Label(id),
Def::SelfTy(a, b) => Def::SelfTy(a, b),
Def::Macro(id, macro_kind) => Def::Macro(id, macro_kind),
Def::ToolMod => Def::ToolMod,
Def::NonMacroAttr(attr_kind) => Def::NonMacroAttr(attr_kind),
Def::Err => Def::Err,
}
}
} }

View file

@ -401,6 +401,7 @@ impl<'a> LoweringContext<'a> {
/// declared for every type and trait definition. /// declared for every type and trait definition.
struct MiscCollector<'lcx, 'interner: 'lcx> { struct MiscCollector<'lcx, 'interner: 'lcx> {
lctx: &'lcx mut LoweringContext<'interner>, lctx: &'lcx mut LoweringContext<'interner>,
hir_id_owner: Option<NodeId>,
} }
impl MiscCollector<'_, '_> { impl MiscCollector<'_, '_> {
@ -432,9 +433,34 @@ impl<'a> LoweringContext<'a> {
} }
} }
} }
fn with_hir_id_owner<F, T>(&mut self, owner: Option<NodeId>, f: F) -> T
where
F: FnOnce(&mut Self) -> T,
{
let old = mem::replace(&mut self.hir_id_owner, owner);
let r = f(self);
self.hir_id_owner = old;
r
}
} }
impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> { impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
fn visit_pat(&mut self, p: &'lcx Pat) {
match p.node {
// Doesn't generate a Hir node
PatKind::Paren(..) => {},
_ => {
if let Some(owner) = self.hir_id_owner {
self.lctx.lower_node_id_with_owner(p.id, owner);
}
}
};
visit::walk_pat(self, p)
}
fn visit_item(&mut self, item: &'lcx Item) { fn visit_item(&mut self, item: &'lcx Item) {
let hir_id = self.lctx.allocate_hir_id_counter(item.id).hir_id; let hir_id = self.lctx.allocate_hir_id_counter(item.id).hir_id;
@ -461,17 +487,63 @@ impl<'a> LoweringContext<'a> {
} }
_ => {} _ => {}
} }
visit::walk_item(self, item);
self.with_hir_id_owner(Some(item.id), |this| {
visit::walk_item(this, item);
});
} }
fn visit_trait_item(&mut self, item: &'lcx TraitItem) { fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
self.lctx.allocate_hir_id_counter(item.id); self.lctx.allocate_hir_id_counter(item.id);
visit::walk_trait_item(self, item);
match item.node {
TraitItemKind::Method(_, None) => {
// Ignore patterns in trait methods without bodies
self.with_hir_id_owner(None, |this| {
visit::walk_trait_item(this, item)
});
}
_ => self.with_hir_id_owner(Some(item.id), |this| {
visit::walk_trait_item(this, item);
})
}
} }
fn visit_impl_item(&mut self, item: &'lcx ImplItem) { fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
self.lctx.allocate_hir_id_counter(item.id); self.lctx.allocate_hir_id_counter(item.id);
visit::walk_impl_item(self, item); self.with_hir_id_owner(Some(item.id), |this| {
visit::walk_impl_item(this, item);
});
}
fn visit_foreign_item(&mut self, i: &'lcx ForeignItem) {
// Ignore patterns in foreign items
self.with_hir_id_owner(None, |this| {
visit::walk_foreign_item(this, i)
});
}
fn visit_ty(&mut self, t: &'lcx Ty) {
match t.node {
// Mirrors the case in visit::walk_ty
TyKind::BareFn(ref f) => {
walk_list!(
self,
visit_generic_param,
&f.generic_params
);
// Mirrors visit::walk_fn_decl
for argument in &f.decl.inputs {
// We don't lower the ids of argument patterns
self.with_hir_id_owner(None, |this| {
this.visit_pat(&argument.pat);
});
self.visit_ty(&argument.ty)
}
self.visit_fn_ret_ty(&f.decl.output)
}
_ => visit::walk_ty(self, t),
}
} }
} }
@ -565,7 +637,7 @@ impl<'a> LoweringContext<'a> {
self.lower_node_id(CRATE_NODE_ID); self.lower_node_id(CRATE_NODE_ID);
debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == hir::CRATE_HIR_ID); debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == hir::CRATE_HIR_ID);
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c); visit::walk_crate(&mut MiscCollector { lctx: &mut self, hir_id_owner: None }, c);
visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c); visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);
let module = self.lower_mod(&c.module); let module = self.lower_mod(&c.module);
@ -729,7 +801,15 @@ impl<'a> LoweringContext<'a> {
self.lower_node_id(self.sess.next_node_id()) self.lower_node_id(self.sess.next_node_id())
} }
fn expect_full_def(&mut self, id: NodeId) -> Def { fn lower_def(&mut self, def: Def<NodeId>) -> Def {
def.map_id(|id| {
self.lower_node_id_generic(id, |_| {
panic!("expected node_id to be lowered already for def {:#?}", def)
}).hir_id
})
}
fn expect_full_def(&mut self, id: NodeId) -> Def<NodeId> {
self.resolver.get_resolution(id).map_or(Def::Err, |pr| { self.resolver.get_resolution(id).map_or(Def::Err, |pr| {
if pr.unresolved_segments() != 0 { if pr.unresolved_segments() != 0 {
bug!("path not fully resolved: {:?}", pr); bug!("path not fully resolved: {:?}", pr);
@ -738,7 +818,7 @@ impl<'a> LoweringContext<'a> {
}) })
} }
fn expect_full_def_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Def> { fn expect_full_def_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Def<NodeId>> {
self.resolver.get_import(id).present_items().map(|pr| { self.resolver.get_import(id).present_items().map(|pr| {
if pr.unresolved_segments() != 0 { if pr.unresolved_segments() != 0 {
bug!("path not fully resolved: {:?}", pr); bug!("path not fully resolved: {:?}", pr);
@ -1324,14 +1404,20 @@ impl<'a> LoweringContext<'a> {
} }
return ty; return ty;
} }
TyKind::ImplicitSelf => hir::TyKind::Path(hir::QPath::Resolved( TyKind::ImplicitSelf => {
let def = self.expect_full_def(t.id);
let def = self.lower_def(def);
hir::TyKind::Path(hir::QPath::Resolved(
None, None,
P(hir::Path { P(hir::Path {
def: self.expect_full_def(t.id), def,
segments: hir_vec![hir::PathSegment::from_ident(keywords::SelfUpper.ident())], segments: hir_vec![hir::PathSegment::from_ident(
keywords::SelfUpper.ident()
)],
span: t.span, span: t.span,
}), }),
)), ))
},
TyKind::Array(ref ty, ref length) => { TyKind::Array(ref ty, ref length) => {
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_anon_const(length)) hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_anon_const(length))
} }
@ -1755,7 +1841,7 @@ impl<'a> LoweringContext<'a> {
let proj_start = p.segments.len() - resolution.unresolved_segments(); let proj_start = p.segments.len() - resolution.unresolved_segments();
let path = P(hir::Path { let path = P(hir::Path {
def: resolution.base_def(), def: self.lower_def(resolution.base_def()),
segments: p.segments[..proj_start] segments: p.segments[..proj_start]
.iter() .iter()
.enumerate() .enumerate()
@ -1931,6 +2017,7 @@ impl<'a> LoweringContext<'a> {
fn lower_path(&mut self, id: NodeId, p: &Path, param_mode: ParamMode) -> hir::Path { fn lower_path(&mut self, id: NodeId, p: &Path, param_mode: ParamMode) -> hir::Path {
let def = self.expect_full_def(id); let def = self.expect_full_def(id);
let def = self.lower_def(def);
self.lower_path_extra(def, p, param_mode, None) self.lower_path_extra(def, p, param_mode, None)
} }
@ -2042,7 +2129,7 @@ impl<'a> LoweringContext<'a> {
hir::PathSegment::new( hir::PathSegment::new(
segment.ident, segment.ident,
Some(id.hir_id), Some(id.hir_id),
Some(def), Some(self.lower_def(def)),
generic_args, generic_args,
infer_types, infer_types,
) )
@ -3174,7 +3261,7 @@ impl<'a> LoweringContext<'a> {
let mut defs = self.expect_full_def_from_use(id); let mut defs = self.expect_full_def_from_use(id);
// We want to return *something* from this function, so hold onto the first item // We want to return *something* from this function, so hold onto the first item
// for later. // for later.
let ret_def = defs.next().unwrap_or(Def::Err); let ret_def = self.lower_def(defs.next().unwrap_or(Def::Err));
// Here, we are looping over namespaces, if they exist for the definition // Here, we are looping over namespaces, if they exist for the definition
// being imported. We only handle type and value namespaces because we // being imported. We only handle type and value namespaces because we
@ -3192,6 +3279,7 @@ impl<'a> LoweringContext<'a> {
self.with_hir_id_owner(new_node_id, |this| { self.with_hir_id_owner(new_node_id, |this| {
let new_id = this.lower_node_id(new_node_id); let new_id = this.lower_node_id(new_node_id);
let def = this.lower_def(def);
let path = let path =
this.lower_path_extra(def, &path, ParamMode::Explicit, None); this.lower_path_extra(def, &path, ParamMode::Explicit, None);
let item = hir::ItemKind::Use(P(path), hir::UseKind::Single); let item = hir::ItemKind::Use(P(path), hir::UseKind::Single);
@ -3347,6 +3435,7 @@ impl<'a> LoweringContext<'a> {
} }
let def = self.expect_full_def_from_use(id).next().unwrap_or(Def::Err); let def = self.expect_full_def_from_use(id).next().unwrap_or(Def::Err);
let def = self.lower_def(def);
let path = P(self.lower_path_extra(def, &prefix, ParamMode::Explicit, None)); let path = P(self.lower_path_extra(def, &prefix, ParamMode::Explicit, None));
hir::ItemKind::Use(path, hir::UseKind::ListStem) hir::ItemKind::Use(path, hir::UseKind::ListStem)
} }
@ -3780,7 +3869,7 @@ impl<'a> LoweringContext<'a> {
None, None,
P(hir::Path { P(hir::Path {
span: ident.span, span: ident.span,
def, def: self.lower_def(def),
segments: hir_vec![hir::PathSegment::from_ident(ident)], segments: hir_vec![hir::PathSegment::from_ident(ident)],
}), }),
)), )),
@ -4435,7 +4524,7 @@ impl<'a> LoweringContext<'a> {
let iter = self.str_to_ident("iter"); let iter = self.str_to_ident("iter");
let next_ident = self.str_to_ident("__next"); let next_ident = self.str_to_ident("__next");
let (next_pat, next_pat_nid) = self.pat_ident_binding_mode( let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
desugared_span, desugared_span,
next_ident, next_ident,
hir::BindingAnnotation::Mutable, hir::BindingAnnotation::Mutable,
@ -4444,9 +4533,9 @@ impl<'a> LoweringContext<'a> {
// `::std::option::Option::Some(val) => next = val` // `::std::option::Option::Some(val) => next = val`
let pat_arm = { let pat_arm = {
let val_ident = self.str_to_ident("val"); let val_ident = self.str_to_ident("val");
let (val_pat, val_pat_nid) = self.pat_ident(pat.span, val_ident); let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_nid)); let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_hid));
let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_nid)); let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_hid));
let assign = P(self.expr( let assign = P(self.expr(
pat.span, pat.span,
hir::ExprKind::Assign(next_expr, val_expr), hir::ExprKind::Assign(next_expr, val_expr),
@ -4497,7 +4586,7 @@ impl<'a> LoweringContext<'a> {
span: head_sp, span: head_sp,
}; };
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat_nid)); let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat_hid));
// `let mut __next` // `let mut __next`
let next_let = self.stmt_let_pat( let next_let = self.stmt_let_pat(
@ -4790,6 +4879,7 @@ impl<'a> LoweringContext<'a> {
self.lower_node_id(id) self.lower_node_id(id)
}; };
let def = self.expect_full_def(id); let def = self.expect_full_def(id);
let def = self.lower_def(def);
hir::VisibilityKind::Restricted { hir::VisibilityKind::Restricted {
path: P(self.lower_path_extra( path: P(self.lower_path_extra(
def, def,
@ -4891,7 +4981,7 @@ impl<'a> LoweringContext<'a> {
self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new()) self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
} }
fn expr_ident(&mut self, span: Span, ident: Ident, binding: NodeId) -> hir::Expr { fn expr_ident(&mut self, span: Span, ident: Ident, binding: hir::HirId) -> hir::Expr {
self.expr_ident_with_attrs(span, ident, binding, ThinVec::new()) self.expr_ident_with_attrs(span, ident, binding, ThinVec::new())
} }
@ -4899,7 +4989,7 @@ impl<'a> LoweringContext<'a> {
&mut self, &mut self,
span: Span, span: Span,
ident: Ident, ident: Ident,
binding: NodeId, binding: hir::HirId,
attrs: ThinVec<Attribute>, attrs: ThinVec<Attribute>,
) -> hir::Expr { ) -> hir::Expr {
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved( let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
@ -4994,8 +5084,8 @@ impl<'a> LoweringContext<'a> {
mutbl: bool, mutbl: bool,
ident: Ident, ident: Ident,
ex: P<hir::Expr>, ex: P<hir::Expr>,
) -> (hir::Stmt, NodeId) { ) -> (hir::Stmt, hir::HirId) {
let (pat, pat_nid) = if mutbl { let (pat, pat_hid) = if mutbl {
self.pat_ident_binding_mode(sp, ident, hir::BindingAnnotation::Mutable) self.pat_ident_binding_mode(sp, ident, hir::BindingAnnotation::Mutable)
} else { } else {
self.pat_ident(sp, ident) self.pat_ident(sp, ident)
@ -5003,7 +5093,7 @@ impl<'a> LoweringContext<'a> {
( (
self.stmt_let_pat(sp, Some(ex), pat, hir::LocalSource::Normal), self.stmt_let_pat(sp, Some(ex), pat, hir::LocalSource::Normal),
pat_nid, pat_hid,
) )
} }
@ -5061,7 +5151,7 @@ impl<'a> LoweringContext<'a> {
self.pat(span, pt) self.pat(span, pt)
} }
fn pat_ident(&mut self, span: Span, ident: Ident) -> (P<hir::Pat>, NodeId) { fn pat_ident(&mut self, span: Span, ident: Ident) -> (P<hir::Pat>, hir::HirId) {
self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated) self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated)
} }
@ -5070,8 +5160,8 @@ impl<'a> LoweringContext<'a> {
span: Span, span: Span,
ident: Ident, ident: Ident,
bm: hir::BindingAnnotation, bm: hir::BindingAnnotation,
) -> (P<hir::Pat>, NodeId) { ) -> (P<hir::Pat>, hir::HirId) {
let LoweredNodeId { node_id, hir_id } = self.next_id(); let LoweredNodeId { node_id: _, hir_id } = self.next_id();
( (
P(hir::Pat { P(hir::Pat {
@ -5079,7 +5169,7 @@ impl<'a> LoweringContext<'a> {
node: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None), node: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
span, span,
}), }),
node_id hir_id
) )
} }

View file

@ -393,7 +393,7 @@ impl<'hir> Map<'hir> {
Node::Block(_) | Node::Block(_) |
Node::Crate => None, Node::Crate => None,
Node::Local(local) => { Node::Local(local) => {
Some(Def::Local(self.hir_to_node_id(local.hir_id))) Some(Def::Local(local.hir_id))
} }
Node::MacroDef(macro_def) => { Node::MacroDef(macro_def) => {
Some(Def::Macro(self.local_def_id_from_hir_id(macro_def.hir_id), Some(Def::Macro(self.local_def_id_from_hir_id(macro_def.hir_id),
@ -402,8 +402,7 @@ impl<'hir> Map<'hir> {
Node::GenericParam(param) => { Node::GenericParam(param) => {
Some(match param.kind { Some(match param.kind {
GenericParamKind::Lifetime { .. } => { GenericParamKind::Lifetime { .. } => {
let node_id = self.hir_to_node_id(param.hir_id); Def::Local(param.hir_id)
Def::Local(node_id)
}, },
GenericParamKind::Type { .. } => Def::TyParam( GenericParamKind::Type { .. } => Def::TyParam(
self.local_def_id_from_hir_id(param.hir_id)), self.local_def_id_from_hir_id(param.hir_id)),

View file

@ -2424,16 +2424,23 @@ impl ForeignItemKind {
/// A free variable referred to in a function. /// A free variable referred to in a function.
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)] #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
pub struct Freevar { pub struct Freevar<Id = HirId> {
/// The variable being accessed free. /// The variable being accessed free.
pub def: Def, pub def: def::Def<Id>,
// First span where it is accessed (there can be multiple). // First span where it is accessed (there can be multiple).
pub span: Span pub span: Span
} }
impl Freevar { impl<Id: fmt::Debug + Copy> Freevar<Id> {
pub fn var_id(&self) -> NodeId { pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Freevar<R> {
Freevar {
def: self.def.map_id(map),
span: self.span,
}
}
pub fn var_id(&self) -> Id {
match self.def { match self.def {
Def::Local(id) | Def::Upvar(id, ..) => id, Def::Local(id) | Def::Upvar(id, ..) => id,
_ => bug!("Freevar::var_id: bad def ({:?})", self.def) _ => bug!("Freevar::var_id: bad def ({:?})", self.def)
@ -2441,7 +2448,7 @@ impl Freevar {
} }
} }
pub type FreevarMap = NodeMap<Vec<Freevar>>; pub type FreevarMap = NodeMap<Vec<Freevar<ast::NodeId>>>;
pub type CaptureModeMap = NodeMap<CaptureClause>; pub type CaptureModeMap = NodeMap<CaptureClause>;

View file

@ -863,7 +863,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// Each match binding is effectively an assignment to the // Each match binding is effectively an assignment to the
// binding being produced. // binding being produced.
let def = Def::Local(mc.tcx.hir().hir_to_node_id(canonical_id)); let def = Def::Local(canonical_id);
if let Ok(ref binding_cmt) = mc.cat_def(pat.hir_id, pat.span, pat_ty, def) { if let Ok(ref binding_cmt) = mc.cat_def(pat.hir_id, pat.span, pat_ty, def) {
delegate.mutate(pat.hir_id, pat.span, binding_cmt, MutateMode::Init); delegate.mutate(pat.hir_id, pat.span, binding_cmt, MutateMode::Init);
} }
@ -930,7 +930,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id); let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id);
self.tcx().with_freevars(closure_expr.hir_id, |freevars| { self.tcx().with_freevars(closure_expr.hir_id, |freevars| {
for freevar in freevars { for freevar in freevars {
let var_hir_id = self.tcx().hir().node_to_hir_id(freevar.var_id()); let var_hir_id = freevar.var_id();
let upvar_id = ty::UpvarId { let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { hir_id: var_hir_id }, var_path: ty::UpvarPath { hir_id: var_hir_id },
closure_expr_id: closure_def_id.to_local(), closure_expr_id: closure_def_id.to_local(),
@ -967,7 +967,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
-> mc::McResult<mc::cmt_<'tcx>> { -> mc::McResult<mc::cmt_<'tcx>> {
// Create the cmt for the variable being borrowed, from the // Create the cmt for the variable being borrowed, from the
// caller's perspective // caller's perspective
let var_hir_id = self.tcx().hir().node_to_hir_id(upvar.var_id()); let var_hir_id = upvar.var_id();
let var_ty = self.mc.node_ty(var_hir_id)?; let var_ty = self.mc.node_ty(var_hir_id)?;
self.mc.cat_def(closure_hir_id, closure_span, var_ty, upvar.def) self.mc.cat_def(closure_hir_id, closure_span, var_ty, upvar.def)
} }

View file

@ -487,8 +487,7 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
call_caps.extend(freevars.iter().filter_map(|fv| { call_caps.extend(freevars.iter().filter_map(|fv| {
if let Def::Local(rv) = fv.def { if let Def::Local(rv) = fv.def {
let fv_ln = ir.add_live_node(FreeVarNode(fv.span)); let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
let var_hid = ir.tcx.hir().node_to_hir_id(rv); Some(CaptureInfo { ln: fv_ln, var_hid: rv })
Some(CaptureInfo { ln: fv_ln, var_hid })
} else { } else {
None None
} }
@ -1347,7 +1346,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn access_path(&mut self, hir_id: HirId, path: &hir::Path, succ: LiveNode, acc: u32) fn access_path(&mut self, hir_id: HirId, path: &hir::Path, succ: LiveNode, acc: u32)
-> LiveNode { -> LiveNode {
match path.def { match path.def {
Def::Local(nid) => { Def::Local(hid) => {
let nid = self.ir.tcx.hir().hir_to_node_id(hid);
self.access_var(hir_id, nid, succ, acc, path.span) self.access_var(hir_id, nid, succ, acc, path.span)
} }
_ => succ _ => succ
@ -1539,13 +1539,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn check_place(&mut self, expr: &'tcx Expr) { fn check_place(&mut self, expr: &'tcx Expr) {
match expr.node { match expr.node {
hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => { hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => {
if let Def::Local(nid) = path.def { if let Def::Local(var_hid) = path.def {
// Assignment to an immutable variable or argument: only legal // Assignment to an immutable variable or argument: only legal
// if there is no later assignment. If this local is actually // if there is no later assignment. If this local is actually
// mutable, then check for a reassignment to flag the mutability // mutable, then check for a reassignment to flag the mutability
// as being used. // as being used.
let ln = self.live_node(expr.hir_id, expr.span); let ln = self.live_node(expr.hir_id, expr.span);
let var_hid = self.ir.tcx.hir().node_to_hir_id(nid);
let var = self.variable(var_hid, expr.span); let var = self.variable(var_hid, expr.span);
self.warn_about_dead_assign(expr.span, expr.hir_id, ln, var); self.warn_about_dead_assign(expr.span, expr.hir_id, ln, var);
} }

View file

@ -734,15 +734,17 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
} }
Def::Upvar(var_id, _, fn_node_id) => { Def::Upvar(var_id, _, fn_node_id) => {
self.cat_upvar(hir_id, span, var_id, fn_node_id) let var_nid = self.tcx.hir().hir_to_node_id(var_id);
self.cat_upvar(hir_id, span, var_nid, fn_node_id)
} }
Def::Local(vid) => { Def::Local(vid) => {
let vnid = self.tcx.hir().hir_to_node_id(vid);
Ok(cmt_ { Ok(cmt_ {
hir_id, hir_id,
span, span,
cat: Categorization::Local(self.tcx.hir().node_to_hir_id(vid)), cat: Categorization::Local(vid),
mutbl: MutabilityCategory::from_local(self.tcx, self.tables, vid), mutbl: MutabilityCategory::from_local(self.tcx, self.tables, vnid),
ty: expr_ty, ty: expr_ty,
note: NoteNone note: NoteNone
}) })

View file

@ -103,8 +103,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
}; };
match def { match def {
Some(Def::Local(node_id)) | Some(Def::Upvar(node_id, ..)) => { Some(Def::Local(hir_id)) | Some(Def::Upvar(hir_id, ..)) => {
let hir_id = self.tcx.hir().node_to_hir_id(node_id);
self.reachable_symbols.insert(hir_id); self.reachable_symbols.insert(hir_id);
} }
Some(def) => { Some(def) => {

View file

@ -2455,7 +2455,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.with_freevars(hir_id, |freevars| { tcx.with_freevars(hir_id, |freevars| {
for (freevar, place) in freevars.iter().zip(places) { for (freevar, place) in freevars.iter().zip(places) {
let var_name = tcx.hir().name(freevar.var_id()); let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
struct_fmt.field(&var_name.as_str(), place); struct_fmt.field(&var_name.as_str(), place);
} }
}); });
@ -2474,7 +2474,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.with_freevars(hir_id, |freevars| { tcx.with_freevars(hir_id, |freevars| {
for (freevar, place) in freevars.iter().zip(places) { for (freevar, place) in freevars.iter().zip(places) {
let var_name = tcx.hir().name(freevar.var_id()); let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
struct_fmt.field(&var_name.as_str(), place); struct_fmt.field(&var_name.as_str(), place);
} }
struct_fmt.field("$state", &places[freevars.len()]); struct_fmt.field("$state", &places[freevars.len()]);

View file

@ -641,7 +641,7 @@ rustc_queries! {
} }
Other { Other {
query module_exports(_: DefId) -> Option<Lrc<Vec<Export>>> { query module_exports(_: DefId) -> Option<Lrc<Vec<Export<hir::HirId>>>> {
eval_always eval_always
} }
} }
@ -781,7 +781,7 @@ rustc_queries! {
eval_always eval_always
desc { "fetching what a crate is named" } desc { "fetching what a crate is named" }
} }
query item_children(_: DefId) -> Lrc<Vec<Export>> {} query item_children(_: DefId) -> Lrc<Vec<Export<hir::HirId>>> {}
query extern_mod_stmt_cnum(_: DefId) -> Option<CrateNum> {} query extern_mod_stmt_cnum(_: DefId) -> Option<CrateNum> {}
query get_lib_features(_: CrateNum) -> Lrc<LibFeatures> { query get_lib_features(_: CrateNum) -> Lrc<LibFeatures> {

View file

@ -1024,7 +1024,7 @@ pub struct GlobalCtxt<'tcx> {
Lrc<StableVec<TraitCandidate>>>>>, Lrc<StableVec<TraitCandidate>>>>>,
/// Export map produced by name resolution. /// Export map produced by name resolution.
export_map: FxHashMap<DefId, Lrc<Vec<Export>>>, export_map: FxHashMap<DefId, Lrc<Vec<Export<hir::HirId>>>>,
hir_map: hir_map::Map<'tcx>, hir_map: hir_map::Map<'tcx>,
@ -1271,10 +1271,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
types: common_types, types: common_types,
trait_map, trait_map,
export_map: resolutions.export_map.into_iter().map(|(k, v)| { export_map: resolutions.export_map.into_iter().map(|(k, v)| {
(k, Lrc::new(v)) let exports: Vec<_> = v.into_iter().map(|e| {
e.map_id(|id| hir.node_to_hir_id(id))
}).collect();
(k, Lrc::new(exports))
}).collect(), }).collect(),
freevars: resolutions.freevars.into_iter().map(|(k, v)| { freevars: resolutions.freevars.into_iter().map(|(k, v)| {
(hir.local_def_id(k), Lrc::new(v)) let vars: Vec<_> = v.into_iter().map(|e| {
e.map_id(|id| hir.node_to_hir_id(id))
}).collect();
(hir.local_def_id(k), Lrc::new(vars))
}).collect(), }).collect(),
maybe_unused_trait_imports: maybe_unused_trait_imports:
resolutions.maybe_unused_trait_imports resolutions.maybe_unused_trait_imports

View file

@ -123,7 +123,7 @@ pub struct Resolutions {
pub trait_map: TraitMap, pub trait_map: TraitMap,
pub maybe_unused_trait_imports: NodeSet, pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>, pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
pub export_map: ExportMap, pub export_map: ExportMap<NodeId>,
pub glob_map: GlobMap, pub glob_map: GlobMap,
/// Extern prelude entries. The value is `true` if the entry was introduced /// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in. /// via `extern crate` item and not `--extern` option or compiler built-in.

View file

@ -592,7 +592,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
p!( p!(
write("{}{}:", write("{}{}:",
sep, sep,
self.tcx().hir().name(freevar.var_id())), self.tcx().hir().name_by_hir_id(freevar.var_id())),
print(upvar_ty)); print(upvar_ty));
sep = ", "; sep = ", ";
} }
@ -635,7 +635,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
p!( p!(
write("{}{}:", write("{}{}:",
sep, sep,
self.tcx().hir().name(freevar.var_id())), self.tcx().hir().name_by_hir_id(freevar.var_id())),
print(upvar_ty)); print(upvar_ty));
sep = ", "; sep = ", ";
} }

View file

@ -11,6 +11,7 @@ use rustc::middle::cstore::{CrateStore, DepKind,
use rustc::middle::exported_symbols::ExportedSymbol; use rustc::middle::exported_symbols::ExportedSymbol;
use rustc::middle::stability::DeprecationEntry; use rustc::middle::stability::DeprecationEntry;
use rustc::hir::def; use rustc::hir::def;
use rustc::hir;
use rustc::session::{CrateDisambiguator, Session}; use rustc::session::{CrateDisambiguator, Session};
use rustc::ty::{self, TyCtxt}; use rustc::ty::{self, TyCtxt};
use rustc::ty::query::Providers; use rustc::ty::query::Providers;
@ -347,7 +348,7 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
{ {
let visible_parent_map = &mut visible_parent_map; let visible_parent_map = &mut visible_parent_map;
let mut add_child = |bfs_queue: &mut VecDeque<_>, let mut add_child = |bfs_queue: &mut VecDeque<_>,
child: &def::Export, child: &def::Export<hir::HirId>,
parent: DefId| { parent: DefId| {
if child.vis != ty::Visibility::Public { if child.vis != ty::Visibility::Public {
return; return;
@ -415,7 +416,11 @@ impl cstore::CStore {
self.get_crate_data(def.krate).get_item_attrs(def.index, sess) self.get_crate_data(def.krate).get_item_attrs(def.index, sess)
} }
pub fn item_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<def::Export> { pub fn item_children_untracked(
&self,
def_id: DefId,
sess: &Session
) -> Vec<def::Export<hir::HirId>> {
let mut result = vec![]; let mut result = vec![];
self.get_crate_data(def_id.krate) self.get_crate_data(def_id.krate)
.each_child_of_item(def_id.index, |child| result.push(child), sess); .each_child_of_item(def_id.index, |child| result.push(child), sess);

View file

@ -734,7 +734,7 @@ impl<'a, 'tcx> CrateMetadata {
/// Iterates over each child of the given item. /// Iterates over each child of the given item.
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session) pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
where F: FnMut(def::Export) where F: FnMut(def::Export<hir::HirId>)
{ {
if let Some(ref proc_macros) = self.proc_macros { if let Some(ref proc_macros) = self.proc_macros {
/* If we are loading as a proc macro, we want to return the view of this crate /* If we are loading as a proc macro, we want to return the view of this crate

View file

@ -423,7 +423,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for RenderedConst {
#[derive(RustcEncodable, RustcDecodable)] #[derive(RustcEncodable, RustcDecodable)]
pub struct ModData { pub struct ModData {
pub reexports: LazySeq<def::Export>, pub reexports: LazySeq<def::Export<hir::HirId>>,
} }
impl_stable_hash_for!(struct ModData { reexports }); impl_stable_hash_for!(struct ModData { reexports });

View file

@ -1824,7 +1824,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
.tcx .tcx
.with_freevars(hir_id, |fv| fv[field.index()]); .with_freevars(hir_id, |fv| fv[field.index()]);
self.infcx.tcx.hir().name(freevar.var_id()).to_string() self.infcx.tcx.hir().name_by_hir_id(freevar.var_id()).to_string()
} }
_ => { _ => {
// Might need a revision when the fields in trait RFC is implemented // Might need a revision when the fields in trait RFC is implemented

View file

@ -986,14 +986,13 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id); let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
match def { match def {
Def::Local(id) => ExprKind::VarRef { id: cx.tcx.hir().node_to_hir_id(id) }, Def::Local(id) => ExprKind::VarRef { id },
Def::Upvar(var_id, index, closure_expr_id) => { Def::Upvar(var_hir_id, index, closure_expr_id) => {
debug!("convert_var(upvar({:?}, {:?}, {:?}))", debug!("convert_var(upvar({:?}, {:?}, {:?}))",
var_id, var_hir_id,
index, index,
closure_expr_id); closure_expr_id);
let var_hir_id = cx.tcx.hir().node_to_hir_id(var_id);
let var_ty = cx.tables().node_type(var_hir_id); let var_ty = cx.tables().node_type(var_hir_id);
// FIXME free regions in closures are not right // FIXME free regions in closures are not right
@ -1195,7 +1194,7 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
freevar: &hir::Freevar, freevar: &hir::Freevar,
freevar_ty: Ty<'tcx>) freevar_ty: Ty<'tcx>)
-> ExprRef<'tcx> { -> ExprRef<'tcx> {
let var_hir_id = cx.tcx.hir().node_to_hir_id(freevar.var_id()); let var_hir_id = freevar.var_id();
let upvar_id = ty::UpvarId { let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { hir_id: var_hir_id }, var_path: ty::UpvarPath { hir_id: var_hir_id },
closure_expr_id: cx.tcx.hir().local_def_id_from_hir_id(closure_expr.hir_id).to_local(), closure_expr_id: cx.tcx.hir().local_def_id_from_hir_id(closure_expr.hir_id).to_local(),

View file

@ -12,7 +12,7 @@ use crate::Namespace::{self, TypeNS, ValueNS, MacroNS};
use crate::{resolve_error, resolve_struct_error, ResolutionError}; use crate::{resolve_error, resolve_struct_error, ResolutionError};
use rustc::bug; use rustc::bug;
use rustc::hir::def::*; use rustc::hir::def::{self, *};
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId}; use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::ty; use rustc::ty;
use rustc::middle::cstore::CrateStore; use rustc::middle::cstore::CrateStore;
@ -44,6 +44,8 @@ use syntax_pos::{Span, DUMMY_SP};
use log::debug; use log::debug;
type Def = def::Def<NodeId>;
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) { impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> { fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding { arenas.alloc_name_binding(NameBinding {
@ -641,7 +643,11 @@ impl<'a> Resolver<'a> {
} }
/// Builds the reduced graph for a single item in an external crate. /// Builds the reduced graph for a single item in an external crate.
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, child: Export) { fn build_reduced_graph_for_external_crate_def(
&mut self,
parent: Module<'a>,
child: Export<ast::NodeId>,
) {
let Export { ident, def, vis, span } = child; let Export { ident, def, vis, span } = child;
// FIXME: We shouldn't create the gensym here, it should come from metadata, // FIXME: We shouldn't create the gensym here, it should come from metadata,
// but metadata cannot encode gensyms currently, so we create it here. // but metadata cannot encode gensyms currently, so we create it here.
@ -684,13 +690,14 @@ impl<'a> Resolver<'a> {
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion)); self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
for child in self.cstore.item_children_untracked(def_id, self.session) { for child in self.cstore.item_children_untracked(def_id, self.session) {
let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS }; let def = child.def.map_id(|_| panic!("unexpected id"));
let ns = if let Def::AssociatedTy(..) = def { TypeNS } else { ValueNS };
self.define(module, child.ident, ns, self.define(module, child.ident, ns,
(child.def, ty::Visibility::Public, DUMMY_SP, expansion)); (def, ty::Visibility::Public, DUMMY_SP, expansion));
if self.cstore.associated_item_cloned_untracked(child.def.def_id()) if self.cstore.associated_item_cloned_untracked(child.def.def_id())
.method_has_self_argument { .method_has_self_argument {
self.has_self.insert(child.def.def_id()); self.has_self.insert(def.def_id());
} }
} }
module.populated.set(true); module.populated.set(true);
@ -777,6 +784,7 @@ impl<'a> Resolver<'a> {
if module.populated.get() { return } if module.populated.get() { return }
let def_id = module.def_id().unwrap(); let def_id = module.def_id().unwrap();
for child in self.cstore.item_children_untracked(def_id, self.session) { for child in self.cstore.item_children_untracked(def_id, self.session) {
let child = child.map_id(|_| panic!("unexpected id"));
self.build_reduced_graph_for_external_crate_def(module, child); self.build_reduced_graph_for_external_crate_def(module, child);
} }
module.populated.set(true) module.populated.set(true)

View file

@ -2,14 +2,16 @@ use std::cmp::Reverse;
use errors::{Applicability, DiagnosticBuilder, DiagnosticId}; use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
use log::debug; use log::debug;
use rustc::hir::def::{Def, CtorKind, Namespace::*}; use rustc::hir::def::{self, CtorKind, Namespace::*};
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId}; use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::session::{Session, config::nightly_options}; use rustc::session::{Session, config::nightly_options};
use syntax::ast::{Expr, ExprKind, Ident}; use syntax::ast::{self, Expr, ExprKind, Ident};
use syntax::ext::base::MacroKind; use syntax::ext::base::MacroKind;
use syntax::symbol::{Symbol, keywords}; use syntax::symbol::{Symbol, keywords};
use syntax_pos::{BytePos, Span}; use syntax_pos::{BytePos, Span};
type Def = def::Def<ast::NodeId>;
use crate::macros::ParentScope; use crate::macros::ParentScope;
use crate::resolve_imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver}; use crate::resolve_imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver};
use crate::{import_candidate_to_enum_paths, is_self_type, is_self_value, path_names_to_string}; use crate::{import_candidate_to_enum_paths, is_self_type, is_self_value, path_names_to_string};

View file

@ -4,6 +4,7 @@
#![feature(label_break_value)] #![feature(label_break_value)]
#![feature(nll)] #![feature(nll)]
#![feature(rustc_diagnostic_macros)] #![feature(rustc_diagnostic_macros)]
#![feature(type_alias_enum_variants)]
#![recursion_limit="256"] #![recursion_limit="256"]
@ -20,7 +21,9 @@ use rustc::hir::{self, PrimTy, Bool, Char, Float, Int, Uint, Str};
use rustc::middle::cstore::CrateStore; use rustc::middle::cstore::CrateStore;
use rustc::session::Session; use rustc::session::Session;
use rustc::lint; use rustc::lint;
use rustc::hir::def::*; use rustc::hir::def::{
self, PathResolution, CtorKind, CtorOf, NonMacroAttrKind, DefMap, ImportMap, ExportMap
};
use rustc::hir::def::Namespace::*; use rustc::hir::def::Namespace::*;
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId}; use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap}; use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
@ -66,6 +69,8 @@ use error_reporting::{find_span_of_binding_until_next_binding, extend_span_to_pr
use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver}; use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
use macros::{InvocationData, LegacyBinding, ParentScope}; use macros::{InvocationData, LegacyBinding, ParentScope};
type Def = def::Def<NodeId>;
// N.B., this module needs to be declared first so diagnostics are // N.B., this module needs to be declared first so diagnostics are
// registered before they are used. // registered before they are used.
mod diagnostics; mod diagnostics;
@ -1563,7 +1568,7 @@ pub struct Resolver<'a> {
import_map: ImportMap, import_map: ImportMap,
pub freevars: FreevarMap, pub freevars: FreevarMap,
freevars_seen: NodeMap<NodeMap<usize>>, freevars_seen: NodeMap<NodeMap<usize>>,
pub export_map: ExportMap, pub export_map: ExportMap<NodeId>,
pub trait_map: TraitMap, pub trait_map: TraitMap,
/// A map from nodes to anonymous modules. /// A map from nodes to anonymous modules.
@ -1773,7 +1778,7 @@ impl<'a> Resolver<'a> {
} }
}; };
let path = self.resolve_hir_path_cb(&path, is_value, |_, _, _| errored = true); let path = self.resolve_hir_path_cb(&path, is_value, |_, _, _| errored = true);
if errored || path.def == Def::Err { if errored || path.def == def::Def::Err {
Err(()) Err(())
} else { } else {
Ok(path) Ok(path)
@ -1819,12 +1824,14 @@ impl<'a> Resolver<'a> {
let segments: Vec<_> = segments.iter().map(|seg| { let segments: Vec<_> = segments.iter().map(|seg| {
let mut hir_seg = hir::PathSegment::from_ident(seg.ident); let mut hir_seg = hir::PathSegment::from_ident(seg.ident);
hir_seg.def = Some(self.def_map.get(&seg.id).map_or(Def::Err, |p| p.base_def())); hir_seg.def = Some(self.def_map.get(&seg.id).map_or(def::Def::Err, |p| {
p.base_def().map_id(|_| panic!("unexpected node_id"))
}));
hir_seg hir_seg
}).collect(); }).collect();
hir::Path { hir::Path {
span, span,
def, def: def.map_id(|_| panic!("unexpected node_id")),
segments: segments.into(), segments: segments.into(),
} }
} }

View file

@ -8,7 +8,7 @@ use crate::build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
use crate::resolve_imports::ImportResolver; use crate::resolve_imports::ImportResolver;
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex, use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
CrateNum, DefIndexAddressSpace}; CrateNum, DefIndexAddressSpace};
use rustc::hir::def::{Def, NonMacroAttrKind}; use rustc::hir::def::{self, NonMacroAttrKind};
use rustc::hir::map::{self, DefCollector}; use rustc::hir::map::{self, DefCollector};
use rustc::{ty, lint}; use rustc::{ty, lint};
use rustc::{bug, span_bug}; use rustc::{bug, span_bug};
@ -33,6 +33,8 @@ use std::cell::Cell;
use std::{mem, ptr}; use std::{mem, ptr};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
type Def = def::Def<ast::NodeId>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct InvocationData<'a> { pub struct InvocationData<'a> {
def_index: DefIndex, def_index: DefIndex,

View file

@ -21,7 +21,7 @@ use rustc::lint::builtin::{
UNUSED_IMPORTS, UNUSED_IMPORTS,
}; };
use rustc::hir::def_id::{CrateNum, DefId}; use rustc::hir::def_id::{CrateNum, DefId};
use rustc::hir::def::*; use rustc::hir::def::{self, PathResolution, Export};
use rustc::session::DiagnosticMessageId; use rustc::session::DiagnosticMessageId;
use rustc::util::nodemap::FxHashSet; use rustc::util::nodemap::FxHashSet;
use rustc::{bug, span_bug}; use rustc::{bug, span_bug};
@ -39,6 +39,8 @@ use log::*;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::{mem, ptr}; use std::{mem, ptr};
type Def = def::Def<NodeId>;
/// Contains data for specific types of import directives. /// Contains data for specific types of import directives.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum ImportDirectiveSubclass<'a> { pub enum ImportDirectiveSubclass<'a> {

View file

@ -915,13 +915,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
// process collected paths // process collected paths
for (id, ident, immut) in collector.collected_idents { for (id, ident, immut) in collector.collected_idents {
match self.save_ctxt.get_path_def(id) { match self.save_ctxt.get_path_def(id) {
HirDef::Local(id) => { HirDef::Local(hir_id) => {
let mut value = if immut == ast::Mutability::Immutable { let mut value = if immut == ast::Mutability::Immutable {
self.span.snippet(ident.span) self.span.snippet(ident.span)
} else { } else {
"<mutable>".to_owned() "<mutable>".to_owned()
}; };
let hir_id = self.tcx.hir().node_to_hir_id(id); let id = self.tcx.hir().hir_to_node_id(hir_id);
let typ = self.save_ctxt let typ = self.save_ctxt
.tables .tables
.node_type_opt(hir_id) .node_type_opt(hir_id)

View file

@ -659,7 +659,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
Node::Binding(&hir::Pat { Node::Binding(&hir::Pat {
node: hir::PatKind::Binding(_, canonical_id, ..), node: hir::PatKind::Binding(_, canonical_id, ..),
.. ..
}) => HirDef::Local(self.tcx.hir().hir_to_node_id(canonical_id)), }) => HirDef::Local(canonical_id),
_ => HirDef::Err, _ => HirDef::Err,
} }
@ -707,7 +707,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
Some(Ref { Some(Ref {
kind: RefKind::Variable, kind: RefKind::Variable,
span, span,
ref_id: id_from_node_id(id, self), ref_id: id_from_node_id(self.tcx.hir().hir_to_node_id(id), self),
}) })
} }
HirDef::Trait(def_id) if fn_type(path_seg) => { HirDef::Trait(def_id) if fn_type(path_seg) => {

View file

@ -350,7 +350,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let def_span = match def { let def_span = match def {
Def::Err => None, Def::Err => None,
Def::Local(id) | Def::Upvar(id, ..) => Some(self.tcx.hir().span(id)), Def::Local(id) | Def::Upvar(id, ..) => {
Some(self.tcx.hir().span_by_hir_id(id))
},
_ => def _ => def
.opt_def_id() .opt_def_id()
.and_then(|did| self.tcx.hir().span_if_local(did)), .and_then(|did| self.tcx.hir().span_if_local(did)),

View file

@ -236,12 +236,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
) -> Option<(Span, &'static str, String)> { ) -> Option<(Span, &'static str, String)> {
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node { if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
if let hir::def::Def::Local(id) = path.def { if let hir::def::Def::Local(id) = path.def {
let parent = self.tcx.hir().get_parent_node(id); let parent = self.tcx.hir().get_parent_node_by_hir_id(id);
if let Some(Node::Expr(hir::Expr { if let Some(Node::Expr(hir::Expr {
hir_id, hir_id,
node: hir::ExprKind::Closure(_, decl, ..), node: hir::ExprKind::Closure(_, decl, ..),
.. ..
})) = self.tcx.hir().find(parent) { })) = self.tcx.hir().find_by_hir_id(parent) {
let parent = self.tcx.hir().get_parent_node_by_hir_id(*hir_id); let parent = self.tcx.hir().get_parent_node_by_hir_id(*hir_id);
if let (Some(Node::Expr(hir::Expr { if let (Some(Node::Expr(hir::Expr {
node: hir::ExprKind::MethodCall(path, span, expr), node: hir::ExprKind::MethodCall(path, span, expr),

View file

@ -249,14 +249,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
ExprKind::Path(ref qpath) => { ExprKind::Path(ref qpath) => {
// local binding // local binding
if let &QPath::Resolved(_, ref path) = &qpath { if let &QPath::Resolved(_, ref path) = &qpath {
if let hir::def::Def::Local(node_id) = path.def { if let hir::def::Def::Local(hir_id) = path.def {
let span = tcx.hir().span(node_id); let span = tcx.hir().span_by_hir_id(hir_id);
let snippet = tcx.sess.source_map().span_to_snippet(span) let snippet = tcx.sess.source_map().span_to_snippet(span)
.unwrap(); .unwrap();
let filename = tcx.sess.source_map().span_to_filename(span); let filename = tcx.sess.source_map().span_to_filename(span);
let parent_node = self.tcx.hir().get( let parent_node = self.tcx.hir().get_by_hir_id(
self.tcx.hir().get_parent_node(node_id), self.tcx.hir().get_parent_node_by_hir_id(hir_id),
); );
let msg = format!( let msg = format!(
"you must specify a type for this binding, like `{}`", "you must specify a type for this binding, like `{}`",

View file

@ -5397,8 +5397,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let tcx = self.tcx; let tcx = self.tcx;
match def { match def {
Def::Local(nid) | Def::Upvar(nid, ..) => { Def::Local(hid) | Def::Upvar(hid, ..) => {
let hid = self.tcx.hir().node_to_hir_id(nid);
let ty = self.local_ty(span, hid).decl_ty; let ty = self.local_ty(span, hid).decl_ty;
let ty = self.normalize_associated_types_in(span, &ty); let ty = self.normalize_associated_types_in(span, &ty);
self.write_ty(hir_id, ty); self.write_ty(hir_id, ty);

View file

@ -126,7 +126,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
for freevar in freevars { for freevar in freevars {
let upvar_id = ty::UpvarId { let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { var_path: ty::UpvarPath {
hir_id: self.tcx.hir().node_to_hir_id(freevar.var_id()), hir_id: freevar.var_id(),
}, },
closure_expr_id: LocalDefId::from_def_id(closure_def_id), closure_expr_id: LocalDefId::from_def_id(closure_def_id),
}; };
@ -250,8 +250,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
freevars freevars
.iter() .iter()
.map(|freevar| { .map(|freevar| {
let var_node_id = freevar.var_id(); let var_hir_id = freevar.var_id();
let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
let freevar_ty = self.node_ty(var_hir_id); let freevar_ty = self.node_ty(var_hir_id);
let upvar_id = ty::UpvarId { let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { hir_id: var_hir_id }, var_path: ty::UpvarPath { hir_id: var_hir_id },
@ -261,7 +260,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!( debug!(
"var_id={:?} freevar_ty={:?} capture={:?}", "var_id={:?} freevar_ty={:?} capture={:?}",
var_node_id, freevar_ty, capture var_hir_id, freevar_ty, capture
); );
match capture { match capture {

View file

@ -429,12 +429,12 @@ fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Def> {
// skip proc-macro stubs, they'll cause `get_macro` to crash // skip proc-macro stubs, they'll cause `get_macro` to crash
} else { } else {
if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(def) { if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(def) {
return Some(def); return Some(def.map_id(|_| panic!("unexpected id")));
} }
} }
} }
if let Some(def) = resolver.all_macros.get(&Symbol::intern(path_str)) { if let Some(def) = resolver.all_macros.get(&Symbol::intern(path_str)) {
return Some(*def); return Some(def.map_id(|_| panic!("unexpected id")));
} }
None None
}) })