Skip to content

Commit 26e61dd

Browse files
committed
rustc: replace Res in hir::Upvar with only Local/Upvar data.
1 parent cd3f21b commit 26e61dd

File tree

9 files changed

+44
-38
lines changed

9 files changed

+44
-38
lines changed

src/librustc/hir/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,26 +2495,24 @@ impl ForeignItemKind {
24952495
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
24962496
pub struct Upvar<Id = HirId> {
24972497
/// The variable being captured.
2498-
pub res: Res<Id>,
2498+
pub var_id: Id,
2499+
2500+
/// The parent closure, if this is not a direct capture,
2501+
/// and the index within that closure's capture list.
2502+
pub parent: Option<(ast::NodeId, usize)>,
24992503

25002504
// First span where it is accessed (there can be multiple).
25012505
pub span: Span
25022506
}
25032507

25042508
impl<Id: fmt::Debug + Copy> Upvar<Id> {
2505-
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Upvar<R> {
2509+
pub fn map_id<R>(self, map: impl FnOnce(Id) -> R) -> Upvar<R> {
25062510
Upvar {
2507-
res: self.res.map_id(map),
2511+
var_id: map(self.var_id),
2512+
parent: self.parent,
25082513
span: self.span,
25092514
}
25102515
}
2511-
2512-
pub fn var_id(&self) -> Id {
2513-
match self.res {
2514-
Res::Local(id) | Res::Upvar(id, ..) => id,
2515-
_ => bug!("Upvar::var_id: bad res ({:?})", self.res)
2516-
}
2517-
}
25182516
}
25192517

25202518
pub type UpvarMap = NodeMap<Vec<Upvar<ast::NodeId>>>;

src/librustc/middle/expr_use_visitor.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
925925
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id);
926926
if let Some(upvars) = self.tcx().upvars(closure_def_id) {
927927
for upvar in upvars.iter() {
928-
let var_hir_id = upvar.var_id();
929928
let upvar_id = ty::UpvarId {
930-
var_path: ty::UpvarPath { hir_id: var_hir_id },
929+
var_path: ty::UpvarPath { hir_id: upvar.var_id },
931930
closure_expr_id: closure_def_id.to_local(),
932931
};
933932
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
@@ -962,9 +961,12 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
962961
-> mc::McResult<mc::cmt_<'tcx>> {
963962
// Create the cmt for the variable being borrowed, from the
964963
// caller's perspective
965-
let var_hir_id = upvar.var_id();
966-
let var_ty = self.mc.node_ty(var_hir_id)?;
967-
self.mc.cat_res(closure_hir_id, closure_span, var_ty, upvar.res)
964+
let var_ty = self.mc.node_ty(upvar.var_id)?;
965+
let res = upvar.parent.map_or(
966+
Res::Local(upvar.var_id),
967+
|(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
968+
);
969+
self.mc.cat_res(closure_hir_id, closure_span, var_ty, res)
968970
}
969971
}
970972

src/librustc/middle/liveness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,9 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
486486
let closure_def_id = ir.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
487487
if let Some(upvars) = ir.tcx.upvars(closure_def_id) {
488488
call_caps.extend(upvars.iter().filter_map(|upvar| {
489-
if let Res::Local(rv) = upvar.res {
489+
if upvar.parent.is_none() {
490490
let upvar_ln = ir.add_live_node(UpvarNode(upvar.span));
491-
Some(CaptureInfo { ln: upvar_ln, var_hid: rv })
491+
Some(CaptureInfo { ln: upvar_ln, var_hid: upvar.var_id })
492492
} else {
493493
None
494494
}

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
25632563

25642564
if let Some(upvars) = tcx.upvars(def_id) {
25652565
for (upvar, place) in upvars.iter().zip(places) {
2566-
let var_name = tcx.hir().name_by_hir_id(upvar.var_id());
2566+
let var_name = tcx.hir().name_by_hir_id(upvar.var_id);
25672567
struct_fmt.field(&var_name.as_str(), place);
25682568
}
25692569
}
@@ -2582,7 +2582,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
25822582

25832583
if let Some(upvars) = tcx.upvars(def_id) {
25842584
for (upvar, place) in upvars.iter().zip(places) {
2585-
let var_name = tcx.hir().name_by_hir_id(upvar.var_id());
2585+
let var_name = tcx.hir().name_by_hir_id(upvar.var_id);
25862586
struct_fmt.field(&var_name.as_str(), place);
25872587
}
25882588
}

src/librustc/ty/print/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
594594
p!(
595595
write("{}{}:",
596596
sep,
597-
self.tcx().hir().name_by_hir_id(upvar.var_id())),
597+
self.tcx().hir().name_by_hir_id(upvar.var_id)),
598598
print(upvar_ty));
599599
sep = ", ";
600600
}
@@ -637,7 +637,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
637637
p!(
638638
write("{}{}:",
639639
sep,
640-
self.tcx().hir().name_by_hir_id(upvar.var_id())),
640+
self.tcx().hir().name_by_hir_id(upvar.var_id)),
641641
print(upvar_ty));
642642
sep = ", ";
643643
}

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
350350
// be borrowck'ing it, so we can just unwrap:
351351
let upvar = self.infcx.tcx.upvars(def_id).unwrap()[field.index()];
352352

353-
self.infcx.tcx.hir().name_by_hir_id(upvar.var_id()).to_string()
353+
self.infcx.tcx.hir().name_by_hir_id(upvar.var_id).to_string()
354354
}
355355
_ => {
356356
// Might need a revision when the fields in trait RFC is implemented

src/librustc_mir/hair/cx/expr.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,19 +1181,22 @@ fn capture_upvar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
11811181
upvar: &hir::Upvar,
11821182
upvar_ty: Ty<'tcx>)
11831183
-> ExprRef<'tcx> {
1184-
let var_hir_id = upvar.var_id();
11851184
let upvar_id = ty::UpvarId {
1186-
var_path: ty::UpvarPath { hir_id: var_hir_id },
1185+
var_path: ty::UpvarPath { hir_id: upvar.var_id },
11871186
closure_expr_id: cx.tcx.hir().local_def_id_from_hir_id(closure_expr.hir_id).to_local(),
11881187
};
11891188
let upvar_capture = cx.tables().upvar_capture(upvar_id);
11901189
let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1191-
let var_ty = cx.tables().node_type(var_hir_id);
1190+
let var_ty = cx.tables().node_type(upvar.var_id);
1191+
let upvar_res = upvar.parent.map_or(
1192+
Res::Local(upvar.var_id),
1193+
|(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
1194+
);
11921195
let captured_var = Expr {
11931196
temp_lifetime,
11941197
ty: var_ty,
11951198
span: closure_expr.span,
1196-
kind: convert_var(cx, closure_expr, upvar.res),
1199+
kind: convert_var(cx, closure_expr, upvar_res),
11971200
};
11981201
match upvar_capture {
11991202
ty::UpvarCapture::ByValue => captured_var.to_ref(),

src/librustc_resolve/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,7 +4040,7 @@ impl<'a> Resolver<'a> {
40404040
Res::Upvar(..) => {
40414041
span_bug!(span, "unexpected {:?} in bindings", res)
40424042
}
4043-
Res::Local(node_id) => {
4043+
Res::Local(var_id) => {
40444044
use ResolutionError::*;
40454045
let mut res_err = None;
40464046

@@ -4051,27 +4051,31 @@ impl<'a> Resolver<'a> {
40514051
// Nothing to do. Continue.
40524052
}
40534053
ClosureRibKind(function_id) => {
4054-
let prev_res = res;
4054+
let parent = match res {
4055+
Res::Upvar(_, i, closure) => Some((closure, i)),
4056+
_ => None,
4057+
};
40554058

40564059
let seen = self.upvars_seen
40574060
.entry(function_id)
40584061
.or_default();
4059-
if let Some(&index) = seen.get(&node_id) {
4060-
res = Res::Upvar(node_id, index, function_id);
4062+
if let Some(&index) = seen.get(&var_id) {
4063+
res = Res::Upvar(var_id, index, function_id);
40614064
continue;
40624065
}
40634066
let vec = self.upvars
40644067
.entry(function_id)
40654068
.or_default();
40664069
let depth = vec.len();
4067-
res = Res::Upvar(node_id, depth, function_id);
4070+
res = Res::Upvar(var_id, depth, function_id);
40684071

40694072
if record_used {
40704073
vec.push(Upvar {
4071-
res: prev_res,
4074+
var_id,
4075+
parent,
40724076
span,
40734077
});
4074-
seen.insert(node_id, depth);
4078+
seen.insert(var_id, depth);
40754079
}
40764080
}
40774081
ItemRibKind | FnItemRibKind | AssocItemRibKind => {

src/librustc_typeck/check/upvar.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
126126
for upvar in upvars.iter() {
127127
let upvar_id = ty::UpvarId {
128128
var_path: ty::UpvarPath {
129-
hir_id: upvar.var_id(),
129+
hir_id: upvar.var_id,
130130
},
131131
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
132132
};
@@ -250,17 +250,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
250250
upvars
251251
.iter()
252252
.map(|upvar| {
253-
let var_hir_id = upvar.var_id();
254-
let upvar_ty = self.node_ty(var_hir_id);
253+
let upvar_ty = self.node_ty(upvar.var_id);
255254
let upvar_id = ty::UpvarId {
256-
var_path: ty::UpvarPath { hir_id: var_hir_id },
255+
var_path: ty::UpvarPath { hir_id: upvar.var_id },
257256
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
258257
};
259258
let capture = self.tables.borrow().upvar_capture(upvar_id);
260259

261260
debug!(
262261
"var_id={:?} upvar_ty={:?} capture={:?}",
263-
var_hir_id, upvar_ty, capture
262+
upvar.var_id, upvar_ty, capture
264263
);
265264

266265
match capture {

0 commit comments

Comments
 (0)