@@ -74,7 +74,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
74
74
75
75
struct SeedBorrowKind < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
76
76
fcx : & ' a FnCtxt < ' a , ' gcx , ' tcx > ,
77
- temp_closure_kinds : NodeMap < ty:: ClosureKind > ,
77
+ temp_closure_kinds : NodeMap < ( ty:: ClosureKind , Option < ( Span , ast :: Name ) > ) > ,
78
78
}
79
79
80
80
impl < ' a , ' gcx , ' tcx > Visitor < ' gcx > for SeedBorrowKind < ' a , ' gcx , ' tcx > {
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> SeedBorrowKind<'a, 'gcx, 'tcx> {
107
107
capture_clause : hir:: CaptureClause )
108
108
{
109
109
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 ) ) ;
111
111
debug ! ( "check_closure: adding closure {:?} as Fn" , expr. id) ;
112
112
}
113
113
@@ -143,12 +143,12 @@ impl<'a, 'gcx, 'tcx> SeedBorrowKind<'a, 'gcx, 'tcx> {
143
143
144
144
struct AdjustBorrowKind < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
145
145
fcx : & ' a FnCtxt < ' a , ' gcx , ' tcx > ,
146
- temp_closure_kinds : NodeMap < ty:: ClosureKind > ,
146
+ temp_closure_kinds : NodeMap < ( ty:: ClosureKind , Option < ( Span , ast :: Name ) > ) > ,
147
147
}
148
148
149
149
impl < ' a , ' gcx , ' tcx > AdjustBorrowKind < ' a , ' gcx , ' tcx > {
150
150
fn new ( fcx : & ' a FnCtxt < ' a , ' gcx , ' tcx > ,
151
- temp_closure_kinds : NodeMap < ty:: ClosureKind > )
151
+ temp_closure_kinds : NodeMap < ( ty:: ClosureKind , Option < ( Span , ast :: Name ) > ) > )
152
152
-> AdjustBorrowKind < ' a , ' gcx , ' tcx > {
153
153
AdjustBorrowKind { fcx : fcx, temp_closure_kinds : temp_closure_kinds }
154
154
}
@@ -211,8 +211,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
211
211
212
212
// If we are also inferred the closure kind here, update the
213
213
// 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, context ) ) = self . temp_closure_kinds . get ( & id) {
215
+ self . fcx . tables . borrow_mut ( ) . closure_kinds . insert ( id, ( kind, context ) ) ;
216
216
let closure_def_id = self . fcx . tcx . hir . local_def_id ( id) ;
217
217
debug ! ( "closure_kind({:?}) = {:?}" , closure_def_id, kind) ;
218
218
@@ -272,6 +272,8 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
272
272
euv:: Move ( _) => { }
273
273
}
274
274
275
+ let tcx = self . fcx . tcx ;
276
+
275
277
// watch out for a move of the deref of a borrowed pointer;
276
278
// for that to be legal, the upvar would have to be borrowed
277
279
// by value instead
@@ -289,7 +291,9 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
289
291
290
292
// to move out of an upvar, this must be a FnOnce closure
291
293
self . adjust_closure_kind ( upvar_id. closure_expr_id ,
292
- ty:: ClosureKind :: FnOnce ) ;
294
+ ty:: ClosureKind :: FnOnce ,
295
+ guarantor. span ,
296
+ tcx. hir . name ( upvar_id. var_id ) ) ;
293
297
294
298
let upvar_capture_map =
295
299
& mut self . fcx . tables . borrow_mut ( ) . upvar_capture_map ;
@@ -303,7 +307,9 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
303
307
// to be a FnOnce closure to permit moves out
304
308
// of the environment.
305
309
self . adjust_closure_kind ( upvar_id. closure_expr_id ,
306
- ty:: ClosureKind :: FnOnce ) ;
310
+ ty:: ClosureKind :: FnOnce ,
311
+ guarantor. span ,
312
+ tcx. hir . name ( upvar_id. var_id ) ) ;
307
313
}
308
314
mc:: NoteNone => {
309
315
}
@@ -331,7 +337,7 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
331
337
332
338
Categorization :: Deref ( base, _, mc:: BorrowedPtr ( ..) ) |
333
339
Categorization :: Deref ( base, _, mc:: Implicit ( ..) ) => {
334
- if !self . try_adjust_upvar_deref ( & cmt. note , ty:: MutBorrow ) {
340
+ if !self . try_adjust_upvar_deref ( cmt, ty:: MutBorrow ) {
335
341
// assignment to deref of an `&mut`
336
342
// borrowed pointer implies that the
337
343
// pointer itself must be unique, but not
@@ -365,7 +371,7 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
365
371
366
372
Categorization :: Deref ( base, _, mc:: BorrowedPtr ( ..) ) |
367
373
Categorization :: Deref ( base, _, mc:: Implicit ( ..) ) => {
368
- if !self . try_adjust_upvar_deref ( & cmt. note , ty:: UniqueImmBorrow ) {
374
+ if !self . try_adjust_upvar_deref ( cmt, ty:: UniqueImmBorrow ) {
369
375
// for a borrowed pointer to be unique, its
370
376
// base must be unique
371
377
self . adjust_upvar_borrow_kind_for_unique ( base) ;
@@ -382,7 +388,7 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
382
388
}
383
389
384
390
fn try_adjust_upvar_deref ( & mut self ,
385
- note : & mc:: Note ,
391
+ cmt : mc:: cmt < ' tcx > ,
386
392
borrow_kind : ty:: BorrowKind )
387
393
-> bool
388
394
{
@@ -394,7 +400,9 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
394
400
ty:: ImmBorrow => false ,
395
401
} ) ;
396
402
397
- match * note {
403
+ let tcx = self . fcx . tcx ;
404
+
405
+ match cmt. note {
398
406
mc:: NoteUpvarRef ( upvar_id) => {
399
407
// if this is an implicit deref of an
400
408
// upvar, then we need to modify the
@@ -407,15 +415,21 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
407
415
}
408
416
409
417
// 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 ) ;
418
+ self . adjust_closure_kind ( upvar_id. closure_expr_id ,
419
+ ty:: ClosureKind :: FnMut ,
420
+ cmt. span ,
421
+ tcx. hir . name ( upvar_id. var_id ) ) ;
411
422
412
423
true
413
424
}
414
425
mc:: NoteClosureEnv ( upvar_id) => {
415
426
// this kind of deref occurs in a `move` closure, or
416
427
// for a by-value upvar; in either case, to mutate an
417
428
// upvar, we need to be an FnMut closure
418
- self . adjust_closure_kind ( upvar_id. closure_expr_id , ty:: ClosureKind :: FnMut ) ;
429
+ self . adjust_closure_kind ( upvar_id. closure_expr_id ,
430
+ ty:: ClosureKind :: FnMut ,
431
+ cmt. span ,
432
+ tcx. hir . name ( upvar_id. var_id ) ) ;
419
433
420
434
true
421
435
}
@@ -462,11 +476,13 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
462
476
463
477
fn adjust_closure_kind ( & mut self ,
464
478
closure_id : ast:: NodeId ,
465
- new_kind : ty:: ClosureKind ) {
466
- debug ! ( "adjust_closure_kind(closure_id={}, new_kind={:?})" ,
467
- closure_id, new_kind) ;
479
+ new_kind : ty:: ClosureKind ,
480
+ upvar_span : Span ,
481
+ var_name : ast:: Name ) {
482
+ debug ! ( "adjust_closure_kind(closure_id={}, new_kind={:?}, upvar_span={:?}, var_name={})" ,
483
+ closure_id, new_kind, upvar_span, var_name) ;
468
484
469
- if let Some ( & existing_kind) = self . temp_closure_kinds . get ( & closure_id) {
485
+ if let Some ( & ( existing_kind, _ ) ) = self . temp_closure_kinds . get ( & closure_id) {
470
486
debug ! ( "adjust_closure_kind: closure_id={}, existing_kind={:?}, new_kind={:?}" ,
471
487
closure_id, existing_kind, new_kind) ;
472
488
@@ -482,7 +498,10 @@ impl<'a, 'gcx, 'tcx> AdjustBorrowKind<'a, 'gcx, 'tcx> {
482
498
( ty:: ClosureKind :: Fn , ty:: ClosureKind :: FnOnce ) |
483
499
( ty:: ClosureKind :: FnMut , ty:: ClosureKind :: FnOnce ) => {
484
500
// new kind is stronger than the old kind
485
- self . temp_closure_kinds . insert ( closure_id, new_kind) ;
501
+ self . temp_closure_kinds . insert (
502
+ closure_id,
503
+ ( new_kind, Some ( ( upvar_span, var_name) ) )
504
+ ) ;
486
505
}
487
506
}
488
507
}
0 commit comments