-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This addresses issue #18 where a contravariant owner could lead to UB.
- Loading branch information
1 parent
8a8670a
commit cf67cbe
Showing
5 changed files
with
67 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use self_cell::self_cell; | ||
|
||
type X<'a> = &'a str; | ||
|
||
self_cell! { | ||
pub struct Foo<'a> { | ||
owner: fn(&'a ()), | ||
|
||
#[covariant] | ||
dependent: X, | ||
} | ||
} | ||
|
||
fn transmute_lifetime<'a, 'b>(x: &'a str) -> &'b str { | ||
fn helper<'x>(s: &'x str) -> impl for<'z> FnOnce(&'z fn(&'x ())) -> &'z str { | ||
move |_| s | ||
} | ||
let x: Foo<'a> = Foo::new(|_| (), helper(x)); | ||
let x: Foo<'static> = x; // coerce using variance | ||
let y = Box::leak(Box::new(x)); | ||
y.borrow_dependent() | ||
} | ||
|
||
fn main() { | ||
let r; | ||
{ | ||
let s = "Hello World".to_owned(); | ||
r = transmute_lifetime(s.as_str()); | ||
dbg!(r); // "Hello World" | ||
} | ||
dbg!(r); // prints garbage :-) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/contravariant_owner.rs:19:27 | ||
| | ||
19 | let x: Foo<'static> = x; // coerce using variance | ||
| ^ lifetime mismatch | ||
| | ||
= note: expected struct `Foo<'static>` | ||
found struct `Foo<'a>` | ||
note: the lifetime `'a` as defined on the function body at 14:23... | ||
--> $DIR/contravariant_owner.rs:14:23 | ||
| | ||
14 | fn transmute_lifetime<'a, 'b>(x: &'a str) -> &'b str { | ||
| ^^ | ||
= note: ...does not necessarily outlive the static lifetime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,35 @@ | ||
IGNORE | ||
IGNORE | ||
--> IGNORE | ||
| | ||
--> wrong_covariance/src/main.rs:7:1 | ||
| | ||
7 | / self_cell!( | ||
8 | | struct NoCov { | ||
9 | | owner: String, | ||
10 | | | ||
... | | ||
13 | | } | ||
14 | | ); | ||
| |__^ lifetime mismatch | ||
| | ||
= note: expected struct `Cell<&'y String>` | ||
found struct `Cell<&'x String>` | ||
| |__^ lifetime mismatch | ||
| | ||
= note: expected struct `Cell<&'y String>` | ||
found struct `Cell<&'x String>` | ||
note: the lifetime `'y` as defined on the function body at IGNORE | ||
--> IGNORE | ||
| | ||
--> wrong_covariance/src/main.rs:7:1 | ||
| | ||
7 | / self_cell!( | ||
8 | | struct NoCov { | ||
9 | | owner: String, | ||
10 | | | ||
... | | ||
13 | | } | ||
14 | | ); | ||
| |__^ | ||
| |__^ | ||
note: ...does not necessarily outlive the lifetime `'x` as defined on the function body at IGNORE | ||
--> IGNORE | ||
| | ||
--> wrong_covariance/src/main.rs:7:1 | ||
| | ||
7 | / self_cell!( | ||
8 | | struct NoCov { | ||
9 | | owner: String, | ||
10 | | | ||
... | | ||
13 | | } | ||
14 | | ); | ||
| |__^ | ||
IGNORE | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. | ||
error: could not compile `wrong_covariance` | ||
|
||
To learn more, run the command again with --verbose. | ||
| |__^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters