-
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.
Add wrong_covariance_unsize_coercion invalid program test
- Loading branch information
1 parent
f82025e
commit e418be1
Showing
2 changed files
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::cell::RefCell; | ||
use std::fmt; | ||
|
||
use self_cell::self_cell; | ||
|
||
self_cell! { | ||
struct WrongVarianceExample { | ||
owner: (), | ||
|
||
#[covariant] | ||
dependent: Dependent, | ||
} | ||
} | ||
|
||
// this type is not covariant | ||
type Dependent<'a> = RefCell<Box<dyn fmt::Display + 'a>>; | ||
|
||
fn main() { | ||
let cell = WrongVarianceExample::new((), |_| RefCell::new(Box::new(""))); | ||
let s = String::from("Hello World"); | ||
|
||
// borrow_dependent unsound due to incorrectly checked variance | ||
*cell.borrow_dependent().borrow_mut() = Box::new(s.as_str()); | ||
|
||
// s still exists | ||
cell.with_dependent(|_, d| println!("{}", d.borrow())); | ||
|
||
drop(s); | ||
|
||
// s is gone | ||
cell.with_dependent(|_, d| println!("{}", d.borrow())); | ||
} |
16 changes: 16 additions & 0 deletions
16
tests-extra/invalid/wrong_covariance_unsize_coercion.stderr
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,16 @@ | ||
error[E0623]: lifetime mismatch | ||
--> $DIR/wrong_covariance_unsize_coercion.rs:6:1 | ||
| | ||
6 | / self_cell! { | ||
7 | | struct WrongVarianceExample { | ||
8 | | owner: (), | ||
9 | | | ||
... | | ||
12 | | } | ||
13 | | } | ||
| | ^ | ||
| | | | ||
| |_these two types are declared with different lifetimes... | ||
| ...but data from `x` flows into `x` here | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) |