From 20e361dd761ec08405595f0cf37a4513c98410bf Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 29 May 2023 23:45:14 +0800 Subject: [PATCH] use invariant for unique borrow in projection --- compiler/rustc_borrowck/src/type_check/mod.rs | 5 ++-- tests/ui/mir/issue-112056.rs | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/ui/mir/issue-112056.rs diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 51a84ce6cadea..e20917abc2b2a 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -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, } diff --git a/tests/ui/mir/issue-112056.rs b/tests/ui/mir/issue-112056.rs new file mode 100644 index 0000000000000..6762b7363c254 --- /dev/null +++ b/tests/ui/mir/issue-112056.rs @@ -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); +}