Skip to content

Commit 903ec52

Browse files
committed
rustc: replace uses of NodeId in Def, other than closures and labels.
1 parent fc363cb commit 903ec52

File tree

19 files changed

+83
-97
lines changed

19 files changed

+83
-97
lines changed

src/librustc/hir/def.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ use hir;
1616
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1717
pub enum Def {
1818
Fn(DefId),
19-
SelfTy(Option<DefId> /* trait */, Option<ast::NodeId> /* impl */),
19+
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
2020
Mod(DefId),
2121
ForeignMod(DefId),
2222
Static(DefId, bool /* is_mutbl */),
2323
Const(DefId),
2424
AssociatedConst(DefId),
25-
Local(DefId, // def id of variable
26-
ast::NodeId), // node id of variable
25+
Local(DefId),
2726
Variant(DefId /* enum */, DefId /* variant */),
2827
Enum(DefId),
2928
TyAlias(DefId),
@@ -32,7 +31,6 @@ pub enum Def {
3231
PrimTy(hir::PrimTy),
3332
TyParam(DefId),
3433
Upvar(DefId, // def id of closed over local
35-
ast::NodeId, // node id of closed over local
3634
usize, // index in the freevars list of the closure
3735
ast::NodeId), // expr node that creates the closure
3836

@@ -101,30 +99,13 @@ pub struct Export {
10199
}
102100

103101
impl Def {
104-
pub fn var_id(&self) -> ast::NodeId {
105-
match *self {
106-
Def::Local(_, id) |
107-
Def::Upvar(_, id, ..) => {
108-
id
109-
}
110-
111-
Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
112-
Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
113-
Def::TyParam(..) | Def::Struct(..) | Def::Union(..) | Def::Trait(..) |
114-
Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
115-
Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
116-
bug!("attempted .var_id() on invalid {:?}", self)
117-
}
118-
}
119-
}
120-
121102
pub fn def_id(&self) -> DefId {
122103
match *self {
123104
Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
124105
Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
125106
Def::TyParam(id) | Def::Struct(id) | Def::Union(id) | Def::Trait(id) |
126107
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
127-
Def::Local(id, _) | Def::Upvar(id, ..) => {
108+
Def::Local(id) | Def::Upvar(id, ..) => {
128109
id
129110
}
130111

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ impl<'a> LoweringContext<'a> {
16971697

16981698
let def = {
16991699
let defs = self.resolver.definitions();
1700-
Def::Local(defs.local_def_id(binding), binding)
1700+
Def::Local(defs.local_def_id(binding))
17011701
};
17021702
self.resolver.record_resolution(expr.id, def);
17031703

@@ -1850,7 +1850,7 @@ impl<'a> LoweringContext<'a> {
18501850
let defs = self.resolver.definitions();
18511851
let def_path_data = DefPathData::Binding(name.as_str());
18521852
let def_index = defs.create_def_with_parent(parent_def, pat.id, def_path_data);
1853-
Def::Local(DefId::local(def_index), pat.id)
1853+
Def::Local(DefId::local(def_index))
18541854
};
18551855
self.resolver.record_resolution(pat.id, def);
18561856

src/librustc/middle/expr_use_visitor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
10291029

10301030
self.tcx().with_freevars(closure_expr.id, |freevars| {
10311031
for freevar in freevars {
1032-
let id_var = freevar.def.var_id();
1032+
let def_id = freevar.def.def_id();
1033+
let id_var = self.tcx().map.as_local_node_id(def_id).unwrap();
10331034
let upvar_id = ty::UpvarId { var_id: id_var,
10341035
closure_expr_id: closure_expr.id };
10351036
let upvar_capture = self.mc.infcx.upvar_capture(upvar_id).unwrap();
@@ -1061,7 +1062,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
10611062
-> mc::McResult<mc::cmt<'tcx>> {
10621063
// Create the cmt for the variable being borrowed, from the
10631064
// caller's perspective
1064-
let var_id = upvar_def.var_id();
1065+
let var_id = self.tcx().map.as_local_node_id(upvar_def.def_id()).unwrap();
10651066
let var_ty = self.mc.infcx.node_ty(var_id)?;
10661067
self.mc.cat_def(closure_id, closure_span, var_ty, upvar_def)
10671068
}

src/librustc/middle/liveness.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ fn visit_expr(ir: &mut IrMaps, expr: &Expr) {
465465
let mut call_caps = Vec::new();
466466
ir.tcx.with_freevars(expr.id, |freevars| {
467467
for fv in freevars {
468-
if let Def::Local(_, rv) = fv.def {
468+
if let Def::Local(def_id) = fv.def {
469+
let rv = ir.tcx.map.as_local_node_id(def_id).unwrap();
469470
let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
470471
call_caps.push(CaptureInfo {ln: fv_ln,
471472
var_nid: rv});
@@ -1270,7 +1271,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12701271
fn access_path(&mut self, expr: &Expr, succ: LiveNode, acc: u32)
12711272
-> LiveNode {
12721273
match self.ir.tcx.expect_def(expr.id) {
1273-
Def::Local(_, nid) => {
1274+
Def::Local(def_id) => {
1275+
let nid = self.ir.tcx.map.as_local_node_id(def_id).unwrap();
12741276
let ln = self.live_node(expr.id, expr.span);
12751277
if acc != 0 {
12761278
self.init_from_succ(ln, succ);
@@ -1529,11 +1531,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15291531
fn check_lvalue(&mut self, expr: &Expr) {
15301532
match expr.node {
15311533
hir::ExprPath(..) => {
1532-
if let Def::Local(_, nid) = self.ir.tcx.expect_def(expr.id) {
1534+
if let Def::Local(def_id) = self.ir.tcx.expect_def(expr.id) {
15331535
// Assignment to an immutable variable or argument: only legal
15341536
// if there is no later assignment. If this local is actually
15351537
// mutable, then check for a reassignment to flag the mutability
15361538
// as being used.
1539+
let nid = self.ir.tcx.map.as_local_node_id(def_id).unwrap();
15371540
let ln = self.live_node(expr.id, expr.span);
15381541
let var = self.variable(nid, expr.span);
15391542
self.warn_about_dead_assign(expr.span, expr.id, ln, var);

src/librustc/middle/mem_categorization.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
549549
}))
550550
}
551551

552-
Def::Upvar(_, var_id, _, fn_node_id) => {
552+
Def::Upvar(def_id, _, fn_node_id) => {
553+
let var_id = self.tcx().map.as_local_node_id(def_id).unwrap();
553554
let ty = self.node_ty(fn_node_id)?;
554555
match ty.sty {
555556
ty::TyClosure(closure_id, _) => {
@@ -585,7 +586,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
585586
}
586587
}
587588

588-
Def::Local(_, vid) => {
589+
Def::Local(def_id) => {
590+
let vid = self.tcx().map.as_local_node_id(def_id).unwrap();
589591
Ok(Rc::new(cmt_ {
590592
id: id,
591593
span: span,

src/librustc/mir/repr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,9 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10981098

10991099
tcx.with_freevars(node_id, |freevars| {
11001100
for (freevar, lv) in freevars.iter().zip(lvs) {
1101-
let var_name = tcx.local_var_name_str(freevar.def.var_id());
1101+
let def_id = freevar.def.def_id();
1102+
let var_id = tcx.map.as_local_node_id(def_id).unwrap();
1103+
let var_name = tcx.local_var_name_str(var_id);
11021104
struct_fmt.field(&var_name, lv);
11031105
}
11041106
});

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
920920
let mut sep = " ";
921921
tcx.with_freevars(node_id, |freevars| {
922922
for (freevar, upvar_ty) in freevars.iter().zip(substs.upvar_tys) {
923-
let node_id = freevar.def.var_id();
923+
let def_id = freevar.def.def_id();
924+
let node_id = tcx.map.as_local_node_id(def_id).unwrap();
924925
write!(f,
925926
"{}{}:{}",
926927
sep,

src/librustc_const_eval/eval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
824824
Def::Struct(..) => {
825825
ConstVal::Struct(e.id)
826826
}
827-
Def::Local(_, id) => {
827+
Def::Local(def_id) => {
828+
let id = tcx.map.as_local_node_id(def_id).unwrap();
828829
debug!("Def::Local({:?}): {:?}", id, fn_args);
829830
if let Some(val) = fn_args.and_then(|args| args.get(&id)) {
830831
val.clone()

src/librustc_metadata/astencode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ fn encode_side_tables_for_id(ecx: &mut EncodeContext, id: ast::NodeId) {
167167
ecx.tag(c::tag_table_upvar_capture_map, |ecx| {
168168
ecx.id(id);
169169

170-
let var_id = freevar.def.var_id();
170+
let def_id = freevar.def.def_id();
171+
let var_id = tcx.map.as_local_node_id(def_id).unwrap();
171172
let upvar_id = ty::UpvarId {
172173
var_id: var_id,
173174
closure_expr_id: id

src/librustc_mir/build/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ pub fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
197197
// Gather the upvars of a closure, if any.
198198
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
199199
freevars.iter().map(|fv| {
200+
let var_id = tcx.map.as_local_node_id(fv.def.def_id()).unwrap();
200201
let by_ref = tcx.upvar_capture(ty::UpvarId {
201-
var_id: fv.def.var_id(),
202+
var_id: var_id,
202203
closure_expr_id: fn_id
203204
}).map_or(false, |capture| match capture {
204205
ty::UpvarCapture::ByValue => false,
@@ -208,7 +209,7 @@ pub fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
208209
debug_name: keywords::Invalid.name(),
209210
by_ref: by_ref
210211
};
211-
if let Some(hir::map::NodeLocal(pat)) = tcx.map.find(fv.def.var_id()) {
212+
if let Some(hir::map::NodeLocal(pat)) = tcx.map.find(var_id) {
212213
if let hir::PatKind::Binding(_, ref ident, _) = pat.node {
213214
decl.debug_name = ident.node;
214215
}

0 commit comments

Comments
 (0)