mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Add tests for *const Trait<'a>
-> *const Trait<'b>
and similar casts
This commit is contained in:
parent
d06cf5b399
commit
9e8ef92da0
9
tests/ui/cast/ptr-to-trait-obj-add-auto.rs
Normal file
9
tests/ui/cast/ptr-to-trait-obj-add-auto.rs
Normal file
@ -0,0 +1,9 @@
|
||||
// check-pass
|
||||
|
||||
trait Trait<'a> {}
|
||||
|
||||
fn add_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send) {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn main() {}
|
@ -28,3 +28,11 @@ fn generic<T>(x: *const dyn Trait<X>, t: *const dyn Trait<T>) {
|
||||
let _: *const dyn Trait<T> = x as _; //~ error: casting `*const (dyn Trait<X> + 'static)` as `*const dyn Trait<T>` is invalid
|
||||
let _: *const dyn Trait<X> = t as _; //~ error: casting `*const (dyn Trait<T> + 'static)` as `*const dyn Trait<X>` is invalid
|
||||
}
|
||||
|
||||
trait Assocked {
|
||||
type Assoc: ?Sized;
|
||||
}
|
||||
|
||||
fn change_assoc(x: *mut dyn Assocked<Assoc = u8>) -> *mut dyn Assocked<Assoc = u32> {
|
||||
x as _
|
||||
}
|
31
tests/ui/cast/ptr-to-trait-obj-different-regions-lt-ext.rs
Normal file
31
tests/ui/cast/ptr-to-trait-obj-different-regions-lt-ext.rs
Normal file
@ -0,0 +1,31 @@
|
||||
// check-pass
|
||||
//
|
||||
// issue: <https://github.com/rust-lang/rust/issues/120217>
|
||||
|
||||
#![feature(arbitrary_self_types)]
|
||||
|
||||
trait Static<'a> {
|
||||
fn proof(self: *const Self, s: &'a str) -> &'static str;
|
||||
}
|
||||
|
||||
fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
|
||||
x as _
|
||||
}
|
||||
|
||||
impl Static<'static> for () {
|
||||
fn proof(self: *const Self, s: &'static str) -> &'static str {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
fn extend_lifetime(s: &str) -> &'static str {
|
||||
bad_cast(&()).proof(s)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = String::from("Hello World");
|
||||
let slice = extend_lifetime(&s);
|
||||
println!("Now it exists: {slice}");
|
||||
drop(s);
|
||||
println!("Now it’s gone: {slice}");
|
||||
}
|
34
tests/ui/cast/ptr-to-trait-obj-different-regions-misc.rs
Normal file
34
tests/ui/cast/ptr-to-trait-obj-different-regions-misc.rs
Normal file
@ -0,0 +1,34 @@
|
||||
// check-pass
|
||||
|
||||
trait Trait<'a> {}
|
||||
|
||||
fn change_lt<'a, 'b>(x: *mut dyn Trait<'a>) -> *mut dyn Trait<'b> {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn change_lt_ab<'a: 'b, 'b>(x: *mut dyn Trait<'a>) -> *mut dyn Trait<'b> {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn change_lt_ba<'a, 'b: 'a>(x: *mut dyn Trait<'a>) -> *mut dyn Trait<'b> {
|
||||
x as _
|
||||
}
|
||||
|
||||
trait Assocked {
|
||||
type Assoc: ?Sized;
|
||||
}
|
||||
|
||||
fn change_assoc_0<'a, 'b>(
|
||||
x: *mut dyn Assocked<Assoc = dyn Send + 'a>,
|
||||
) -> *mut dyn Assocked<Assoc = dyn Send + 'b> {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn change_assoc_1<'a, 'b>(
|
||||
x: *mut dyn Assocked<Assoc = dyn Trait<'a>>,
|
||||
) -> *mut dyn Assocked<Assoc = dyn Trait<'b>> {
|
||||
x as _
|
||||
}
|
||||
|
||||
|
||||
fn main() {}
|
17
tests/ui/cast/ptr-to-trait-obj-ok.rs
Normal file
17
tests/ui/cast/ptr-to-trait-obj-ok.rs
Normal file
@ -0,0 +1,17 @@
|
||||
// check-pass
|
||||
|
||||
trait Trait<'a> {}
|
||||
|
||||
fn remove_auto<'a>(x: *mut (dyn Trait<'a> + Send)) -> *mut dyn Trait<'a> {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn cast_inherent_lt<'a, 'b>(x: *mut (dyn Trait<'static> + 'a)) -> *mut (dyn Trait<'static> + 'b) {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn unprincipled<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) {
|
||||
x as _
|
||||
}
|
||||
|
||||
fn main() {}
|
Loading…
Reference in New Issue
Block a user