Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions compiler/rustc_borrowck/src/borrow_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,28 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> {
// to avoid capturing the local variable's place.
let source_phantom_deref_place =
source_place.project_deeper(&[PlaceElem::PhantomDeref], self.tcx);
if target_args.regions().count() != 1 {
bug!(
"ADT containing no '&mut T' or 'T: Reborrow' fields must only have one lifetime to implement Reborrow"
);
// With no field to reborrow we don't know which of the target's lifetimes
// the borrow should be tied to, so borrow the whole source under each of them.
let mut target_regions: SmallVec<[RegionVid; 1]> = SmallVec::new();
for region in target_args.regions() {
let region = region.as_var();
if !target_regions.contains(&region) {
target_regions.push(region);
}
}
if target_regions.is_empty() {
bug!("Reborrow target type has no lifetime arguments");
}
for region in target_regions {
v.push(BorrowData {
kind,
region,
reserve_location: location,
activation_location: TwoPhaseActivation::NotTwoPhase,
borrowed_place: source_phantom_deref_place,
assigned_place: target_place,
});
}
let target_region = target_args.regions().next().unwrap();
v.push(BorrowData {
kind,
region: target_region.as_var(),
reserve_location: location,
activation_location: TwoPhaseActivation::NotTwoPhase,
borrowed_place: source_phantom_deref_place,
assigned_place: target_place,
});
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/reborrow/reborrow-multiple-lifetimes-exclusive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! A `Reborrow` type with two lifetimes and no reborrowed field is borrowed as a
//! whole, under both lifetimes, so two live reborrows still conflict.

#![feature(reborrow)]

use std::marker::{PhantomData, Reborrow};

struct Marker<'a, 'b>(PhantomData<(&'a mut (), &'b mut ())>);

impl<'a, 'b> Reborrow for Marker<'a, 'b> {}

fn reborrow<'a, 'b>(m: Marker<'a, 'b>) -> Marker<'a, 'b> {
m
}

fn keep(_: &Marker<'_, '_>) {}

fn main() {
let m = Marker(PhantomData);
let x = reborrow(m);
let y = reborrow(m); //~ ERROR cannot borrow `m` as mutable more than once at a time
keep(&x);
keep(&y);
}
13 changes: 13 additions & 0 deletions tests/ui/reborrow/reborrow-multiple-lifetimes-exclusive.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0499]: cannot borrow `m` as mutable more than once at a time
--> $DIR/reborrow-multiple-lifetimes-exclusive.rs:21:22
|
LL | let x = reborrow(m);
| - first mutable borrow occurs here
LL | let y = reborrow(m);
| ^ second mutable borrow occurs here
LL | keep(&x);
| -- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.
23 changes: 23 additions & 0 deletions tests/ui/reborrow/reborrow-multiple-lifetimes-issue-165653.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/165653>.
//!
//! Reborrowing a type with two lifetimes and no `&mut` or `Reborrow` field used to
//! hit a `bug!` in borrowck. It now gives the same error as the one lifetime case.

#![feature(reborrow)]

use std::marker::Reborrow;

impl<'a, 'b> Reborrow for DerivedShared<'a, 'b> {}

struct DerivedShared<'a, 'b> {
a: &'a &'b str,
}

impl<'a, 'b> DerivedShared<'a, 'b> {
fn clone(&self) -> Self {
*self
//~^ ERROR cannot borrow `*self` as mutable, as it is behind a `&` reference
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/reborrow/reborrow-multiple-lifetimes-issue-165653.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `*self` as mutable, as it is behind a `&` reference
--> $DIR/reborrow-multiple-lifetimes-issue-165653.rs:18:9
|
LL | *self
| ^^^^^ `self` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
LL | fn clone(&mut self) -> Self {
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0596`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//@ check-pass
//! Types with more than one lifetime where borrowck finds no field to reborrow.
//! Borrowck used to hit a `bug!` on all of these. Where a value is used twice, the
//! second use only compiles if the first one was a reborrow and not a move.

#![feature(reborrow)]

use std::marker::{CoerceShared, PhantomData, Reborrow};

// Only a marker field.
struct Marker<'a, 'b>(PhantomData<(&'a mut (), &'b mut ())>);

impl<'a, 'b> Reborrow for Marker<'a, 'b> {}

fn marker<'a, 'b>(m: Marker<'a, 'b>) -> Marker<'a, 'b> {
m
}

// A type parameter field.
struct Generic<'a, 'b, T> {
t: T,
marker: PhantomData<(&'a (), &'b ())>,
}

impl<'a, 'b, T: Reborrow> Reborrow for Generic<'a, 'b, T> {}

fn generic<'a, 'b>(g: Generic<'a, 'b, Inner<'a>>) -> Generic<'a, 'b, Inner<'a>> {
g
}

// An associated type field that normalizes to `&mut`.
trait HasAssoc<'a> {
type Assoc;
}

impl<'a> HasAssoc<'a> for () {
type Assoc = &'a mut ();
}

struct Assoc<'a, 'b> {
assoc: <() as HasAssoc<'a>>::Assoc,
shared: &'b (),
}

impl<'a, 'b> Reborrow for Assoc<'a, 'b> {}

fn assoc<'a, 'b>(x: Assoc<'a, 'b>) -> Assoc<'a, 'b> {
x
}

// A field that is only `Copy` when `T: Copy`, used in a body with that bound.
#[derive(Clone, Copy)]
struct Wrap<'a, T>(PhantomData<&'a ()>, T);

impl<'a, T: Reborrow> Reborrow for Wrap<'a, T> {}

struct WithWrap<'a, 'b, T> {
w: Wrap<'a, T>,
shared: &'b (),
}

impl<'a, 'b, T: Reborrow> Reborrow for WithWrap<'a, 'b, T> {}

fn with_wrap<'a, 'b, T: Reborrow + Copy>(x: WithWrap<'a, 'b, T>) -> WithWrap<'a, 'b, T> {
x
}

// A `CoerceShared` target with more lifetimes than the source.
struct Src<'a>(PhantomData<(&'a (), &'a ())>);

impl<'a> Reborrow for Src<'a> {}

#[derive(Clone, Copy)]
struct Dst<'a, 'b>(PhantomData<(&'a (), &'b ())>);

impl<'a> CoerceShared<Dst<'a, 'a>> for Src<'a> {}

fn coerce<'a>(x: Src<'a>) -> Dst<'a, 'a> {
x
}

struct Inner<'a>(&'a mut ());

impl<'a> Reborrow for Inner<'a> {}

fn main() {
let m = Marker(PhantomData);
let _ = marker(m);
let _ = marker(m);

let mut a = ();
let g = Generic { t: Inner(&mut a), marker: PhantomData };
let _ = generic(g);
let _ = generic(g);

}
Loading