diff options
| author | Frank King <frankking1729@gmail.com> | 2025-03-03 10:53:25 +0800 |
|---|---|---|
| committer | Frank King <frankking1729@gmail.com> | 2025-03-22 15:14:49 +0800 |
| commit | 9e5cca504988932581fa6c83c8626101013d64cd (patch) | |
| tree | c9b91edad16c5fded5668b0c7643b0d0bca68c0a /tests/ui/variance | |
| parent | 5016467a23c93384ffcb58b9c5ff2f8afc349d54 (diff) | |
| download | rust-9e5cca504988932581fa6c83c8626101013d64cd.tar.gz rust-9e5cca504988932581fa6c83c8626101013d64cd.zip | |
Make UniqueArc invariant for soundness
Diffstat (limited to 'tests/ui/variance')
| -rw-r--r-- | tests/ui/variance/variance-uniquearc.rs | 27 | ||||
| -rw-r--r-- | tests/ui/variance/variance-uniquearc.stderr | 15 | ||||
| -rw-r--r-- | tests/ui/variance/variance-uniquerc.rs | 2 |
3 files changed, 43 insertions, 1 deletions
diff --git a/tests/ui/variance/variance-uniquearc.rs b/tests/ui/variance/variance-uniquearc.rs new file mode 100644 index 00000000000..03589512388 --- /dev/null +++ b/tests/ui/variance/variance-uniquearc.rs @@ -0,0 +1,27 @@ +// regression test of https://github.com/rust-lang/rust/pull/133572#issuecomment-2543007164 +// see also the test for UniqueRc` in variance-uniquerc.rs +// +// inline comments explain how this code *would* compile if UniqueArc was still covariant + +#![feature(unique_rc_arc)] + +use std::sync::UniqueArc; + +fn extend_lifetime<'a, 'b>(x: &'a str) -> &'b str { + let r = UniqueArc::new(""); // UniqueArc<&'static str> + let w = UniqueArc::downgrade(&r); // Weak<&'static str> + let mut r = r; // [IF COVARIANT]: ==>> UniqueArc<&'a str> + *r = x; // assign the &'a str + let _r = UniqueArc::into_arc(r); // Arc<&'a str>, but we only care to activate the weak + let r = w.upgrade().unwrap(); // Arc<&'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}"); +} diff --git a/tests/ui/variance/variance-uniquearc.stderr b/tests/ui/variance/variance-uniquearc.stderr new file mode 100644 index 00000000000..55076dae732 --- /dev/null +++ b/tests/ui/variance/variance-uniquearc.stderr @@ -0,0 +1,15 @@ +error: lifetime may not live long enough + --> $DIR/variance-uniquearc.rs:17:5 + | +LL | fn extend_lifetime<'a, 'b>(x: &'a str) -> &'b str { + | -- -- lifetime `'b` defined here + | | + | lifetime `'a` defined here +... +LL | *r // &'static str, coerces to &'b str + | ^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` + | + = help: consider adding the following bound: `'a: 'b` + +error: aborting due to 1 previous error + diff --git a/tests/ui/variance/variance-uniquerc.rs b/tests/ui/variance/variance-uniquerc.rs index 0c395ab06ea..2e9738f66dc 100644 --- a/tests/ui/variance/variance-uniquerc.rs +++ b/tests/ui/variance/variance-uniquerc.rs @@ -1,5 +1,5 @@ // regression test of https://github.com/rust-lang/rust/pull/133572#issuecomment-2543007164 -// we should also test UniqueArc once implemented +// see also the test for UniqueArc in variance-uniquearc.rs // // inline comments explain how this code *would* compile if UniqueRc was still covariant |
