Skip to content

Commit 4dcc3a4

Browse files
Use DefIndex instead of NodeId in UpvarId.
1 parent a8cf6cc commit 4dcc3a4

File tree

13 files changed

+142
-85
lines changed

13 files changed

+142
-85
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! This module contains `HashStable` implementations for various data types
1212
//! from rustc::ty in no particular order.
1313
14+
use hir::def_id::DefId;
1415
use ich::{self, StableHashingContext, NodeIdHashingMode};
1516
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
1617
StableHasherResult};
@@ -618,7 +619,7 @@ for ty::TypeckTables<'gcx> {
618619
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
619620
hasher: &mut StableHasher<W>) {
620621
let ty::TypeckTables {
621-
local_id_root: _,
622+
local_id_root,
622623
ref type_dependent_defs,
623624
ref node_types,
624625
ref node_substs,
@@ -649,8 +650,14 @@ for ty::TypeckTables<'gcx> {
649650
closure_expr_id
650651
} = *up_var_id;
651652

652-
let var_def_id = hcx.tcx().hir.local_def_id(var_id);
653-
let closure_def_id = hcx.tcx().hir.local_def_id(closure_expr_id);
653+
let var_def_id = DefId {
654+
krate: local_id_root.krate,
655+
index: var_id,
656+
};
657+
let closure_def_id = DefId {
658+
krate: local_id_root.krate,
659+
index: closure_expr_id,
660+
};
654661
(hcx.def_path_hash(var_def_id), hcx.def_path_hash(closure_def_id))
655662
});
656663

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
913913
}
914914
infer::UpvarRegion(ref upvar_id, _) => {
915915
format!(" for capture of `{}` by closure",
916-
self.tcx.local_var_name_str(upvar_id.var_id).to_string())
916+
self.tcx.local_var_name_str_def_index(upvar_id.var_id))
917917
}
918918
};
919919

src/librustc/infer/error_reporting/note.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
4545
err.span_note(span,
4646
&format!("...so that closure can access `{}`",
4747
self.tcx
48-
.local_var_name_str(upvar_id.var_id)
49-
.to_string()));
48+
.local_var_name_str_def_index(upvar_id.var_id)));
5049
}
5150
infer::InfStackClosure(span) => {
5251
err.span_note(span, "...so that closure does not outlive its stack frame");
@@ -176,18 +175,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
176175
E0313,
177176
"lifetime of borrowed pointer outlives lifetime \
178177
of captured variable `{}`...",
179-
self.tcx.local_var_name_str(upvar_id.var_id));
178+
self.tcx
179+
.local_var_name_str_def_index(upvar_id.var_id));
180180
self.tcx.note_and_explain_region(&mut err,
181181
"...the borrowed pointer is valid for ",
182182
sub,
183183
"...");
184184
self.tcx
185-
.note_and_explain_region(&mut err,
186-
&format!("...but `{}` is only valid for ",
187-
self.tcx
188-
.local_var_name_str(upvar_id.var_id)),
189-
sup,
190-
"");
185+
.note_and_explain_region(
186+
&mut err,
187+
&format!("...but `{}` is only valid for ",
188+
self.tcx.local_var_name_str_def_index(upvar_id.var_id)),
189+
sup,
190+
"");
191191
err
192192
}
193193
infer::InfStackClosure(span) => {

src/librustc/middle/expr_use_visitor.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,13 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
890890

891891
self.tcx().with_freevars(closure_expr.id, |freevars| {
892892
for freevar in freevars {
893-
let def_id = freevar.def.def_id();
894-
let id_var = self.tcx().hir.as_local_node_id(def_id).unwrap();
895-
let upvar_id = ty::UpvarId { var_id: id_var,
896-
closure_expr_id: closure_expr.id };
893+
let var_def_id = freevar.def.def_id();
894+
debug_assert!(var_def_id.is_local());
895+
let closure_def_id = self.tcx().hir.local_def_id(closure_expr.id);
896+
let upvar_id = ty::UpvarId {
897+
var_id: var_def_id.index,
898+
closure_expr_id: closure_def_id.index
899+
};
897900
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
898901
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
899902
fn_decl_span,

src/librustc/middle/mem_categorization.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub use self::Note::*;
7070
use self::Aliasability::*;
7171

7272
use middle::region::RegionMaps;
73-
use hir::def_id::DefId;
73+
use hir::def_id::{DefId, DefIndex};
7474
use hir::map as hir_map;
7575
use infer::InferCtxt;
7676
use hir::def::{Def, CtorKind};
@@ -190,7 +190,7 @@ pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
190190

191191
pub enum ImmutabilityBlame<'tcx> {
192192
ImmLocal(ast::NodeId),
193-
ClosureEnv(ast::NodeId),
193+
ClosureEnv(DefIndex),
194194
LocalDeref(ast::NodeId),
195195
AdtFieldDeref(&'tcx ty::AdtDef, &'tcx ty::FieldDef)
196196
}
@@ -728,8 +728,13 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
728728
None => span_bug!(span, "missing closure kind")
729729
};
730730

731-
let upvar_id = ty::UpvarId { var_id,
732-
closure_expr_id: fn_node_id };
731+
let closure_expr_def_index = self.tcx.hir.local_def_id(fn_node_id).index;
732+
let var_def_index = self.tcx.hir.local_def_id(var_id).index;
733+
734+
let upvar_id = ty::UpvarId {
735+
var_id: var_def_index,
736+
closure_expr_id: closure_expr_def_index
737+
};
733738
let var_hir_id = self.tcx.hir.node_to_hir_id(var_id);
734739
let var_ty = self.node_ty(var_hir_id)?;
735740

@@ -766,8 +771,6 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
766771
// If this is a by-ref capture, then the upvar we loaded is
767772
// actually a reference, so we have to add an implicit deref
768773
// for that.
769-
let upvar_id = ty::UpvarId { var_id,
770-
closure_expr_id: fn_node_id };
771774
let upvar_capture = self.tables.upvar_capture(upvar_id);
772775
let cmt_result = match upvar_capture {
773776
ty::UpvarCapture::ByValue => {
@@ -805,7 +808,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
805808
// The environment of a closure is guaranteed to
806809
// outlive any bindings introduced in the body of the
807810
// closure itself.
808-
scope: self.tcx.hir.local_def_id(upvar_id.closure_expr_id),
811+
scope: DefId::local(upvar_id.closure_expr_id),
809812
bound_region: ty::BrEnv
810813
}));
811814

src/librustc/ty/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ impl<T> Slice<T> {
572572
/// by the upvar) and the id of the closure expression.
573573
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
574574
pub struct UpvarId {
575-
pub var_id: NodeId,
576-
pub closure_expr_id: NodeId,
575+
pub var_id: DefIndex,
576+
pub closure_expr_id: DefIndex,
577577
}
578578

579579
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, Copy)]
@@ -1983,6 +1983,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
19831983
}
19841984
}
19851985

1986+
pub fn local_var_name_str_def_index(self, def_index: DefIndex) -> InternedString {
1987+
let node_id = self.hir.as_local_node_id(DefId::local(def_index)).unwrap();
1988+
self.local_var_name_str(node_id)
1989+
}
1990+
19861991
pub fn expr_is_lval(self, expr: &hir::Expr) -> bool {
19871992
match expr.node {
19881993
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,9 @@ impl<'tcx> fmt::Display for ty::TyS<'tcx> {
864864

865865
impl fmt::Debug for ty::UpvarId {
866866
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
867-
write!(f, "UpvarId({};`{}`;{})",
867+
write!(f, "UpvarId({:?};`{}`;{:?})",
868868
self.var_id,
869-
ty::tls::with(|tcx| tcx.local_var_name_str(self.var_id)),
869+
ty::tls::with(|tcx| tcx.local_var_name_str_def_index(self.var_id)),
870870
self.closure_expr_id)
871871
}
872872
}

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
9393
}
9494
}
9595
if let NoteClosureEnv(upvar_id) = error.move_from.note {
96-
err.span_label(bccx.tcx.hir.span(upvar_id.var_id),
96+
let var_node_id = bccx.tcx.hir.def_index_to_node_id(upvar_id.var_id);
97+
err.span_label(bccx.tcx.hir.span(var_node_id),
9798
"captured outer variable");
9899
}
99100
err.emit();
100-
101101
}
102102
}
103103

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc::middle::dataflow::DataFlowContext;
2727
use rustc::middle::dataflow::BitwiseOperator;
2828
use rustc::middle::dataflow::DataFlowOperator;
2929
use rustc::middle::dataflow::KillFrom;
30-
use rustc::hir::def_id::DefId;
30+
use rustc::hir::def_id::{DefId, DefIndex};
3131
use rustc::middle::expr_use_visitor as euv;
3232
use rustc::middle::mem_categorization as mc;
3333
use rustc::middle::mem_categorization::Categorization;
@@ -323,8 +323,9 @@ pub enum LoanPathElem<'tcx> {
323323
LpInterior(Option<DefId>, InteriorKind),
324324
}
325325

326-
pub fn closure_to_block(closure_id: ast::NodeId,
327-
tcx: TyCtxt) -> ast::NodeId {
326+
fn closure_to_block(closure_id: DefIndex,
327+
tcx: TyCtxt) -> ast::NodeId {
328+
let closure_id = tcx.hir.def_index_to_node_id(closure_id);
328329
match tcx.hir.get(closure_id) {
329330
hir_map::NodeExpr(expr) => match expr.node {
330331
hir::ExprClosure(.., body_id, _) => {
@@ -845,7 +846,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
845846
} else {
846847
"consider changing this closure to take self by mutable reference"
847848
};
848-
err.span_help(self.tcx.hir.span(id), help);
849+
let node_id = self.tcx.hir.def_index_to_node_id(id);
850+
err.span_help(self.tcx.hir.span(node_id), help);
849851
err
850852
}
851853
_ => {
@@ -1181,7 +1183,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
11811183
_ => bug!()
11821184
};
11831185
if kind == ty::ClosureKind::Fn {
1184-
db.span_help(self.tcx.hir.span(upvar_id.closure_expr_id),
1186+
let closure_node_id =
1187+
self.tcx.hir.def_index_to_node_id(upvar_id.closure_expr_id);
1188+
db.span_help(self.tcx.hir.span(closure_node_id),
11851189
"consider changing this closure to take \
11861190
self by mutable reference");
11871191
}
@@ -1214,7 +1218,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12141218
loan_path: &LoanPath<'tcx>,
12151219
out: &mut String) {
12161220
match loan_path.kind {
1217-
LpUpvar(ty::UpvarId{ var_id: id, closure_expr_id: _ }) |
1221+
LpUpvar(ty::UpvarId { var_id: id, closure_expr_id: _ }) => {
1222+
out.push_str(&self.tcx.local_var_name_str_def_index(id));
1223+
}
12181224
LpVar(id) => {
12191225
out.push_str(&self.tcx.local_var_name_str(id));
12201226
}
@@ -1352,8 +1358,11 @@ impl<'tcx> fmt::Debug for LoanPath<'tcx> {
13521358
}
13531359

13541360
LpUpvar(ty::UpvarId{ var_id, closure_expr_id }) => {
1355-
let s = ty::tls::with(|tcx| tcx.hir.node_to_string(var_id));
1356-
write!(f, "$({} captured by id={})", s, closure_expr_id)
1361+
let s = ty::tls::with(|tcx| {
1362+
let var_node_id = tcx.hir.def_index_to_node_id(var_id);
1363+
tcx.hir.node_to_string(var_node_id)
1364+
});
1365+
write!(f, "$({} captured by id={:?})", s, closure_expr_id)
13571366
}
13581367

13591368
LpDowncast(ref lp, variant_def_id) => {
@@ -1384,7 +1393,10 @@ impl<'tcx> fmt::Display for LoanPath<'tcx> {
13841393
}
13851394

13861395
LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
1387-
let s = ty::tls::with(|tcx| tcx.hir.node_to_user_string(var_id));
1396+
let s = ty::tls::with(|tcx| {
1397+
let var_node_id = tcx.hir.def_index_to_node_id(var_id);
1398+
tcx.hir.node_to_string(var_node_id)
1399+
});
13881400
write!(f, "$({} captured by closure)", s)
13891401
}
13901402

src/librustc_mir/build/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
368368
// Gather the upvars of a closure, if any.
369369
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
370370
freevars.iter().map(|fv| {
371-
let var_id = tcx.hir.as_local_node_id(fv.def.def_id()).unwrap();
371+
let var_def_id = fv.def.def_id();
372+
let var_node_id = tcx.hir.as_local_node_id(var_def_id).unwrap();
373+
let closure_expr_id = tcx.hir.local_def_id(fn_id).index;
372374
let capture = hir.tables().upvar_capture(ty::UpvarId {
373-
var_id: var_id,
374-
closure_expr_id: fn_id
375+
var_id: var_def_id.index,
376+
closure_expr_id,
375377
});
376378
let by_ref = match capture {
377379
ty::UpvarCapture::ByValue => false,
@@ -381,7 +383,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
381383
debug_name: keywords::Invalid.name(),
382384
by_ref: by_ref
383385
};
384-
if let Some(hir::map::NodeLocal(pat)) = tcx.hir.find(var_id) {
386+
if let Some(hir::map::NodeLocal(pat)) = tcx.hir.find(var_node_id) {
385387
if let hir::PatKind::Binding(_, _, ref ident, _) = pat.node {
386388
decl.debug_name = ident.node;
387389
}

src/librustc_mir/hair/cx/expr.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
684684
ExprKind::VarRef { id: node_id }
685685
}
686686

687-
Def::Upvar(def_id, index, closure_expr_id) => {
688-
let id_var = cx.tcx.hir.as_local_node_id(def_id).unwrap();
687+
Def::Upvar(var_def_id, index, closure_expr_id) => {
688+
let id_var = cx.tcx.hir.as_local_node_id(var_def_id).unwrap();
689689
debug!("convert_var(upvar({:?}, {:?}, {:?}))",
690690
id_var,
691691
index,
@@ -768,8 +768,8 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
768768
// ...but the upvar might be an `&T` or `&mut T` capture, at which
769769
// point we need an implicit deref
770770
let upvar_id = ty::UpvarId {
771-
var_id: id_var,
772-
closure_expr_id: closure_expr_id,
771+
var_id: var_def_id.index,
772+
closure_expr_id: closure_def_id.index,
773773
};
774774
match cx.tables().upvar_capture(upvar_id) {
775775
ty::UpvarCapture::ByValue => field_kind,
@@ -880,15 +880,16 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
880880
freevar: &hir::Freevar,
881881
freevar_ty: Ty<'tcx>)
882882
-> ExprRef<'tcx> {
883-
let id_var = cx.tcx.hir.as_local_node_id(freevar.def.def_id()).unwrap();
883+
let var_def_id = freevar.def.def_id();
884+
let var_node_id = cx.tcx.hir.as_local_node_id(var_def_id).unwrap();
884885
let upvar_id = ty::UpvarId {
885-
var_id: id_var,
886-
closure_expr_id: closure_expr.id,
886+
var_id: var_def_id.index,
887+
closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).index,
887888
};
888889
let upvar_capture = cx.tables().upvar_capture(upvar_id);
889890
let temp_lifetime = cx.region_maps.temporary_scope(closure_expr.id);
890891
let var_ty = cx.tables()
891-
.node_id_to_type(cx.tcx.hir.node_to_hir_id(id_var));
892+
.node_id_to_type(cx.tcx.hir.node_to_hir_id(var_node_id));
892893
let captured_var = Expr {
893894
temp_lifetime: temp_lifetime,
894895
ty: var_ty,

0 commit comments

Comments
 (0)