forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#110878 - whtahy:105107/known-bug-tests-for-…
…unsound-issues, r=jackh726 Add `known-bug` tests for 4 unsound issues This PR adds `known-bug` tests for 4 unsound issues as part of rust-lang#105107 - rust-lang#40582 - rust-lang#49682 - rust-lang#74629 - rust-lang#105782
- Loading branch information
Showing
4 changed files
with
152 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,41 @@ | ||
// check-pass | ||
// known-bug: #74629 | ||
|
||
// Should fail. The `0` and `1` impls overlap, violating coherence. Eg, with | ||
// `T = Test, F = ()`, all bounds are true, making both impls applicable. | ||
// `Test: Fold<Nil>`, `Test: Fold<()>` are true because of `2`. | ||
// `Is<Test>: NotNil` is true because of `auto trait` and lack of negative impl. | ||
|
||
#![feature(negative_impls)] | ||
#![feature(auto_traits)] | ||
|
||
struct Nil; | ||
struct Cons<H>(H); | ||
struct Test; | ||
|
||
trait Fold<F> {} | ||
|
||
impl<T, F> Fold<F> for Cons<T> // 0 | ||
where | ||
T: Fold<Nil>, | ||
{} | ||
|
||
impl<T, F> Fold<F> for Cons<T> // 1 | ||
where | ||
T: Fold<F>, | ||
private::Is<T>: private::NotNil, | ||
{} | ||
|
||
impl<F> Fold<F> for Test {} // 2 | ||
|
||
mod private { | ||
use crate::Nil; | ||
|
||
pub struct Is<T>(T); | ||
pub auto trait NotNil {} | ||
|
||
#[allow(suspicious_auto_trait_impls)] | ||
impl !NotNil for Is<Nil> {} | ||
} | ||
|
||
fn main() {} |
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,35 @@ | ||
// check-pass | ||
// known-bug: #40582 | ||
|
||
// Should fail. Should not be possible to implement `make_static`. | ||
|
||
#![feature(specialization)] | ||
#![allow(incomplete_features)] | ||
|
||
trait FromRef<'a, T: ?Sized> { | ||
fn from_ref(r: &'a T) -> Self; | ||
} | ||
|
||
impl<'a, T: ?Sized> FromRef<'a, T> for &'a T { | ||
fn from_ref(r: &'a T) -> Self { | ||
r | ||
} | ||
} | ||
|
||
impl<'a, T: ?Sized, R> FromRef<'a, T> for R { | ||
default fn from_ref(_: &'a T) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn make_static<T: ?Sized>(data: &T) -> &'static T { | ||
fn helper<T: ?Sized, R>(data: &T) -> R { | ||
R::from_ref(data) | ||
} | ||
helper(data) | ||
} | ||
|
||
fn main() { | ||
let s = "specialization".to_owned(); | ||
println!("{:?}", make_static(s.as_str())); | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/ui/specialization/specialization-default-items-drop-coherence.rs
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,30 @@ | ||
// check-pass | ||
// known-bug: #105782 | ||
|
||
// Should fail. Default items completely drop candidates instead of ambiguity, | ||
// which is unsound during coherence, since coherence requires completeness. | ||
|
||
#![feature(specialization)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Default { | ||
type Id; | ||
} | ||
|
||
impl<T> Default for T { | ||
default type Id = T; | ||
} | ||
|
||
trait Overlap { | ||
type Assoc; | ||
} | ||
|
||
impl Overlap for u32 { | ||
type Assoc = usize; | ||
} | ||
|
||
impl Overlap for <u32 as Default>::Id { | ||
type Assoc = Box<usize>; | ||
} | ||
|
||
fn main() {} |
46 changes: 46 additions & 0 deletions
46
tests/ui/thread-local/thread-local-static-ref-use-after-free.rs
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,46 @@ | ||
// check-pass | ||
// known-bug: #49682 | ||
// edition:2021 | ||
|
||
// Should fail. Keeping references to thread local statics can result in a | ||
// use-after-free. | ||
|
||
#![feature(thread_local)] | ||
|
||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::thread; | ||
|
||
#[allow(dead_code)] | ||
#[thread_local] | ||
static FOO: AtomicUsize = AtomicUsize::new(0); | ||
|
||
#[allow(dead_code)] | ||
async fn bar() {} | ||
|
||
#[allow(dead_code)] | ||
async fn foo() { | ||
let r = &FOO; | ||
bar().await; | ||
r.load(Ordering::SeqCst); | ||
} | ||
|
||
fn main() { | ||
// &FOO = 0x7fd1e9cbf6d0 | ||
_ = thread::spawn(|| { | ||
let g = foo(); | ||
println!("&FOO = {:p}", &FOO); | ||
g | ||
}) | ||
.join() | ||
.unwrap(); | ||
|
||
// &FOO = 0x7fd1e9cc0f50 | ||
println!("&FOO = {:p}", &FOO); | ||
|
||
// &FOO = 0x7fd1e9cbf6d0 | ||
thread::spawn(move || { | ||
println!("&FOO = {:p}", &FOO); | ||
}) | ||
.join() | ||
.unwrap(); | ||
} |