@@ -27,14 +27,31 @@ use rustc_span::Span;
27
27
/// employing the ExprUseVisitor.
28
28
pub trait Delegate < ' tcx > {
29
29
// The value found at `place` is either copied or moved, depending
30
- // on mode.
31
- fn consume ( & mut self , place_with_id : & PlaceWithHirId < ' tcx > , mode : ConsumeMode ) ;
30
+ // on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
31
+ //
32
+ // The parameter `diag_expr_id` indicates the HIR id that ought to be used for
33
+ // diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
34
+ // id will be the id of the expression `expr` but the place itself will have
35
+ // the id of the binding in the pattern `pat`.
36
+ fn consume (
37
+ & mut self ,
38
+ place_with_id : & PlaceWithHirId < ' tcx > ,
39
+ diag_expr_id : hir:: HirId ,
40
+ mode : ConsumeMode ,
41
+ ) ;
32
42
33
43
// The value found at `place` is being borrowed with kind `bk`.
34
- fn borrow ( & mut self , place_with_id : & PlaceWithHirId < ' tcx > , bk : ty:: BorrowKind ) ;
35
-
36
- // The path at `place_with_id` is being assigned to.
37
- fn mutate ( & mut self , assignee_place : & PlaceWithHirId < ' tcx > ) ;
44
+ // `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
45
+ fn borrow (
46
+ & mut self ,
47
+ place_with_id : & PlaceWithHirId < ' tcx > ,
48
+ diag_expr_id : hir:: HirId ,
49
+ bk : ty:: BorrowKind ,
50
+ ) ;
51
+
52
+ // The path at `assignee_place` is being assigned to.
53
+ // `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
54
+ fn mutate ( & mut self , assignee_place : & PlaceWithHirId < ' tcx > , diag_expr_id : hir:: HirId ) ;
38
55
}
39
56
40
57
#[ derive( Copy , Clone , PartialEq , Debug ) ]
@@ -116,11 +133,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
116
133
self . mc . tcx ( )
117
134
}
118
135
119
- fn delegate_consume ( & mut self , place_with_id : & PlaceWithHirId < ' tcx > ) {
136
+ fn delegate_consume ( & mut self , place_with_id : & PlaceWithHirId < ' tcx > , diag_expr_id : hir :: HirId ) {
120
137
debug ! ( "delegate_consume(place_with_id={:?})" , place_with_id) ;
121
138
122
139
let mode = copy_or_move ( & self . mc , place_with_id) ;
123
- self . delegate . consume ( place_with_id, mode) ;
140
+ self . delegate . consume ( place_with_id, diag_expr_id , mode) ;
124
141
}
125
142
126
143
fn consume_exprs ( & mut self , exprs : & [ hir:: Expr < ' _ > ] ) {
@@ -133,21 +150,21 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
133
150
debug ! ( "consume_expr(expr={:?})" , expr) ;
134
151
135
152
let place_with_id = return_if_err ! ( self . mc. cat_expr( expr) ) ;
136
- self . delegate_consume ( & place_with_id) ;
153
+ self . delegate_consume ( & place_with_id, place_with_id . hir_id ) ;
137
154
self . walk_expr ( expr) ;
138
155
}
139
156
140
157
fn mutate_expr ( & mut self , expr : & hir:: Expr < ' _ > ) {
141
158
let place_with_id = return_if_err ! ( self . mc. cat_expr( expr) ) ;
142
- self . delegate . mutate ( & place_with_id) ;
159
+ self . delegate . mutate ( & place_with_id, place_with_id . hir_id ) ;
143
160
self . walk_expr ( expr) ;
144
161
}
145
162
146
163
fn borrow_expr ( & mut self , expr : & hir:: Expr < ' _ > , bk : ty:: BorrowKind ) {
147
164
debug ! ( "borrow_expr(expr={:?}, bk={:?})" , expr, bk) ;
148
165
149
166
let place_with_id = return_if_err ! ( self . mc. cat_expr( expr) ) ;
150
- self . delegate . borrow ( & place_with_id, bk) ;
167
+ self . delegate . borrow ( & place_with_id, place_with_id . hir_id , bk) ;
151
168
152
169
self . walk_expr ( expr)
153
170
}
@@ -404,7 +421,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
404
421
with_field. ty ( self . tcx ( ) , substs) ,
405
422
ProjectionKind :: Field ( f_index as u32 , VariantIdx :: new ( 0 ) ) ,
406
423
) ;
407
- self . delegate_consume ( & field_place) ;
424
+ self . delegate_consume ( & field_place, field_place . hir_id ) ;
408
425
}
409
426
}
410
427
}
@@ -436,7 +453,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
436
453
adjustment:: Adjust :: NeverToAny | adjustment:: Adjust :: Pointer ( _) => {
437
454
// Creating a closure/fn-pointer or unsizing consumes
438
455
// the input and stores it into the resulting rvalue.
439
- self . delegate_consume ( & place_with_id) ;
456
+ self . delegate_consume ( & place_with_id, place_with_id . hir_id ) ;
440
457
}
441
458
442
459
adjustment:: Adjust :: Deref ( None ) => { }
@@ -448,7 +465,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
448
465
// this is an autoref of `x`.
449
466
adjustment:: Adjust :: Deref ( Some ( ref deref) ) => {
450
467
let bk = ty:: BorrowKind :: from_mutbl ( deref. mutbl ) ;
451
- self . delegate . borrow ( & place_with_id, bk) ;
468
+ self . delegate . borrow ( & place_with_id, place_with_id . hir_id , bk) ;
452
469
}
453
470
454
471
adjustment:: Adjust :: Borrow ( ref autoref) => {
@@ -476,13 +493,17 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
476
493
477
494
match * autoref {
478
495
adjustment:: AutoBorrow :: Ref ( _, m) => {
479
- self . delegate . borrow ( base_place, ty:: BorrowKind :: from_mutbl ( m. into ( ) ) ) ;
496
+ self . delegate . borrow (
497
+ base_place,
498
+ base_place. hir_id ,
499
+ ty:: BorrowKind :: from_mutbl ( m. into ( ) ) ,
500
+ ) ;
480
501
}
481
502
482
503
adjustment:: AutoBorrow :: RawPtr ( m) => {
483
504
debug ! ( "walk_autoref: expr.hir_id={} base_place={:?}" , expr. hir_id, base_place) ;
484
505
485
- self . delegate . borrow ( base_place, ty:: BorrowKind :: from_mutbl ( m) ) ;
506
+ self . delegate . borrow ( base_place, base_place . hir_id , ty:: BorrowKind :: from_mutbl ( m) ) ;
486
507
}
487
508
}
488
509
}
@@ -525,19 +546,22 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
525
546
// binding being produced.
526
547
let def = Res :: Local ( canonical_id) ;
527
548
if let Ok ( ref binding_place) = mc. cat_res( pat. hir_id, pat. span, pat_ty, def) {
528
- delegate. mutate( binding_place) ;
549
+ delegate. mutate( binding_place, binding_place . hir_id ) ;
529
550
}
530
551
531
552
// It is also a borrow or copy/move of the value being matched.
553
+ // In a cases of pattern like `let pat = upvar`, don't use the span
554
+ // of the pattern, as this just looks confusing, instead use the span
555
+ // of the discriminant.
532
556
match bm {
533
557
ty:: BindByReference ( m) => {
534
558
let bk = ty:: BorrowKind :: from_mutbl( m) ;
535
- delegate. borrow( place, bk) ;
559
+ delegate. borrow( place, discr_place . hir_id , bk) ;
536
560
}
537
561
ty:: BindByValue ( ..) => {
538
- let mode = copy_or_move( mc, place) ;
562
+ let mode = copy_or_move( mc, & place) ;
539
563
debug!( "walk_pat binding consuming pat" ) ;
540
- delegate. consume( place, mode) ;
564
+ delegate. consume( place, discr_place . hir_id , mode) ;
541
565
}
542
566
}
543
567
}
@@ -564,10 +588,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
564
588
match upvar_capture {
565
589
ty:: UpvarCapture :: ByValue ( _) => {
566
590
let mode = copy_or_move ( & self . mc , & captured_place) ;
567
- self . delegate . consume ( & captured_place, mode) ;
591
+ self . delegate . consume ( & captured_place, captured_place . hir_id , mode) ;
568
592
}
569
593
ty:: UpvarCapture :: ByRef ( upvar_borrow) => {
570
- self . delegate . borrow ( & captured_place, upvar_borrow. kind ) ;
594
+ self . delegate . borrow (
595
+ & captured_place,
596
+ captured_place. hir_id ,
597
+ upvar_borrow. kind ,
598
+ ) ;
571
599
}
572
600
}
573
601
}
0 commit comments