Skip to content

Commit 90c5418

Browse files
committed
Auto merge of #112145 - wesleywiser:backport_112070, r=Mark-Simulacrum
Backport of #112070 Backports #112070 to stable r? `@Mark-Simulacrum`
2 parents d332def + 4930c02 commit 90c5418

File tree

13 files changed

+92
-19
lines changed

13 files changed

+92
-19
lines changed

compiler/rustc_borrowck/src/def_use.rs

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub fn categorize(context: PlaceContext) -> Option<DefUse> {
5050
PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
5151
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
5252
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
53-
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
5453

5554
PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
5655
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |

compiler/rustc_borrowck/src/type_check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
775775
ty::Invariant
776776
}
777777
PlaceContext::NonMutatingUse(
778-
Inspect | Copy | Move | SharedBorrow | ShallowBorrow | UniqueBorrow | AddressOf
779-
| Projection,
780-
) => ty::Covariant,
778+
Inspect | Copy | Move | SharedBorrow | ShallowBorrow | AddressOf | Projection
779+
) => {
780+
ty::Covariant
781+
},
781782
PlaceContext::NonUse(AscribeUserTy) => ty::Covariant,
782783
}
783784
}

compiler/rustc_codegen_ssa/src/mir/analyze.rs

-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
232232
| PlaceContext::NonMutatingUse(
233233
NonMutatingUseContext::Inspect
234234
| NonMutatingUseContext::SharedBorrow
235-
| NonMutatingUseContext::UniqueBorrow
236235
| NonMutatingUseContext::ShallowBorrow
237236
| NonMutatingUseContext::AddressOf
238237
| NonMutatingUseContext::Projection,

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
412412
BorrowKind::Shallow => {
413413
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
414414
}
415-
BorrowKind::Unique => {
416-
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
417-
}
415+
BorrowKind::Unique => PlaceContext::MutatingUse(MutatingUseContext::Borrow),
418416
BorrowKind::Mut { .. } => {
419417
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
420418
}

compiler/rustc_middle/src/mir/syntax.rs

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ pub enum BorrowKind {
220220
/// immutable, but not aliasable. This solves the problem. For
221221
/// simplicity, we don't give users the way to express this
222222
/// borrow, it's just used when translating closures.
223+
///
224+
// FIXME(#112072): This is wrong. Unique borrows are mutable borrows except
225+
// that they do not require their pointee to be marked as a mutable.
226+
// They should still be treated as mutable borrows in every other way,
227+
// e.g. for variance or overlap checking.
223228
Unique,
224229

225230
/// Data is mutable and not aliasable.

compiler/rustc_middle/src/mir/visit.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ macro_rules! make_mir_visitor {
640640
BorrowKind::Shallow => PlaceContext::NonMutatingUse(
641641
NonMutatingUseContext::ShallowBorrow
642642
),
643-
BorrowKind::Unique => PlaceContext::NonMutatingUse(
644-
NonMutatingUseContext::UniqueBorrow
643+
BorrowKind::Unique => PlaceContext::MutatingUse(
644+
MutatingUseContext::Borrow
645645
),
646646
BorrowKind::Mut { .. } =>
647647
PlaceContext::MutatingUse(MutatingUseContext::Borrow),
@@ -1247,8 +1247,6 @@ pub enum NonMutatingUseContext {
12471247
SharedBorrow,
12481248
/// Shallow borrow.
12491249
ShallowBorrow,
1250-
/// Unique borrow.
1251-
UniqueBorrow,
12521250
/// AddressOf for *const pointer.
12531251
AddressOf,
12541252
/// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
@@ -1324,9 +1322,7 @@ impl PlaceContext {
13241322
matches!(
13251323
self,
13261324
PlaceContext::NonMutatingUse(
1327-
NonMutatingUseContext::SharedBorrow
1328-
| NonMutatingUseContext::ShallowBorrow
1329-
| NonMutatingUseContext::UniqueBorrow
1325+
NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::ShallowBorrow
13301326
) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
13311327
)
13321328
}

compiler/rustc_mir_dataflow/src/impls/liveness.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ impl DefUse {
198198
| NonMutatingUseContext::Inspect
199199
| NonMutatingUseContext::Move
200200
| NonMutatingUseContext::ShallowBorrow
201-
| NonMutatingUseContext::SharedBorrow
202-
| NonMutatingUseContext::UniqueBorrow,
201+
| NonMutatingUseContext::SharedBorrow,
203202
) => Some(DefUse::Use),
204203

205204
PlaceContext::MutatingUse(MutatingUseContext::Projection)

compiler/rustc_mir_transform/src/const_prop.rs

-1
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ impl Visitor<'_> for CanConstProp {
822822
// mutation.
823823
| NonMutatingUse(NonMutatingUseContext::SharedBorrow)
824824
| NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
825-
| NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
826825
| NonMutatingUse(NonMutatingUseContext::AddressOf)
827826
| MutatingUse(MutatingUseContext::Borrow)
828827
| MutatingUse(MutatingUseContext::AddressOf) => {

compiler/rustc_mir_transform/src/copy_prop.rs

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
131131
PlaceContext::NonMutatingUse(
132132
NonMutatingUseContext::SharedBorrow
133133
| NonMutatingUseContext::ShallowBorrow
134-
| NonMutatingUseContext::UniqueBorrow
135134
| NonMutatingUseContext::AddressOf,
136135
) => true,
137136
// For debuginfo, merging locals is ok.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// edition:2021
2+
3+
// regression test for #112056
4+
5+
fn extend_lifetime<'a, 'b>(x: &mut (&'a str,), y: &'b str) {
6+
let mut closure = |input| x.0 = input;
7+
//~^ ERROR: lifetime may not live long enough
8+
closure(y);
9+
}
10+
11+
fn main() {
12+
let mut tuple = ("static",);
13+
{
14+
let x = String::from("temporary");
15+
extend_lifetime(&mut tuple, &x);
16+
}
17+
println!("{}", tuple.0);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/unique-borrows-are-invariant-1.rs:6:31
3+
|
4+
LL | fn extend_lifetime<'a, 'b>(x: &mut (&'a str,), y: &'b str) {
5+
| -- -- lifetime `'b` defined here
6+
| |
7+
| lifetime `'a` defined here
8+
LL | let mut closure = |input| x.0 = input;
9+
| ^^^^^^^^^^^ assignment requires that `'b` must outlive `'a`
10+
|
11+
= help: consider adding the following bound: `'b: 'a`
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// edition:2021
2+
3+
// regression test for #112056
4+
5+
struct Spooky<'b> {
6+
owned: Option<&'static u32>,
7+
borrowed: &'b &'static u32,
8+
}
9+
10+
impl<'b> Spooky<'b> {
11+
fn create_self_reference<'a>(&'a mut self) {
12+
let mut closure = || {
13+
if let Some(owned) = &self.owned {
14+
let borrow: &'a &'static u32 = owned;
15+
self.borrowed = borrow;
16+
//~^ ERROR: lifetime may not live long enough
17+
}
18+
};
19+
closure();
20+
}
21+
}
22+
23+
fn main() {
24+
let mut spooky: Spooky<'static> = Spooky {
25+
owned: Some(&1),
26+
borrowed: &&1,
27+
};
28+
spooky.create_self_reference();
29+
spooky.owned = None;
30+
println!("{}", **spooky.borrowed);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/unique-borrows-are-invariant-2.rs:15:17
3+
|
4+
LL | impl<'b> Spooky<'b> {
5+
| -- lifetime `'b` defined here
6+
LL | fn create_self_reference<'a>(&'a mut self) {
7+
| -- lifetime `'a` defined here
8+
...
9+
LL | self.borrowed = borrow;
10+
| ^^^^^^^^^^^^^^^^^^^^^^ assignment requires that `'a` must outlive `'b`
11+
|
12+
= help: consider adding the following bound: `'a: 'b`
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)