@@ -57,16 +57,33 @@ mod prefixes;
57
57
use std:: borrow:: Cow ;
58
58
59
59
struct FindLocalAssignmentVisitor {
60
- from : Local ,
61
- loc : Vec < Location > ,
60
+ needle : Local ,
61
+ locations : Vec < Location > ,
62
+ placectxt : PlaceContext ,
63
+ location : Location ,
62
64
}
63
65
64
66
impl < ' tcx > Visitor < ' tcx > for FindLocalAssignmentVisitor {
65
67
fn visit_local ( & mut self ,
66
- local : & mut Local ,
67
- _: PlaceContext < ' tcx > ,
68
- _: Location ) {
69
- Visitor :: visit_local ( local, )
68
+ local : & Local ,
69
+ place_context : PlaceContext < ' tcx > ,
70
+ location : Location ) {
71
+ if self . needle != * local {
72
+ return ;
73
+ }
74
+
75
+ match place_context {
76
+ PlaceContext :: Store | PlaceContext :: Call => {
77
+ self . locations . push ( location) ;
78
+ }
79
+ PlaceContext :: AsmOutput | PlaceContext :: Drop | PlaceContext :: Inspect |
80
+ PlaceContext :: Borrow | PlaceContext :: Projection | PlaceContext :: Copy |
81
+ PlaceContext :: Move | PlaceContext :: StorageLive | PlaceContext :: StorageDead |
82
+ PlaceContext :: Validate => {
83
+ }
84
+ }
85
+
86
+ Visitor :: visit_local ( local, place_context, location)
70
87
}
71
88
}
72
89
@@ -1570,41 +1587,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1570
1587
None => "immutable item" . to_owned ( ) ,
1571
1588
} ;
1572
1589
1573
- // let item_msg = match place{
1574
- // Place::Projection(ref proj) => {
1575
- // let Projection { ref base, ref elem } = **proj;
1576
- // match *elem {
1577
- // ProjectionElem::Deref => {
1578
- // if let Err(place_err) = self.is_mutable(place, is_local_mutation_allowed) {
1579
- // debug!("place_err = {:?} and base={:?}", place_err, base);
1580
- // format!("`&`-reference {:?}", place_err)
1581
-
1582
-
1583
- // }else{
1584
- // match self.describe_place(place) {
1585
- // Some(name) => format!("immutable item `{}`", name),
1586
- // None => "immutable item".to_owned(),
1587
- // }
1588
- // }
1589
- // }
1590
- // _ => {
1591
- // match self.describe_place(place) {
1592
- // Some(name) => format!("immutable item `{}`", name),
1593
- // None => "immutable item".to_owned(),
1594
- // }
1595
-
1596
- // }
1597
- // }
1598
- // },
1599
-
1600
- // _=> {
1601
- // match self.describe_place(place) {
1602
- // Some(name) => format!("immutable item `{}`", name),
1603
- // None => "immutable item".to_owned(),
1604
- // }
1605
- // }
1606
- // };
1607
-
1590
+ // call find_assignments() here
1608
1591
let mut err = self . tcx
1609
1592
. cannot_borrow_path_as_mutable ( span, & item_msg, Origin :: Mir ) ;
1610
1593
err. span_label ( span, "cannot borrow as mutable" ) ;
@@ -2286,10 +2269,12 @@ impl ContextKind {
2286
2269
}
2287
2270
}
2288
2271
2289
- impl Mir {
2290
- fn find_assignments ( & self , local : Local ) -> Vec < Location >
2272
+ impl Mir {
2273
+ fn find_assignments ( & self , local : Local , place_context : PlaceContext , location : Location ) -> Vec < Location >
2291
2274
{
2292
-
2275
+ let mut visitor = FindLocalAssignmentVisitor { needle : local, locations : vec ! [ ] , location : location, place_context: } ;
2276
+ visitor. visit_mir ( self ) ;
2277
+ visitor. locations
2293
2278
}
2294
2279
}
2295
2280
0 commit comments