rust/tests/ui/variance/variance-uniquerc.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

28 lines
942 B
Rust
Raw Permalink Normal View History

2025-01-11 21:18:52 +00:00
// regression test of https://github.com/rust-lang/rust/pull/133572#issuecomment-2543007164
2025-03-03 02:53:25 +00:00
// see also the test for UniqueArc in variance-uniquearc.rs
2025-01-11 21:18:52 +00:00
//
// inline comments explain how this code *would* compile if UniqueRc was still covariant
#![feature(unique_rc_arc)]
use std::rc::UniqueRc;
fn extend_lifetime<'a, 'b>(x: &'a str) -> &'b str {
let r = UniqueRc::new(""); // UniqueRc<&'static str>
let w = UniqueRc::downgrade(&r); // Weak<&'static str>
let mut r = r; // [IF COVARIANT]: ==>> UniqueRc<&'a str>
*r = x; // assign the &'a str
let _r = UniqueRc::into_rc(r); // Rc<&'a str>, but we only care to activate the weak
let r = w.upgrade().unwrap(); // Rc<&'static str>
*r // &'static str, coerces to &'b str
//~^ ERROR lifetime may not live long enough
}
fn main() {
let s = String::from("Hello World!");
let r = extend_lifetime(&s);
println!("{r}");
drop(s);
println!("{r}");
}