Skip to content

Commit 125dc60

Browse files
committed
rustc: replace uses of with_freevars with the freevars query.
1 parent 5d8fd98 commit 125dc60

File tree

11 files changed

+49
-74
lines changed

11 files changed

+49
-74
lines changed

src/librustc/middle/expr_use_visitor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
931931
debug!("walk_captures({:?})", closure_expr);
932932

933933
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id);
934-
self.tcx().with_freevars(closure_expr.hir_id, |freevars| {
935-
for freevar in freevars {
934+
if let Some(freevars) = self.tcx().freevars(closure_def_id) {
935+
for freevar in freevars.iter() {
936936
let var_hir_id = freevar.var_id();
937937
let upvar_id = ty::UpvarId {
938938
var_path: ty::UpvarPath { hir_id: var_hir_id },
@@ -960,7 +960,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
960960
}
961961
}
962962
}
963-
});
963+
}
964964
}
965965

966966
fn cat_captured_var(&mut self,

src/librustc/middle/liveness.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,17 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
483483
// in better error messages than just pointing at the closure
484484
// construction site.
485485
let mut call_caps = Vec::new();
486-
ir.tcx.with_freevars(expr.hir_id, |freevars| {
487-
call_caps.extend(freevars.iter().filter_map(|fv| {
488-
if let Res::Local(rv) = fv.res {
489-
let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
490-
Some(CaptureInfo { ln: fv_ln, var_hid: rv })
486+
let closure_def_id = ir.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
487+
if let Some(freevars) = ir.tcx.freevars(closure_def_id) {
488+
call_caps.extend(freevars.iter().filter_map(|freevar| {
489+
if let Res::Local(rv) = freevar.res {
490+
let freevar_ln = ir.add_live_node(FreeVarNode(freevar.span));
491+
Some(CaptureInfo { ln: freevar_ln, var_hid: rv })
491492
} else {
492493
None
493494
}
494495
}));
495-
});
496+
}
496497
ir.set_captures(expr.hir_id, call_caps);
497498

498499
intravisit::walk_expr(ir, expr);

src/librustc/mir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2572,12 +2572,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
25722572
};
25732573
let mut struct_fmt = fmt.debug_struct(&name);
25742574

2575-
tcx.with_freevars(hir_id, |freevars| {
2575+
if let Some(freevars) = tcx.freevars(def_id) {
25762576
for (freevar, place) in freevars.iter().zip(places) {
25772577
let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
25782578
struct_fmt.field(&var_name.as_str(), place);
25792579
}
2580-
});
2580+
}
25812581

25822582
struct_fmt.finish()
25832583
} else {
@@ -2591,12 +2591,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
25912591
tcx.hir().span_by_hir_id(hir_id));
25922592
let mut struct_fmt = fmt.debug_struct(&name);
25932593

2594-
tcx.with_freevars(hir_id, |freevars| {
2594+
if let Some(freevars) = tcx.freevars(def_id) {
25952595
for (freevar, place) in freevars.iter().zip(places) {
25962596
let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
25972597
struct_fmt.field(&var_name.as_str(), place);
25982598
}
2599-
});
2599+
}
26002600

26012601
struct_fmt.finish()
26022602
} else {

src/librustc/ty/mod.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use self::IntVarValue::*;
99
pub use self::fold::TypeFoldable;
1010

1111
use crate::hir::{map as hir_map, FreevarMap, GlobMap, TraitMap};
12-
use crate::hir::{HirId, Node};
12+
use crate::hir::Node;
1313
use crate::hir::def::{Res, DefKind, CtorOf, CtorKind, ExportMap};
1414
use crate::hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1515
use rustc_data_structures::svh::Svh;
@@ -3120,18 +3120,6 @@ impl Iterator for AssociatedItemsIterator<'_, '_, '_> {
31203120
}
31213121
}
31223122

3123-
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
3124-
pub fn with_freevars<T, F>(self, fid: HirId, f: F) -> T where
3125-
F: FnOnce(&[hir::Freevar]) -> T,
3126-
{
3127-
let def_id = self.hir().local_def_id_from_hir_id(fid);
3128-
match self.freevars(def_id) {
3129-
None => f(&[]),
3130-
Some(d) => f(&d),
3131-
}
3132-
}
3133-
}
3134-
31353123
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> AssociatedItem {
31363124
let id = tcx.hir().as_local_hir_id(def_id).unwrap();
31373125
let parent_id = tcx.hir().get_parent_item(id);

src/librustc/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
584584
let mut sep = " ";
585585
for (freevar, upvar_ty) in self.tcx().freevars(did)
586586
.as_ref()
587-
.map_or(&[][..], |fv| &fv[..])
587+
.map_or(&[][..], |v| &v[..])
588588
.iter()
589589
.zip(upvar_tys)
590590
{
@@ -627,7 +627,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
627627
let mut sep = " ";
628628
for (freevar, upvar_ty) in self.tcx().freevars(did)
629629
.as_ref()
630-
.map_or(&[][..], |fv| &fv[..])
630+
.map_or(&[][..], |v| &v[..])
631631
.iter()
632632
.zip(upvar_tys)
633633
{

src/librustc_mir/borrow_check/error_reporting.rs

+13-26
Original file line numberDiff line numberDiff line change
@@ -1814,14 +1814,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18141814
ty::Array(ty, _) | ty::Slice(ty) =>
18151815
self.describe_field_from_ty(&ty, field, variant_index),
18161816
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
1817-
// Convert the def-id into a node-id. node-ids are only valid for
1818-
// the local code in the current crate, so this returns an `Option` in case
1817+
// `tcx.freevars(def_id)` returns an `Option`, which is `None` in case
18191818
// the closure comes from another crate. But in that case we wouldn't
18201819
// be borrowck'ing it, so we can just unwrap:
1821-
let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id).unwrap();
1822-
let freevar = self.infcx
1823-
.tcx
1824-
.with_freevars(hir_id, |fv| fv[field.index()]);
1820+
let freevar = self.infcx.tcx.freevars(def_id).unwrap()[field.index()];
18251821

18261822
self.infcx.tcx.hir().name_by_hir_id(freevar.var_id()).to_string()
18271823
}
@@ -2613,28 +2609,19 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
26132609
if let hir::ExprKind::Closure(
26142610
.., args_span, _
26152611
) = expr {
2616-
let var_span = self.infcx.tcx.with_freevars(
2617-
hir_id,
2618-
|freevars| {
2619-
for (v, place) in freevars.iter().zip(places) {
2620-
match place {
2621-
Operand::Copy(place) |
2622-
Operand::Move(place) if target_place == place => {
2623-
debug!("closure_span: found captured local {:?}", place);
2624-
return Some(v.span);
2625-
},
2626-
_ => {}
2627-
}
2628-
}
2629-
2630-
None
2631-
},
2632-
)?;
2612+
for (v, place) in self.infcx.tcx.freevars(def_id)?.iter().zip(places) {
2613+
match place {
2614+
Operand::Copy(place) |
2615+
Operand::Move(place) if target_place == place => {
2616+
debug!("closure_span: found captured local {:?}", place);
2617+
return Some((*args_span, v.span));
2618+
},
2619+
_ => {}
2620+
}
2621+
}
26332622

2634-
Some((*args_span, var_span))
2635-
} else {
2636-
None
26372623
}
2624+
None
26382625
}
26392626

26402627
/// Helper to retrieve span(s) of given borrow from the current MIR

src/librustc_mir/hair/cx/expr.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,11 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
516516
span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
517517
}
518518
};
519-
let upvars = cx.tcx.with_freevars(expr.hir_id, |freevars| {
520-
freevars.iter()
521-
.zip(substs.upvar_tys(def_id, cx.tcx))
522-
.map(|(fv, ty)| capture_freevar(cx, expr, fv, ty))
523-
.collect()
524-
});
519+
let upvars = cx.tcx.freevars(def_id).iter()
520+
.flat_map(|freevars| freevars.iter())
521+
.zip(substs.upvar_tys(def_id, cx.tcx))
522+
.map(|(freevar, ty)| capture_freevar(cx, expr, freevar, ty))
523+
.collect();
525524
ExprKind::Closure {
526525
closure_id: def_id,
527526
substs,

src/librustc_passes/rvalue_promotion.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ fn check_expr_kind<'a, 'tcx>(
449449
let nested_body_promotable = v.check_nested_body(body_id);
450450
// Paths in constant contexts cannot refer to local variables,
451451
// as there are none, and thus closures can't have upvars there.
452-
if v.tcx.with_freevars(e.hir_id, |fv| !fv.is_empty()) {
452+
let closure_def_id = v.tcx.hir().local_def_id_from_hir_id(e.hir_id);
453+
if !v.tcx.freevars(closure_def_id).map_or(true, |v| v.is_empty()) {
453454
NotPromotable
454455
} else {
455456
nested_body_promotable

src/librustc_typeck/check/coercion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
721721
722722
let b = self.shallow_resolve(b);
723723

724-
let hir_id_a = self.tcx.hir().as_local_hir_id(def_id_a).unwrap();
725724
match b.sty {
726-
ty::FnPtr(fn_ty) if self.tcx.with_freevars(hir_id_a, |v| v.is_empty()) => {
725+
ty::FnPtr(fn_ty) if self.tcx.freevars(def_id_a).map_or(true, |v| v.is_empty()) => {
727726
// We coerce the closure, which has fn type
728727
// `extern "rust-call" fn((arg0,arg1,...)) -> _`
729728
// to

src/librustc_typeck/check/upvar.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
121121
None
122122
};
123123

124-
self.tcx.with_freevars(closure_hir_id, |freevars| {
124+
if let Some(freevars) = self.tcx.freevars(closure_def_id) {
125125
let mut freevar_list: Vec<ty::UpvarId> = Vec::with_capacity(freevars.len());
126-
for freevar in freevars {
126+
for freevar in freevars.iter() {
127127
let upvar_id = ty::UpvarId {
128128
var_path: ty::UpvarPath {
129129
hir_id: freevar.var_id(),
@@ -155,14 +155,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
155155
}
156156
// Add the vector of freevars to the map keyed with the closure id.
157157
// This gives us an easier access to them without having to call
158-
// with_freevars again..
158+
// tcx.freevars again..
159159
if !freevar_list.is_empty() {
160160
self.tables
161161
.borrow_mut()
162162
.upvar_list
163163
.insert(closure_def_id, freevar_list);
164164
}
165-
});
165+
}
166166

167167
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
168168
let region_scope_tree = &self.tcx.region_scope_tree(body_owner_def_id);
@@ -244,17 +244,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
244244
// This may change if abstract return types of some sort are
245245
// implemented.
246246
let tcx = self.tcx;
247-
let closure_def_index = tcx.hir().local_def_id_from_hir_id(closure_id);
247+
let closure_def_id = tcx.hir().local_def_id_from_hir_id(closure_id);
248248

249-
tcx.with_freevars(closure_id, |freevars| {
249+
tcx.freevars(closure_def_id).iter().flat_map(|freevars| {
250250
freevars
251251
.iter()
252252
.map(|freevar| {
253253
let var_hir_id = freevar.var_id();
254254
let freevar_ty = self.node_ty(var_hir_id);
255255
let upvar_id = ty::UpvarId {
256256
var_path: ty::UpvarPath { hir_id: var_hir_id },
257-
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
257+
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
258258
};
259259
let capture = self.tables.borrow().upvar_capture(upvar_id);
260260

@@ -274,8 +274,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
274274
),
275275
}
276276
})
277-
.collect()
278277
})
278+
.collect()
279279
}
280280
}
281281

src/librustc_typeck/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,8 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
10931093
}),
10941094
);
10951095

1096-
tcx.with_freevars(hir_id, |fv| {
1097-
params.extend(fv.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
1096+
if let Some(freevars) = tcx.freevars(def_id) {
1097+
params.extend(freevars.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
10981098
ty::GenericParamDef {
10991099
index: type_start + i,
11001100
name: Symbol::intern("<upvar>").as_interned_str(),
@@ -1107,7 +1107,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
11071107
},
11081108
}
11091109
}));
1110-
});
1110+
}
11111111
}
11121112

11131113
let param_def_id_to_index = params

0 commit comments

Comments
 (0)