From 2f3448127bb2058acaaaa90886613f2f99581257 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Thu, 9 Nov 2023 18:57:52 +0100 Subject: [PATCH] Add wrong_covariance_unsize_coercion invalid program test --- .../wrong_covariance_unsize_coercion.rs | 32 +++++++++++++++++++ .../wrong_covariance_unsize_coercion.stderr | 16 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests-extra/invalid/wrong_covariance_unsize_coercion.rs create mode 100644 tests-extra/invalid/wrong_covariance_unsize_coercion.stderr diff --git a/tests-extra/invalid/wrong_covariance_unsize_coercion.rs b/tests-extra/invalid/wrong_covariance_unsize_coercion.rs new file mode 100644 index 0000000..44e7a32 --- /dev/null +++ b/tests-extra/invalid/wrong_covariance_unsize_coercion.rs @@ -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>; + +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())); +} diff --git a/tests-extra/invalid/wrong_covariance_unsize_coercion.stderr b/tests-extra/invalid/wrong_covariance_unsize_coercion.stderr new file mode 100644 index 0000000..784ff7d --- /dev/null +++ b/tests-extra/invalid/wrong_covariance_unsize_coercion.stderr @@ -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)