|
11 | 11 | use borrow_check::borrow_set::BorrowData; |
12 | 12 | use borrow_check::nll::region_infer::Cause; |
13 | 13 | use borrow_check::{Context, MirBorrowckCtxt, WriteKind}; |
14 | | -use rustc::mir::{Local, Location, Place, TerminatorKind}; |
| 14 | +use rustc::mir::{FakeReadCause, Local, Location, Place, StatementKind, TerminatorKind}; |
15 | 15 | use rustc_errors::DiagnosticBuilder; |
16 | 16 | use rustc::ty::Region; |
17 | 17 |
|
@@ -142,7 +142,31 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { |
142 | 142 | if spans.for_closure() { |
143 | 143 | "borrow later captured here by closure" |
144 | 144 | } else { |
145 | | - "borrow later used here" |
| 145 | + // Check if the location represents a `FakeRead`, and adapt the error |
| 146 | + // message to the `FakeReadCause` it is from: in particular, |
| 147 | + // the ones inserted in optimized `let var = <expr>` patterns. |
| 148 | + let is_fake_read_for_let = match self.mir.basic_blocks()[location.block] |
| 149 | + .statements |
| 150 | + .get(location.statement_index) |
| 151 | + { |
| 152 | + None => false, |
| 153 | + Some(stmt) => { |
| 154 | + if let StatementKind::FakeRead(ref cause, _) = stmt.kind { |
| 155 | + match cause { |
| 156 | + FakeReadCause::ForLet => true, |
| 157 | + _ => false, |
| 158 | + } |
| 159 | + } else { |
| 160 | + false |
| 161 | + } |
| 162 | + } |
| 163 | + }; |
| 164 | + |
| 165 | + if is_fake_read_for_let { |
| 166 | + "borrow later stored here" |
| 167 | + } else { |
| 168 | + "borrow later used here" |
| 169 | + } |
146 | 170 | } |
147 | 171 | }; |
148 | 172 | err.span_label(spans.var_or_use(), message); |
|
0 commit comments