Skip to content

Commit 2c7712b

Browse files
authored
Rollup merge of #60530 - eddyb:unfreevars, r=oli-obk
rustc: rename all occurences of "freevar" to "upvar". Most of the more recent code talks about "(closure) upvars", so I believe that's the name we want to use. There's also the possibility of using "capture" which is more user-facing, but I'd rather not change *both* "freevar" and "upvar" to something else in this one PR. cc @nikomatsakis @petrochenkov
2 parents d475c4e + 8d9f4a1 commit 2c7712b

File tree

20 files changed

+120
-145
lines changed

20 files changed

+120
-145
lines changed

src/librustc/hir/def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub enum Res<Id = hir::HirId> {
140140
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
141141
Local(Id),
142142
Upvar(Id, // `HirId` of closed over local
143-
usize, // index in the `freevars` list of the closure
143+
usize, // index in the `upvars` list of the closure
144144
ast::NodeId), // expr node that creates the closure
145145

146146
// Macro namespace

src/librustc/hir/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2476,19 +2476,19 @@ impl ForeignItemKind {
24762476
}
24772477
}
24782478

2479-
/// A free variable referred to in a function.
2479+
/// A variable captured by a closure.
24802480
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
2481-
pub struct Freevar<Id = HirId> {
2482-
/// The variable being accessed free.
2481+
pub struct Upvar<Id = HirId> {
2482+
/// The variable being captured.
24832483
pub res: Res<Id>,
24842484

24852485
// First span where it is accessed (there can be multiple).
24862486
pub span: Span
24872487
}
24882488

2489-
impl<Id: fmt::Debug + Copy> Freevar<Id> {
2490-
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Freevar<R> {
2491-
Freevar {
2489+
impl<Id: fmt::Debug + Copy> Upvar<Id> {
2490+
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Upvar<R> {
2491+
Upvar {
24922492
res: self.res.map_id(map),
24932493
span: self.span,
24942494
}
@@ -2497,12 +2497,12 @@ impl<Id: fmt::Debug + Copy> Freevar<Id> {
24972497
pub fn var_id(&self) -> Id {
24982498
match self.res {
24992499
Res::Local(id) | Res::Upvar(id, ..) => id,
2500-
_ => bug!("Freevar::var_id: bad res ({:?})", self.res)
2500+
_ => bug!("Upvar::var_id: bad res ({:?})", self.res)
25012501
}
25022502
}
25032503
}
25042504

2505-
pub type FreevarMap = NodeMap<Vec<Freevar<ast::NodeId>>>;
2505+
pub type UpvarMap = NodeMap<Vec<Upvar<ast::NodeId>>>;
25062506

25072507
pub type CaptureModeMap = NodeMap<CaptureClause>;
25082508

src/librustc/infer/error_reporting/note.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
4646
err.span_note(span,
4747
"...so that pointer is not dereferenced outside its lifetime");
4848
}
49-
infer::FreeVariable(span, id) => {
49+
infer::ClosureCapture(span, id) => {
5050
err.span_note(span,
5151
&format!("...so that captured variable `{}` does not outlive the \
5252
enclosing closure",
@@ -214,7 +214,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
214214
"the reference is only valid for ", sup, "");
215215
err
216216
}
217-
infer::FreeVariable(span, id) => {
217+
infer::ClosureCapture(span, id) => {
218218
let mut err = struct_span_err!(self.tcx.sess,
219219
span,
220220
E0474,

src/librustc/infer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ pub enum SubregionOrigin<'tcx> {
264264
/// Dereference of reference must be within its lifetime
265265
DerefPointer(Span),
266266

267-
/// Closure bound must not outlive captured free variables
268-
FreeVariable(Span, ast::NodeId),
267+
/// Closure bound must not outlive captured variables
268+
ClosureCapture(Span, ast::NodeId),
269269

270270
/// Index into slice must be within its lifetime
271271
IndexSlice(Span),
@@ -1660,7 +1660,7 @@ impl<'tcx> SubregionOrigin<'tcx> {
16601660
InfStackClosure(a) => a,
16611661
InvokeClosure(a) => a,
16621662
DerefPointer(a) => a,
1663-
FreeVariable(a, _) => a,
1663+
ClosureCapture(a, _) => a,
16641664
IndexSlice(a) => a,
16651665
RelateObjectBound(a) => a,
16661666
RelateParamBound(a, _) => a,

src/librustc/middle/expr_use_visitor.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -931,42 +931,42 @@ 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 {
936-
let var_hir_id = freevar.var_id();
934+
if let Some(upvars) = self.tcx().upvars(closure_def_id) {
935+
for upvar in upvars.iter() {
936+
let var_hir_id = upvar.var_id();
937937
let upvar_id = ty::UpvarId {
938938
var_path: ty::UpvarPath { hir_id: var_hir_id },
939939
closure_expr_id: closure_def_id.to_local(),
940940
};
941941
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
942942
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.hir_id,
943943
fn_decl_span,
944-
freevar));
944+
upvar));
945945
match upvar_capture {
946946
ty::UpvarCapture::ByValue => {
947947
let mode = copy_or_move(&self.mc,
948948
self.param_env,
949949
&cmt_var,
950950
CaptureMove);
951-
self.delegate.consume(closure_expr.hir_id, freevar.span, &cmt_var, mode);
951+
self.delegate.consume(closure_expr.hir_id, upvar.span, &cmt_var, mode);
952952
}
953953
ty::UpvarCapture::ByRef(upvar_borrow) => {
954954
self.delegate.borrow(closure_expr.hir_id,
955955
fn_decl_span,
956956
&cmt_var,
957957
upvar_borrow.region,
958958
upvar_borrow.kind,
959-
ClosureCapture(freevar.span));
959+
ClosureCapture(upvar.span));
960960
}
961961
}
962962
}
963-
});
963+
}
964964
}
965965

966966
fn cat_captured_var(&mut self,
967967
closure_hir_id: hir::HirId,
968968
closure_span: Span,
969-
upvar: &hir::Freevar)
969+
upvar: &hir::Upvar)
970970
-> mc::McResult<mc::cmt_<'tcx>> {
971971
// Create the cmt for the variable being borrowed, from the
972972
// caller's perspective

src/librustc/middle/liveness.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl LiveNode {
144144

145145
#[derive(Copy, Clone, PartialEq, Debug)]
146146
enum LiveNodeKind {
147-
FreeVarNode(Span),
147+
UpvarNode(Span),
148148
ExprNode(Span),
149149
VarDefNode(Span),
150150
ExitNode
@@ -153,8 +153,8 @@ enum LiveNodeKind {
153153
fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_, '_, '_>) -> String {
154154
let cm = tcx.sess.source_map();
155155
match lnk {
156-
FreeVarNode(s) => {
157-
format!("Free var node [{}]", cm.span_to_string(s))
156+
UpvarNode(s) => {
157+
format!("Upvar node [{}]", cm.span_to_string(s))
158158
}
159159
ExprNode(s) => {
160160
format!("Expr node [{}]", cm.span_to_string(s))
@@ -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(upvars) = ir.tcx.upvars(closure_def_id) {
488+
call_caps.extend(upvars.iter().filter_map(|upvar| {
489+
if let Res::Local(rv) = upvar.res {
490+
let upvar_ln = ir.add_live_node(UpvarNode(upvar.span));
491+
Some(CaptureInfo { ln: upvar_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

+8-8
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| {
2576-
for (freevar, place) in freevars.iter().zip(places) {
2577-
let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
2575+
if let Some(upvars) = tcx.upvars(def_id) {
2576+
for (upvar, place) in upvars.iter().zip(places) {
2577+
let var_name = tcx.hir().name_by_hir_id(upvar.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| {
2595-
for (freevar, place) in freevars.iter().zip(places) {
2596-
let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
2594+
if let Some(upvars) = tcx.upvars(def_id) {
2595+
for (upvar, place) in upvars.iter().zip(places) {
2596+
let var_name = tcx.hir().name_by_hir_id(upvar.var_id());
25972597
struct_fmt.field(&var_name.as_str(), place);
25982598
}
2599-
});
2599+
}
26002600

26012601
struct_fmt.finish()
26022602
} else {

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ rustc_queries! {
824824
desc { "generating a postorder list of CrateNums" }
825825
}
826826

827-
query freevars(_: DefId) -> Option<Lrc<Vec<hir::Freevar>>> {
827+
query upvars(_: DefId) -> Option<Lrc<Vec<hir::Upvar>>> {
828828
eval_always
829829
}
830830
query maybe_unused_trait_import(_: DefId) -> bool {

src/librustc/ty/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1071,10 +1071,10 @@ pub struct GlobalCtxt<'tcx> {
10711071

10721072
pub queries: query::Queries<'tcx>,
10731073

1074-
// Records the free variables referenced by every closure
1074+
// Records the captured variables referenced by every closure
10751075
// expression. Do not track deps for this, just recompute it from
10761076
// scratch every time.
1077-
freevars: FxHashMap<DefId, Lrc<Vec<hir::Freevar>>>,
1077+
upvars: FxHashMap<DefId, Lrc<Vec<hir::Upvar>>>,
10781078

10791079
maybe_unused_trait_imports: FxHashSet<DefId>,
10801080
maybe_unused_extern_crates: Vec<(DefId, Span)>,
@@ -1317,7 +1317,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13171317
}).collect();
13181318
(k, Lrc::new(exports))
13191319
}).collect(),
1320-
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
1320+
upvars: resolutions.upvars.into_iter().map(|(k, v)| {
13211321
let vars: Vec<_> = v.into_iter().map(|e| {
13221322
e.map_id(|id| hir.node_to_hir_id(id))
13231323
}).collect();
@@ -3055,7 +3055,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
30553055
assert_eq!(id, LOCAL_CRATE);
30563056
Lrc::new(middle::lang_items::collect(tcx))
30573057
};
3058-
providers.freevars = |tcx, id| tcx.gcx.freevars.get(&id).cloned();
3058+
providers.upvars = |tcx, id| tcx.gcx.upvars.get(&id).cloned();
30593059
providers.maybe_unused_trait_import = |tcx, id| {
30603060
tcx.maybe_unused_trait_imports.contains(&id)
30613061
};

src/librustc/ty/mod.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub use self::BorrowKind::*;
88
pub use self::IntVarValue::*;
99
pub use self::fold::TypeFoldable;
1010

11-
use crate::hir::{map as hir_map, FreevarMap, GlobMap, TraitMap};
12-
use crate::hir::{HirId, Node};
11+
use crate::hir::{map as hir_map, UpvarMap, GlobMap, TraitMap};
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;
@@ -122,7 +122,7 @@ mod sty;
122122

123123
#[derive(Clone)]
124124
pub struct Resolutions {
125-
pub freevars: FreevarMap,
125+
pub upvars: UpvarMap,
126126
pub trait_map: TraitMap,
127127
pub maybe_unused_trait_imports: NodeSet,
128128
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
@@ -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

+6-6
Original file line numberDiff line numberDiff line change
@@ -582,16 +582,16 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
582582
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(did) {
583583
p!(write("@{:?}", self.tcx().hir().span_by_hir_id(hir_id)));
584584
let mut sep = " ";
585-
for (freevar, upvar_ty) in self.tcx().freevars(did)
585+
for (upvar, upvar_ty) in self.tcx().upvars(did)
586586
.as_ref()
587-
.map_or(&[][..], |fv| &fv[..])
587+
.map_or(&[][..], |v| &v[..])
588588
.iter()
589589
.zip(upvar_tys)
590590
{
591591
p!(
592592
write("{}{}:",
593593
sep,
594-
self.tcx().hir().name_by_hir_id(freevar.var_id())),
594+
self.tcx().hir().name_by_hir_id(upvar.var_id())),
595595
print(upvar_ty));
596596
sep = ", ";
597597
}
@@ -625,16 +625,16 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
625625
p!(write("@{:?}", self.tcx().hir().span_by_hir_id(hir_id)));
626626
}
627627
let mut sep = " ";
628-
for (freevar, upvar_ty) in self.tcx().freevars(did)
628+
for (upvar, upvar_ty) in self.tcx().upvars(did)
629629
.as_ref()
630-
.map_or(&[][..], |fv| &fv[..])
630+
.map_or(&[][..], |v| &v[..])
631631
.iter()
632632
.zip(upvar_tys)
633633
{
634634
p!(
635635
write("{}{}:",
636636
sep,
637-
self.tcx().hir().name_by_hir_id(freevar.var_id())),
637+
self.tcx().hir().name_by_hir_id(upvar.var_id())),
638638
print(upvar_ty));
639639
sep = ", ";
640640
}

src/librustc_interface/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl ExpansionResult {
180180
ExpansionResult {
181181
defs: Steal::new(resolver.definitions),
182182
resolutions: Steal::new(Resolutions {
183-
freevars: resolver.freevars,
183+
upvars: resolver.upvars,
184184
export_map: resolver.export_map,
185185
trait_map: resolver.trait_map,
186186
glob_map: resolver.glob_map,
@@ -199,7 +199,7 @@ impl ExpansionResult {
199199
ExpansionResult {
200200
defs: Steal::new(resolver.definitions.clone()),
201201
resolutions: Steal::new(Resolutions {
202-
freevars: resolver.freevars.clone(),
202+
upvars: resolver.upvars.clone(),
203203
export_map: resolver.export_map.clone(),
204204
trait_map: resolver.trait_map.clone(),
205205
glob_map: resolver.glob_map.clone(),

0 commit comments

Comments
 (0)