Skip to content

Commit 9a25164

Browse files
committed
Auto merge of #96268 - jackh726:remove-mutable_borrow_reservation_conflict-lint, r=nikomatsakis
Remove mutable_borrow_reservation_conflict lint and allow the code pattern This was the only breaking issue with the NLL stabilization PR. Lang team decided to go ahead and allow this. r? `@nikomatsakis` Closes #59159 Closes #56254
2 parents 8c4fc9d + 2300401 commit 9a25164

21 files changed

+89
-380
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ impl<'tcx> BorrowSet<'tcx> {
167167
crate fn get_index_of(&self, location: &Location) -> Option<BorrowIndex> {
168168
self.location_map.get_index_of(location).map(BorrowIndex::from)
169169
}
170-
171-
crate fn contains(&self, location: &Location) -> bool {
172-
self.location_map.contains_key(location)
173-
}
174170
}
175171

176172
struct GatherBorrows<'a, 'tcx> {

compiler/rustc_borrowck/src/lib.rs

+8-65
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, Stat
3434
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
3535
use rustc_middle::ty::query::Providers;
3636
use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
37-
use rustc_session::lint::builtin::{MUTABLE_BORROW_RESERVATION_CONFLICT, UNUSED_MUT};
38-
use rustc_span::{Span, Symbol, DUMMY_SP};
37+
use rustc_session::lint::builtin::UNUSED_MUT;
38+
use rustc_span::{Span, Symbol};
3939

4040
use either::Either;
4141
use smallvec::SmallVec;
4242
use std::cell::RefCell;
4343
use std::collections::BTreeMap;
44-
use std::mem;
4544
use std::rc::Rc;
4645

4746
use rustc_mir_dataflow::impls::{
@@ -312,7 +311,6 @@ fn do_mir_borrowck<'a, 'tcx>(
312311
locals_are_invalidated_at_exit,
313312
access_place_error_reported: Default::default(),
314313
reservation_error_reported: Default::default(),
315-
reservation_warnings: Default::default(),
316314
uninitialized_error_reported: Default::default(),
317315
regioncx: regioncx.clone(),
318316
used_mut: Default::default(),
@@ -344,7 +342,6 @@ fn do_mir_borrowck<'a, 'tcx>(
344342
fn_self_span_reported: Default::default(),
345343
access_place_error_reported: Default::default(),
346344
reservation_error_reported: Default::default(),
347-
reservation_warnings: Default::default(),
348345
uninitialized_error_reported: Default::default(),
349346
regioncx: Rc::clone(&regioncx),
350347
used_mut: Default::default(),
@@ -377,34 +374,6 @@ fn do_mir_borrowck<'a, 'tcx>(
377374
&mut mbcx,
378375
);
379376

380-
// Convert any reservation warnings into lints.
381-
let reservation_warnings = mem::take(&mut mbcx.reservation_warnings);
382-
for (_, (place, span, location, bk, borrow)) in reservation_warnings {
383-
let initial_diag = mbcx.report_conflicting_borrow(location, (place, span), bk, &borrow);
384-
385-
let scope = mbcx.body.source_info(location).scope;
386-
let lint_root = match &mbcx.body.source_scopes[scope].local_data {
387-
ClearCrossCrate::Set(data) => data.lint_root,
388-
_ => tcx.hir().local_def_id_to_hir_id(def.did),
389-
};
390-
391-
// Span and message don't matter; we overwrite them below anyway
392-
mbcx.infcx.tcx.struct_span_lint_hir(
393-
MUTABLE_BORROW_RESERVATION_CONFLICT,
394-
lint_root,
395-
DUMMY_SP,
396-
|lint| {
397-
let mut diag = lint.build("");
398-
399-
diag.message = initial_diag.styled_message().clone();
400-
diag.span = initial_diag.span.clone();
401-
402-
mbcx.buffer_non_error_diag(diag);
403-
},
404-
);
405-
initial_diag.cancel();
406-
}
407-
408377
// For each non-user used mutable variable, check if it's been assigned from
409378
// a user-declared local. If so, then put that local into the used_mut set.
410379
// Note that this set is expected to be small - only upvars from closures
@@ -539,11 +508,6 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
539508
/// used to report extra information for `FnSelfUse`, to avoid
540509
/// unnecessarily verbose errors.
541510
fn_self_span_reported: FxHashSet<Span>,
542-
/// Migration warnings to be reported for #56254. We delay reporting these
543-
/// so that we can suppress the warning if there's a corresponding error
544-
/// for the activation of the borrow.
545-
reservation_warnings:
546-
FxHashMap<BorrowIndex, (Place<'tcx>, Span, Location, BorrowKind, BorrowData<'tcx>)>,
547511
/// This field keeps track of errors reported in the checking of uninitialized variables,
548512
/// so that we don't report seemingly duplicate errors.
549513
uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>,
@@ -995,12 +959,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
995959
let conflict_error =
996960
self.check_access_for_conflict(location, place_span, sd, rw, flow_state);
997961

998-
if let (Activation(_, borrow_idx), true) = (kind.1, conflict_error) {
999-
// Suppress this warning when there's an error being emitted for the
1000-
// same borrow: fixing the error is likely to fix the warning.
1001-
self.reservation_warnings.remove(&borrow_idx);
1002-
}
1003-
1004962
if conflict_error || mutability_error {
1005963
debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);
1006964
self.access_place_error_reported.insert((place_span.0, place_span.1));
@@ -1067,6 +1025,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10671025
BorrowKind::Unique | BorrowKind::Mut { .. },
10681026
) => Control::Continue,
10691027

1028+
(Reservation(_), BorrowKind::Shallow | BorrowKind::Shared) => {
1029+
// This used to be a future compatibility warning (to be
1030+
// disallowed on NLL). See rust-lang/rust#56254
1031+
Control::Continue
1032+
}
1033+
10701034
(Write(WriteKind::Move), BorrowKind::Shallow) => {
10711035
// Handled by initialization checks.
10721036
Control::Continue
@@ -1095,27 +1059,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10951059
Control::Break
10961060
}
10971061

1098-
(
1099-
Reservation(WriteKind::MutableBorrow(bk)),
1100-
BorrowKind::Shallow | BorrowKind::Shared,
1101-
) if { tcx.migrate_borrowck() && this.borrow_set.contains(&location) } => {
1102-
let bi = this.borrow_set.get_index_of(&location).unwrap();
1103-
debug!(
1104-
"recording invalid reservation of place: {:?} with \
1105-
borrow index {:?} as warning",
1106-
place_span.0, bi,
1107-
);
1108-
// rust-lang/rust#56254 - This was previously permitted on
1109-
// the 2018 edition so we emit it as a warning. We buffer
1110-
// these separately so that we only emit a warning if borrow
1111-
// checking was otherwise successful.
1112-
this.reservation_warnings
1113-
.insert(bi, (place_span.0, place_span.1, location, bk, borrow.clone()));
1114-
1115-
// Don't suppress actual errors.
1116-
Control::Continue
1117-
}
1118-
11191062
(Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
11201063
match rw {
11211064
Reservation(..) => {

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
490490
"converted into hard error, see RFC 2972 \
491491
<https://github.com/rust-lang/rfcs/blob/master/text/2972-constrained-naked.md> for more information",
492492
);
493+
store.register_removed(
494+
"mutable_borrow_reservation_conflict",
495+
"now allowed, see issue #59159 \
496+
<https://github.com/rust-lang/rust/issues/59159> for more information",
497+
);
493498
}
494499

495500
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

-35
Original file line numberDiff line numberDiff line change
@@ -2345,40 +2345,6 @@ declare_lint! {
23452345
};
23462346
}
23472347

2348-
declare_lint! {
2349-
/// The `mutable_borrow_reservation_conflict` lint detects the reservation
2350-
/// of a two-phased borrow that conflicts with other shared borrows.
2351-
///
2352-
/// ### Example
2353-
///
2354-
/// ```rust
2355-
/// let mut v = vec![0, 1, 2];
2356-
/// let shared = &v;
2357-
/// v.push(shared.len());
2358-
/// ```
2359-
///
2360-
/// {{produces}}
2361-
///
2362-
/// ### Explanation
2363-
///
2364-
/// This is a [future-incompatible] lint to transition this to a hard error
2365-
/// in the future. See [issue #59159] for a complete description of the
2366-
/// problem, and some possible solutions.
2367-
///
2368-
/// [issue #59159]: https://github.com/rust-lang/rust/issues/59159
2369-
/// [future-incompatible]: ../index.md#future-incompatible-lints
2370-
pub MUTABLE_BORROW_RESERVATION_CONFLICT,
2371-
Warn,
2372-
"reservation of a two-phased borrow conflicts with other shared borrows",
2373-
@future_incompatible = FutureIncompatibleInfo {
2374-
reason: FutureIncompatibilityReason::Custom(
2375-
"this borrowing pattern was not meant to be accepted, \
2376-
and may become a hard error in the future"
2377-
),
2378-
reference: "issue #59159 <https://github.com/rust-lang/rust/issues/59159>",
2379-
};
2380-
}
2381-
23822348
declare_lint! {
23832349
/// The `soft_unstable` lint detects unstable features that were
23842350
/// unintentionally allowed on stable.
@@ -3179,7 +3145,6 @@ declare_lint_pass! {
31793145
META_VARIABLE_MISUSE,
31803146
DEPRECATED_IN_FUTURE,
31813147
AMBIGUOUS_ASSOCIATED_ITEMS,
3182-
MUTABLE_BORROW_RESERVATION_CONFLICT,
31833148
INDIRECT_STRUCTURAL_MATCH,
31843149
POINTER_STRUCTURAL_MATCH,
31853150
NONTRIVIAL_STRUCTURAL_MATCH,

src/test/ui/borrowck/suggest-local-var-imm-and-mut.nll.stderr

+2-13
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@ LL | self.foo(self.bar());
77
| | | mutable borrow occurs here
88
| | immutable borrow later used by call
99
| immutable borrow occurs here
10-
|
11-
help: try adding a local storing this argument...
12-
--> $DIR/suggest-local-var-imm-and-mut.rs:12:22
13-
|
14-
LL | self.foo(self.bar());
15-
| ^^^^^^^^^^
16-
help: ...and then using that local as the argument to this call
17-
--> $DIR/suggest-local-var-imm-and-mut.rs:12:13
18-
|
19-
LL | self.foo(self.bar());
20-
| ^^^^^^^^^^^^^^^^^^^^
2110

2211
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
23-
--> $DIR/suggest-local-var-imm-and-mut.rs:24:39
12+
--> $DIR/suggest-local-var-imm-and-mut.rs:24:29
2413
|
2514
LL | Self::foo(self, Self::bar(self));
26-
| --------- ---- ^^^^ mutable borrow occurs here
15+
| --------- ---- ^^^^^^^^^^^^^^^ mutable borrow occurs here
2716
| | |
2817
| | immutable borrow occurs here
2918
| immutable borrow later used by call

src/test/ui/borrowck/two-phase-cannot-nest-mut-self-calls.stderr

-17
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ LL | |
1313
LL | | 0
1414
LL | | });
1515
| |______- immutable borrow occurs here
16-
|
17-
help: try adding a local storing this argument...
18-
--> $DIR/two-phase-cannot-nest-mut-self-calls.rs:16:9
19-
|
20-
LL | vec.push(2);
21-
| ^^^^^^^^^^^
22-
help: ...and then using that local as the argument to this call
23-
--> $DIR/two-phase-cannot-nest-mut-self-calls.rs:14:5
24-
|
25-
LL | / vec.get({
26-
LL | |
27-
LL | | vec.push(2);
28-
LL | |
29-
LL | |
30-
LL | | 0
31-
LL | | });
32-
| |______^
3316

3417
error: aborting due to previous error
3518

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5
3+
|
4+
LL | let shared = &v;
5+
| -- immutable borrow occurs here
6+
LL |
7+
LL | v.extend(shared);
8+
| ^^------^^^^^^^^
9+
| | |
10+
| | immutable borrow later used by call
11+
| mutable borrow occurs here
12+
13+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
14+
--> $DIR/two-phase-reservation-sharing-interference-2.rs:27:5
15+
|
16+
LL | v.extend(&v);
17+
| ^^------^--^
18+
| | | |
19+
| | | immutable borrow occurs here
20+
| | immutable borrow later used by call
21+
| mutable borrow occurs here
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0502`.

src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2015.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ LL | v.extend(&v);
2020
| | immutable borrow later used by call
2121
| mutable borrow occurs here
2222

23-
warning: cannot borrow `v` as mutable because it is also borrowed as immutable
24-
--> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5
25-
|
26-
LL | let shared = &v;
27-
| -- immutable borrow occurs here
28-
LL |
29-
LL | v.push(shared.len());
30-
| ^^^^^^^------------^
31-
| | |
32-
| | immutable borrow later used here
33-
| mutable borrow occurs here
34-
|
35-
= note: `#[warn(mutable_borrow_reservation_conflict)]` on by default
36-
= warning: this borrowing pattern was not meant to be accepted, and may become a hard error in the future
37-
= note: for more information, see issue #59159 <https://github.com/rust-lang/rust/issues/59159>
38-
39-
error: aborting due to 2 previous errors; 1 warning emitted
23+
error: aborting due to 2 previous errors
4024

4125
For more information about this error, try `rustc --explain E0502`.

src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2018.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ LL | v.extend(&v);
2020
| | immutable borrow later used by call
2121
| mutable borrow occurs here
2222

23-
warning: cannot borrow `v` as mutable because it is also borrowed as immutable
24-
--> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5
25-
|
26-
LL | let shared = &v;
27-
| -- immutable borrow occurs here
28-
LL |
29-
LL | v.push(shared.len());
30-
| ^^^^^^^------------^
31-
| | |
32-
| | immutable borrow later used here
33-
| mutable borrow occurs here
34-
|
35-
= note: `#[warn(mutable_borrow_reservation_conflict)]` on by default
36-
= warning: this borrowing pattern was not meant to be accepted, and may become a hard error in the future
37-
= note: for more information, see issue #59159 <https://github.com/rust-lang/rust/issues/59159>
38-
39-
error: aborting due to 2 previous errors; 1 warning emitted
23+
error: aborting due to 2 previous errors
4024

4125
For more information about this error, try `rustc --explain E0502`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5
3+
|
4+
LL | let shared = &v;
5+
| -- immutable borrow occurs here
6+
LL |
7+
LL | v.extend(shared);
8+
| ^^------^^^^^^^^
9+
| | |
10+
| | immutable borrow later used by call
11+
| mutable borrow occurs here
12+
13+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
14+
--> $DIR/two-phase-reservation-sharing-interference-2.rs:27:5
15+
|
16+
LL | v.extend(&v);
17+
| ^^------^--^
18+
| | | |
19+
| | | immutable borrow occurs here
20+
| | immutable borrow later used by call
21+
| mutable borrow occurs here
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0502`.

src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2015.stderr

+4-16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ LL | let shared = &v;
55
| -- immutable borrow occurs here
66
LL |
77
LL | v.extend(shared);
8-
| ^^^^^^^^^------^
9-
| | |
10-
| | immutable borrow later used here
8+
| ^^------^^^^^^^^
9+
| | |
10+
| | immutable borrow later used by call
1111
| mutable borrow occurs here
1212

1313
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
@@ -20,18 +20,6 @@ LL | v.extend(&v);
2020
| | immutable borrow later used by call
2121
| mutable borrow occurs here
2222

23-
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
24-
--> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5
25-
|
26-
LL | let shared = &v;
27-
| -- immutable borrow occurs here
28-
LL |
29-
LL | v.push(shared.len());
30-
| ^^^^^^^------------^
31-
| | |
32-
| | immutable borrow later used here
33-
| mutable borrow occurs here
34-
35-
error: aborting due to 3 previous errors
23+
error: aborting due to 2 previous errors
3624

3725
For more information about this error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)