Skip to content

Commit 75b94e2

Browse files
committed
Create a helper function to retrieve the FakeReadClause at a location
1 parent ae6479c commit 75b94e2

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use borrow_check::WriteKind;
1212
use rustc::middle::region::ScopeTree;
1313
use rustc::mir::VarBindingForm;
1414
use rustc::mir::{BindingForm, BorrowKind, ClearCrossCrate, Field, Local};
15-
use rustc::mir::{LocalDecl, LocalKind, Location, Operand, Place};
15+
use rustc::mir::{FakeReadCause, LocalDecl, LocalKind, Location, Operand, Place};
1616
use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
1717
use rustc::ty;
1818
use rustc_data_structures::fx::FxHashSet;
@@ -989,6 +989,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
989989
false
990990
}
991991
}
992+
993+
/// Returns the `FakeReadCause` at this location if it is a `FakeRead` statement.
994+
pub(super) fn retrieve_fake_read_cause_for_location(
995+
&self,
996+
location: &Location,
997+
) -> Option<FakeReadCause> {
998+
let stmt = self.mir.basic_blocks()[location.block]
999+
.statements
1000+
.get(location.statement_index)?;
1001+
if let StatementKind::FakeRead(cause, _) = stmt.kind {
1002+
Some(cause)
1003+
} else {
1004+
None
1005+
}
1006+
}
9921007
}
9931008

9941009
// The span(s) associated to a use of a place.

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use borrow_check::borrow_set::BorrowData;
1212
use borrow_check::nll::region_infer::Cause;
1313
use borrow_check::{Context, MirBorrowckCtxt, WriteKind};
14-
use rustc::mir::{FakeReadCause, Local, Location, Place, StatementKind, TerminatorKind};
14+
use rustc::mir::{FakeReadCause, Local, Location, Place, TerminatorKind};
1515
use rustc_errors::DiagnosticBuilder;
1616
use rustc::ty::Region;
1717

@@ -145,27 +145,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
145145
// Check if the location represents a `FakeRead`, and adapt the error
146146
// message to the `FakeReadCause` it is from: in particular,
147147
// 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"
148+
match self.retrieve_fake_read_cause_for_location(&location) {
149+
Some(FakeReadCause::ForLet) => "borrow later stored here",
150+
_ => "borrow later used here"
169151
}
170152
}
171153
};

0 commit comments

Comments
 (0)