Skip to content

Commit 7748bc6

Browse files
committed
Include context info into closure_kinds
1 parent 5b13bff commit 7748bc6

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

src/librustc/infer/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16821682
{
16831683
if let InferTables::InProgress(tables) = self.tables {
16841684
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
1685-
return tables.borrow().closure_kinds.get(&id).cloned();
1685+
return tables.borrow()
1686+
.closure_kinds
1687+
.get(&id)
1688+
.cloned()
1689+
.map(|(kind, _)| kind);
16861690
}
16871691
}
16881692

src/librustc/ty/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use syntax::abi;
5858
use syntax::ast::{self, Name, NodeId};
5959
use syntax::attr;
6060
use syntax::symbol::{Symbol, keywords};
61+
use syntax_pos::Span;
6162

6263
use hir;
6364

@@ -229,8 +230,9 @@ pub struct TypeckTables<'tcx> {
229230
/// Records the type of each closure.
230231
pub closure_tys: NodeMap<ty::PolyFnSig<'tcx>>,
231232

232-
/// Records the kind of each closure.
233-
pub closure_kinds: NodeMap<ty::ClosureKind>,
233+
/// Records the kind of each closure and the span of the variable that
234+
/// cause the closure to be this kind.
235+
pub closure_kinds: NodeMap<(ty::ClosureKind, Option<Span>)>,
234236

235237
/// For each fn, records the "liberated" types of its arguments
236238
/// and return type. Liberated means that all bound regions

src/librustc_typeck/check/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
103103
self.tables.borrow_mut().closure_tys.insert(expr.id, sig);
104104
match opt_kind {
105105
Some(kind) => {
106-
self.tables.borrow_mut().closure_kinds.insert(expr.id, kind);
106+
self.tables.borrow_mut().closure_kinds.insert(expr.id, (kind, None));
107107
}
108108
None => {}
109109
}

src/librustc_typeck/check/method/probe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
802802

803803
let closure_kinds = &self.tables.borrow().closure_kinds;
804804
let closure_kind = match closure_kinds.get(&closure_id) {
805-
Some(&k) => k,
805+
Some(&(k, _)) => k,
806806
None => {
807807
return Err(MethodError::ClosureAmbiguity(trait_def_id));
808808
}

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ fn closure_kind<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
702702
def_id: DefId)
703703
-> ty::ClosureKind {
704704
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
705-
tcx.typeck_tables_of(def_id).closure_kinds[&node_id]
705+
tcx.typeck_tables_of(def_id).closure_kinds[&node_id].0
706706
}
707707

708708
fn adt_destructor<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustc_typeck/check/upvar.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
7474

7575
struct SeedBorrowKind<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
7676
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
77-
temp_closure_kinds: NodeMap<ty::ClosureKind>,
77+
temp_closure_kinds: NodeMap<(ty::ClosureKind, Option<Span>)>,
7878
}
7979

8080
impl<'a, 'gcx, 'tcx> Visitor<'gcx> for SeedBorrowKind<'a, 'gcx, 'tcx> {
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> SeedBorrowKind<'a, 'gcx, 'tcx> {
107107
capture_clause: hir::CaptureClause)
108108
{
109109
if !self.fcx.tables.borrow().closure_kinds.contains_key(&expr.id) {
110-
self.temp_closure_kinds.insert(expr.id, ty::ClosureKind::Fn);
110+
self.temp_closure_kinds.insert(expr.id, (ty::ClosureKind::Fn, None));
111111
debug!("check_closure: adding closure {:?} as Fn", expr.id);
112112
}
113113

@@ -143,12 +143,12 @@ impl<'a, 'gcx, 'tcx> SeedBorrowKind<'a, 'gcx, 'tcx> {
143143

144144
struct AdjustBorrowKind<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
145145
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
146-
temp_closure_kinds: NodeMap<ty::ClosureKind>,
146+
temp_closure_kinds: NodeMap<(ty::ClosureKind, Option<Span>)>,
147147
}
148148

149149
impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
150150
fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
151-
temp_closure_kinds: NodeMap<ty::ClosureKind>)
151+
temp_closure_kinds: NodeMap<(ty::ClosureKind, Option<Span>)>)
152152
-> AdjustBorrowKind<'a, 'gcx, 'tcx> {
153153
AdjustBorrowKind { fcx: fcx, temp_closure_kinds: temp_closure_kinds }
154154
}
@@ -211,8 +211,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
211211

212212
// If we are also inferred the closure kind here, update the
213213
// main table and process any deferred resolutions.
214-
if let Some(&kind) = self.temp_closure_kinds.get(&id) {
215-
self.fcx.tables.borrow_mut().closure_kinds.insert(id, kind);
214+
if let Some(&(kind, span)) = self.temp_closure_kinds.get(&id) {
215+
self.fcx.tables.borrow_mut().closure_kinds.insert(id, (kind, span));
216216
let closure_def_id = self.fcx.tcx.hir.local_def_id(id);
217217
debug!("closure_kind({:?}) = {:?}", closure_def_id, kind);
218218

@@ -276,6 +276,7 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
276276
// for that to be legal, the upvar would have to be borrowed
277277
// by value instead
278278
let guarantor = cmt.guarantor();
279+
let tcx = self.fcx.tcx;
279280
debug!("adjust_upvar_borrow_kind_for_consume: guarantor={:?}",
280281
guarantor);
281282
match guarantor.cat {
@@ -289,7 +290,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
289290

290291
// to move out of an upvar, this must be a FnOnce closure
291292
self.adjust_closure_kind(upvar_id.closure_expr_id,
292-
ty::ClosureKind::FnOnce);
293+
ty::ClosureKind::FnOnce,
294+
tcx.hir.span(upvar_id.var_id));
293295

294296
let upvar_capture_map =
295297
&mut self.fcx.tables.borrow_mut().upvar_capture_map;
@@ -303,7 +305,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
303305
// to be a FnOnce closure to permit moves out
304306
// of the environment.
305307
self.adjust_closure_kind(upvar_id.closure_expr_id,
306-
ty::ClosureKind::FnOnce);
308+
ty::ClosureKind::FnOnce,
309+
tcx.hir.span(upvar_id.var_id));
307310
}
308311
mc::NoteNone => {
309312
}
@@ -394,6 +397,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
394397
ty::ImmBorrow => false,
395398
});
396399

400+
let tcx = self.fcx.tcx;
401+
397402
match *note {
398403
mc::NoteUpvarRef(upvar_id) => {
399404
// if this is an implicit deref of an
@@ -407,15 +412,19 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
407412
}
408413

409414
// also need to be in an FnMut closure since this is not an ImmBorrow
410-
self.adjust_closure_kind(upvar_id.closure_expr_id, ty::ClosureKind::FnMut);
415+
self.adjust_closure_kind(upvar_id.closure_expr_id,
416+
ty::ClosureKind::FnMut,
417+
tcx.hir.span(upvar_id.var_id));
411418

412419
true
413420
}
414421
mc::NoteClosureEnv(upvar_id) => {
415422
// this kind of deref occurs in a `move` closure, or
416423
// for a by-value upvar; in either case, to mutate an
417424
// upvar, we need to be an FnMut closure
418-
self.adjust_closure_kind(upvar_id.closure_expr_id, ty::ClosureKind::FnMut);
425+
self.adjust_closure_kind(upvar_id.closure_expr_id,
426+
ty::ClosureKind::FnMut,
427+
tcx.hir.span(upvar_id.var_id));
419428

420429
true
421430
}
@@ -462,11 +471,12 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
462471

463472
fn adjust_closure_kind(&mut self,
464473
closure_id: ast::NodeId,
465-
new_kind: ty::ClosureKind) {
474+
new_kind: ty::ClosureKind,
475+
upvar_span: Span) {
466476
debug!("adjust_closure_kind(closure_id={}, new_kind={:?})",
467477
closure_id, new_kind);
468478

469-
if let Some(&existing_kind) = self.temp_closure_kinds.get(&closure_id) {
479+
if let Some(&(existing_kind, _)) = self.temp_closure_kinds.get(&closure_id) {
470480
debug!("adjust_closure_kind: closure_id={}, existing_kind={:?}, new_kind={:?}",
471481
closure_id, existing_kind, new_kind);
472482

@@ -482,7 +492,7 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
482492
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
483493
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
484494
// new kind is stronger than the old kind
485-
self.temp_closure_kinds.insert(closure_id, new_kind);
495+
self.temp_closure_kinds.insert(closure_id, (new_kind, Some(upvar_span)));
486496
}
487497
}
488498
}

0 commit comments

Comments
 (0)