Skip to content

Commit 684d1bd

Browse files
committed
Auto merge of #58739 - matthewjasper:more-restrictive-tpb, r=<try>
More restrictive 2 phase borrows - take 2 Another try at this. Currently changes to a hard error, but we probably want to change it to a lint instead. cc #56254 r? @pnkfelix cc @RalfJung @nikomatsakis
2 parents fb162e6 + 58d9280 commit 684d1bd

16 files changed

+41
-176
lines changed

src/librustc_codegen_llvm/abi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
266266
OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
267267
}
268268
PassMode::Direct(_) | PassMode::Indirect(_, None) | PassMode::Cast(_) => {
269-
self.store(bx, next(), dst);
269+
let next_arg = next();
270+
self.store(bx, next_arg, dst);
270271
}
271272
}
272273
}

src/librustc_mir/borrow_check/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
997997
Control::Continue
998998
}
999999

1000-
(Read(_), BorrowKind::Shared) | (Reservation(..), BorrowKind::Shared)
1001-
| (Read(_), BorrowKind::Shallow) | (Reservation(..), BorrowKind::Shallow)
1000+
(Read(_), BorrowKind::Shared)
1001+
| (Read(_), BorrowKind::Shallow)
10021002
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Unique)
10031003
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
10041004
Control::Continue
@@ -1028,8 +1028,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
10281028
Control::Break
10291029
}
10301030

1031-
(Reservation(kind), BorrowKind::Unique)
1032-
| (Reservation(kind), BorrowKind::Mut { .. })
1031+
(Reservation(kind), _)
10331032
| (Activation(kind, _), _)
10341033
| (Write(kind), _) => {
10351034
match rw {

src/librustc_mir/borrow_check/nll/invalidation.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ impl<'cg, 'cx, 'tcx, 'gcx> InvalidationGenerator<'cx, 'tcx, 'gcx> {
432432
// have already taken the reservation
433433
}
434434

435-
(Read(_), BorrowKind::Shallow) | (Reservation(..), BorrowKind::Shallow)
436-
| (Read(_), BorrowKind::Shared) | (Reservation(..), BorrowKind::Shared)
435+
(Read(_), BorrowKind::Shallow)
436+
| (Read(_), BorrowKind::Shared)
437437
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Unique)
438438
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
439439
// Reads/reservations don't invalidate shared or shallow borrows
@@ -452,16 +452,15 @@ impl<'cg, 'cx, 'tcx, 'gcx> InvalidationGenerator<'cx, 'tcx, 'gcx> {
452452
this.generate_invalidates(borrow_index, context.loc);
453453
}
454454

455-
(Reservation(_), BorrowKind::Unique)
456-
| (Reservation(_), BorrowKind::Mut { .. })
457-
| (Activation(_, _), _)
458-
| (Write(_), _) => {
459-
// unique or mutable borrows are invalidated by writes.
460-
// Reservations count as writes since we need to check
461-
// that activating the borrow will be OK
462-
// FIXME(bob_twinkles) is this actually the right thing to do?
463-
this.generate_invalidates(borrow_index, context.loc);
464-
}
455+
(Reservation(_), _)
456+
| (Activation(_, _), _)
457+
| (Write(_), _) => {
458+
// unique or mutable borrows are invalidated by writes.
459+
// Reservations count as writes since we need to check
460+
// that activating the borrow will be OK
461+
// FIXME(bob_twinkles) is this actually the right thing to do?
462+
this.generate_invalidates(borrow_index, context.loc);
463+
}
465464
}
466465
Control::Continue
467466
},

src/librustc_mir/transform/add_retag.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl MirPass for AddRetag {
165165
if src_ty.is_region_ptr() {
166166
// The only `Misc` casts on references are those creating raw pointers.
167167
assert!(dest_ty.is_unsafe_ptr());
168-
(RetagKind::Raw, place)
168+
(RetagKind::Raw, place.clone())
169169
} else {
170170
// Some other cast, no retag
171171
continue
@@ -183,7 +183,7 @@ impl MirPass for AddRetag {
183183
_ =>
184184
RetagKind::Default,
185185
};
186-
(kind, place)
186+
(kind, place.clone())
187187
}
188188
// Do nothing for the rest
189189
_ => continue,
@@ -192,7 +192,7 @@ impl MirPass for AddRetag {
192192
let source_info = block_data.statements[i].source_info;
193193
block_data.statements.insert(i+1, Statement {
194194
source_info,
195-
kind: StatementKind::Retag(retag_kind, place.clone()),
195+
kind: StatementKind::Retag(retag_kind, place),
196196
});
197197
}
198198
}

src/libsyntax_ext/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ impl<'a, 'b> Context<'a, 'b> {
347347

348348
Named(name) => {
349349
match self.names.get(&name) {
350-
Some(idx) => {
350+
Some(&idx) => {
351351
// Treat as positional arg.
352-
self.verify_arg_type(Exact(*idx), ty)
352+
self.verify_arg_type(Exact(idx), ty)
353353
}
354354
None => {
355355
let msg = format!("there is no argument named `{}`", name);

src/test/ui/borrowck/borrowck-for-loop-head-linkage.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | for &x in &vector {
88
| immutable borrow later used here
99
LL | let cap = vector.capacity();
1010
LL | vector.extend(repeat(0)); //~ ERROR cannot borrow
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
11+
| ^^^^^^ mutable borrow occurs here
1212

1313
error[E0502]: cannot borrow `vector` as mutable because it is also borrowed as immutable
1414
--> $DIR/borrowck-for-loop-head-linkage.rs:8:9

src/test/ui/borrowck/borrowck-object-lifetime.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0502]: cannot borrow `*x` as mutable because it is also borrowed as immut
44
LL | let y = x.borrowed();
55
| - immutable borrow occurs here
66
LL | let z = x.mut_borrowed(); //~ ERROR cannot borrow
7-
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
7+
| ^ mutable borrow occurs here
88
LL | y.use_ref();
99
| - immutable borrow later used here
1010

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | vec.get({
77
| immutable borrow occurs here
88
LL |
99
LL | vec.push(2);
10-
| ^^^^^^^^^^^ mutable borrow occurs here
10+
| ^^^ mutable borrow occurs here
1111

1212
error: aborting due to previous error
1313

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

+2-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22

33
// This is similar to two-phase-reservation-sharing-interference.rs
44
// in that it shows a reservation that overlaps with a shared borrow.
5-
//
6-
// Currently, this test fails with lexical lifetimes, but succeeds
7-
// with non-lexical lifetimes. (The reason is because the activation
8-
// of the mutable borrow ends up overlapping with a lexically-scoped
9-
// shared borrow; but a non-lexical shared borrow can end before the
10-
// activation occurs.)
11-
//
12-
// So this test is just making a note of the current behavior.
135

14-
#![feature(rustc_attrs)]
15-
16-
#[rustc_error]
17-
fn main() { //~ ERROR compilation successful
6+
fn main() {
187
let mut v = vec![0, 1, 2];
198
let shared = &v;
209

21-
v.push(shared.len());
10+
v.push(shared.len()); //~ ERROR cannot borrow `v` as mutable
2211

2312
assert_eq!(v, [0, 1, 2, 3]);
2413
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: compilation successful
2-
--> $DIR/two-phase-reservation-sharing-interference-2.rs:17:1
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/two-phase-reservation-sharing-interference-2.rs:10:5
33
|
4-
LL | / fn main() { //~ ERROR compilation successful
5-
LL | | let mut v = vec![0, 1, 2];
6-
LL | | let shared = &v;
7-
LL | |
8-
... |
9-
LL | | assert_eq!(v, [0, 1, 2, 3]);
10-
LL | | }
11-
| |_^
4+
LL | let shared = &v;
5+
| -- immutable borrow occurs here
6+
LL |
7+
LL | v.push(shared.len()); //~ ERROR cannot borrow `v` as mutable
8+
| ^ ------ immutable borrow later used here
9+
| |
10+
| mutable borrow occurs here
1211

1312
error: aborting due to previous error
1413

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

src/test/ui/error-codes/E0502.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0502]: cannot borrow `*a` as mutable because it is also borrowed as immutable
2-
--> $DIR/E0502.rs:4:5
2+
--> $DIR/E0502.rs:4:9
33
|
44
LL | let ref y = a;
55
| ----- immutable borrow occurs here
66
LL | bar(a); //~ ERROR E0502
7-
| ^^^^^^ mutable borrow occurs here
7+
| ^ mutable borrow occurs here
88
LL | y.use_ref();
99
| - immutable borrow later used here
1010

src/test/ui/hashmap-iter-value-lifetime.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let (_, thing) = my_stuff.iter().next().unwrap();
55
| -------- immutable borrow occurs here
66
LL |
77
LL | my_stuff.clear(); //~ ERROR cannot borrow
8-
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
8+
| ^^^^^^^^ mutable borrow occurs here
99
LL |
1010
LL | println!("{}", *thing);
1111
| ------ immutable borrow later used here

src/test/ui/hashmap-lifetimes.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as
44
LL | let mut it = my_stuff.iter();
55
| -------- immutable borrow occurs here
66
LL | my_stuff.insert(1, 43); //~ ERROR cannot borrow
7-
| ^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
7+
| ^^^^^^^^ mutable borrow occurs here
88
LL | it;
99
| -- immutable borrow later used here
1010

src/test/ui/nll/get_default.nll.stderr

-84
This file was deleted.

src/test/ui/nll/region-ends-after-if-condition.nll.stderr

-39
This file was deleted.

src/test/ui/rfc-2005-default-binding-mode/borrowck-issue-49631.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0502]: cannot borrow `foo` as mutable because it is also borrowed as immu
44
LL | while let Some(Ok(string)) = foo.get() {
55
| --- immutable borrow occurs here
66
LL | foo.mutate();
7-
| ^^^^^^^^^^^^ mutable borrow occurs here
7+
| ^^^ mutable borrow occurs here
88
LL | //~^ ERROR cannot borrow `foo` as mutable
99
LL | println!("foo={:?}", *string);
1010
| ------- immutable borrow later used here

0 commit comments

Comments
 (0)