File: variance-uniquerc.rs

package info (click to toggle)
rustc 1.88.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 934,128 kB
  • sloc: xml: 158,127; python: 36,062; javascript: 19,855; sh: 19,700; cpp: 18,947; ansic: 12,993; asm: 4,792; makefile: 690; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (27 lines) | stat: -rw-r--r-- 942 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// regression test of https://github.com/rust-lang/rust/pull/133572#issuecomment-2543007164
// see also the test for UniqueArc in variance-uniquearc.rs
//
// 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}");
}