Skip to content

Commit 91dc3e5

Browse files
committed
Add scary warnings to errors-downgraded-to-warnings in borrowck=migrate.
Also convert an ICE that became reachable code under borrowck=migrate into a normally reported error (which is then downgraded to a warning). This actually has a nice side benefit of providing a somewhat more useful error message, at least in the particular case of the example from issue rust-lang#27282.
1 parent 3460115 commit 91dc3e5

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/librustc_mir/borrow_check/mod.rs

+44-14
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,13 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
338338
// downgrade all the buffered MIR-borrowck errors
339339
// to warnings.
340340
for err in &mut mbcx.errors_buffer {
341-
if err.is_error() { err.level = Level::Warning; }
341+
if err.is_error() {
342+
err.level = Level::Warning;
343+
err.warn("This error has been downgraded to a warning \
344+
for backwards compatibility with previous releases.\n\
345+
It represents potential unsoundness in your code.\n\
346+
This warning will become a hard error in the future.");
347+
}
342348
}
343349
}
344350
SignalledError::SawSomeError => {
@@ -1768,20 +1774,44 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17681774
}
17691775
}
17701776

1771-
Reservation(WriteKind::Move)
1772-
| Write(WriteKind::Move)
1773-
| Reservation(WriteKind::StorageDeadOrDrop)
1774-
| Reservation(WriteKind::MutableBorrow(BorrowKind::Shared))
1775-
| Write(WriteKind::StorageDeadOrDrop)
1776-
| Write(WriteKind::MutableBorrow(BorrowKind::Shared)) => {
1777+
Reservation(wk @ WriteKind::Move)
1778+
| Write(wk @ WriteKind::Move)
1779+
| Reservation(wk @ WriteKind::StorageDeadOrDrop)
1780+
| Reservation(wk @ WriteKind::MutableBorrow(BorrowKind::Shared))
1781+
| Write(wk @ WriteKind::StorageDeadOrDrop)
1782+
| Write(wk @ WriteKind::MutableBorrow(BorrowKind::Shared)) => {
17771783
if let Err(_place_err) = self.is_mutable(place, is_local_mutation_allowed) {
1778-
self.tcx.sess.delay_span_bug(
1779-
span,
1780-
&format!(
1781-
"Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
1782-
place, kind
1783-
),
1784-
);
1784+
if self.tcx.migrate_borrowck() {
1785+
// rust-lang/rust#46908: In pure NLL mode this
1786+
// code path should be unreachable (and thus
1787+
// we signal an ICE in the else branch
1788+
// here). But we can legitimately get here
1789+
// under borrowck=migrate mode, so instead of
1790+
// ICE'ing we instead report a legitimate
1791+
// error (which will then be downgraded to a
1792+
// warning by the migrate machinery).
1793+
error_access = match wk {
1794+
WriteKind::MutableBorrow(_) => AccessKind::MutableBorrow,
1795+
WriteKind::Move => AccessKind::Move,
1796+
WriteKind::StorageDeadOrDrop |
1797+
WriteKind::Mutate => AccessKind::Mutate,
1798+
};
1799+
self.report_mutability_error(
1800+
place,
1801+
span,
1802+
_place_err,
1803+
error_access,
1804+
location,
1805+
);
1806+
} else {
1807+
self.tcx.sess.delay_span_bug(
1808+
span,
1809+
&format!(
1810+
"Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
1811+
place, kind
1812+
),
1813+
);
1814+
}
17851815
}
17861816
return false;
17871817
}

src/librustc_mir/borrow_check/mutability_errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use util::suggest_ref_mut;
2424
pub(super) enum AccessKind {
2525
MutableBorrow,
2626
Mutate,
27+
Move,
2728
}
2829

2930
impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
@@ -110,6 +111,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
110111
if let Some(desc) = access_place_desc {
111112
item_msg = format!("`{}`", desc);
112113
reason = match error_access {
114+
AccessKind::Move |
113115
AccessKind::Mutate => format!(" which is behind a {}", pointer_type),
114116
AccessKind::MutableBorrow => {
115117
format!(", as it is behind a {}", pointer_type)
@@ -160,6 +162,13 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
160162

161163

162164
let span = match error_access {
165+
AccessKind::Move => {
166+
err = self.tcx
167+
.cannot_move_out_of(span, &(item_msg + &reason), Origin::Mir);
168+
act = "move";
169+
acted_on = "moved";
170+
span
171+
}
163172
AccessKind::Mutate => {
164173
err = self.tcx
165174
.cannot_assign(span, &(item_msg + &reason), Origin::Mir);

0 commit comments

Comments
 (0)