Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use invariant for unique borrow in projection #112073

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
match context {
PlaceContext::MutatingUse(_) => ty::Invariant,
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
PlaceContext::NonMutatingUse(UniqueBorrow) => ty::Invariant,
PlaceContext::NonMutatingUse(
Inspect | Copy | Move | PlaceMention | SharedBorrow | ShallowBorrow | UniqueBorrow
| AddressOf | Projection,
Inspect | Copy | Move | PlaceMention | SharedBorrow | ShallowBorrow | AddressOf
| Projection,
) => ty::Covariant,
PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/mir/issue-112056.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
struct Spooky<'b> {
owned: Option<&'static u32>,
borrowed: &'b &'static u32,
}

impl<'b> Spooky<'b> {
fn create_self_reference<'a>(&'a mut self) {
let mut closure = || {
if let Some(owned) = &self.owned {
let borrow: &'a &'static u32 = owned;
self.borrowed = borrow;
//~^ ERROR lifetime may not live long
}
};
closure();
}
}

fn main() {
let mut spooky: Spooky<'static> = Spooky { owned: Some(&1), borrowed: &&1 };
spooky.create_self_reference();
spooky.owned = None;
println!("{}", **spooky.borrowed);
}