Skip to content

Commit

Permalink
Add wrong_covariance_unsize_coercion invalid program test
Browse files Browse the repository at this point in the history
  • Loading branch information
Voultapher committed Nov 9, 2023
1 parent 5c01ab6 commit 2f34481
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests-extra/invalid/wrong_covariance_unsize_coercion.rs
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 tests-extra/invalid/wrong_covariance_unsize_coercion.stderr
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)

0 comments on commit 2f34481

Please sign in to comment.