Skip to content

Commit 0df3f97

Browse files
ivarflakstadizagawd
authored andcommitted
Add ui tests that were unsound previously and now fail on downcast_trait().unwrap() as expected
1 parent 19d11cd commit 0df3f97

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-pass
2+
#![feature(downcast_trait)]
3+
4+
use std::{any::downcast_trait, sync::OnceLock};
5+
6+
trait Trait {
7+
fn call(&self, x: &Box<i32>);
8+
}
9+
10+
impl Trait for for<'a> fn(&'a Box<i32>) {
11+
fn call(&self, x: &Box<i32>) {
12+
self(x);
13+
}
14+
}
15+
16+
static STORAGE: OnceLock<&'static Box<i32>> = OnceLock::new();
17+
18+
fn store(x: &'static Box<i32>) {
19+
STORAGE.set(x).unwrap();
20+
}
21+
22+
fn main() {
23+
let data = Box::new(Box::new(1i32));
24+
let fn_ptr: fn(&'static Box<i32>) = store;
25+
downcast_trait::<_, dyn Trait>(&fn_ptr)
26+
.unwrap()
27+
.call(&*data);
28+
drop(data);
29+
println!("{}", STORAGE.get().unwrap());
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ run-pass
2+
#![feature(downcast_trait)]
3+
use std::{any::downcast_trait, sync::OnceLock};
4+
5+
trait Trait<T> {
6+
fn call(&self, t: T, x: &Box<i32>);
7+
}
8+
9+
impl Trait<for<'a> fn(&'a Box<i32>)> for () {
10+
fn call(&self, f: for<'a> fn(&'a Box<i32>), x: &Box<i32>) {
11+
f(x);
12+
}
13+
}
14+
15+
static STORAGE: OnceLock<&'static Box<i32>> = OnceLock::new();
16+
17+
fn store(x: &'static Box<i32>) {
18+
STORAGE.set(x).unwrap();
19+
}
20+
21+
fn main() {
22+
let data = Box::new(Box::new(1i32));
23+
downcast_trait::<_, dyn Trait<fn(&'static Box<i32>)>>(&())
24+
.unwrap()
25+
.call(store, &*data);
26+
drop(data);
27+
println!("{}", STORAGE.get().unwrap());
28+
}

0 commit comments

Comments
 (0)