forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lukas Markeffsky
committed
Jan 18, 2024
1 parent
2457c02
commit afb5668
Showing
11 changed files
with
443 additions
and
1 deletion.
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
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
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 |
---|---|---|
|
@@ -1015,6 +1015,7 @@ symbols! { | |
memtag, | ||
message, | ||
meta, | ||
metadata_cast, | ||
metadata_type, | ||
min_align_of, | ||
min_align_of_val, | ||
|
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
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
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
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
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
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,56 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/metadata-cast.rs:63:5 | ||
| | ||
LL | fn lifetimes_err1<'a, 'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | check::<DynMetadata<&'a ()>, DynMetadata<&'b ()>>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'b` | ||
| | ||
= help: consider adding the following bound: `'a: 'b` | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/metadata-cast.rs:63:5 | ||
| | ||
LL | fn lifetimes_err1<'a, 'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | check::<DynMetadata<&'a ()>, DynMetadata<&'b ()>>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a` | ||
| | ||
= help: consider adding the following bound: `'b: 'a` | ||
|
||
help: `'a` and `'b` must be the same: replace one with the other | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/metadata-cast.rs:68:5 | ||
| | ||
LL | fn lifetimes_err2<'a, 'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | check::<&'a (), &'b ()>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'b` | ||
| | ||
= help: consider adding the following bound: `'a: 'b` | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/metadata-cast.rs:68:5 | ||
| | ||
LL | fn lifetimes_err2<'a, 'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | check::<&'a (), &'b ()>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a` | ||
| | ||
= help: consider adding the following bound: `'b: 'a` | ||
|
||
help: `'a` and `'b` must be the same: replace one with the other | ||
| | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error: aborting due to 4 previous errors | ||
|
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,92 @@ | ||
// revisions: traits lifetimes | ||
#![feature(ptr_metadata, ptr_metadata_cast)] | ||
|
||
use std::ptr::{Pointee, MetadataCast, DynMetadata}; | ||
|
||
fn cast<T: ?Sized, U: ?Sized>(_: *mut T) -> *mut U | ||
where | ||
<T as Pointee>::Metadata: MetadataCast<<U as Pointee>::Metadata>, | ||
{ | ||
todo!() | ||
} | ||
|
||
fn check<T: ?Sized, U: ?Sized>() where T: MetadataCast<U> {} | ||
|
||
trait Trait {} | ||
trait Trait2 {} | ||
|
||
fn main() { | ||
// any to () is OK | ||
check::<(), ()>(); | ||
check::<usize, ()>(); | ||
check::<DynMetadata<dyn Trait>, ()>(); | ||
|
||
// same types are OK | ||
check::<usize, usize>(); | ||
check::<i32, i32>(); | ||
check::<DynMetadata<dyn Trait>, DynMetadata<dyn Trait>>(); | ||
|
||
// changing auto traits of trait object in DynMetadata is OK | ||
check::<DynMetadata<dyn Send>, DynMetadata<dyn Sync>>(); | ||
check::<DynMetadata<dyn Trait + Send>, DynMetadata<dyn Trait + Sync>>(); | ||
|
||
#[cfg(traits)] | ||
{ | ||
check::<(), usize>(); //[traits]~ ERROR not satisfied | ||
check::<(), DynMetadata<dyn Trait>>(); //[traits]~ ERROR not satisfied | ||
check::<usize, DynMetadata<dyn Trait>>(); //[traits]~ ERROR not satisfied | ||
check::<DynMetadata<dyn Trait>, usize>(); //[traits]~ ERROR not satisfied | ||
check::<DynMetadata<dyn Trait>, DynMetadata<dyn Trait2>>(); //[traits]~ ERROR not satisfied | ||
check::<dyn Trait + Send, dyn Trait + Sync>(); //[traits]~ ERROR not satisfied | ||
} | ||
|
||
// `dyn MetadataCast<usize>` should not implement `MetadataCast<usize>` | ||
check::<dyn MetadataCast<usize>, ()>(); | ||
check::<dyn MetadataCast<usize>, dyn MetadataCast<usize>>(); | ||
#[cfg(traits)] | ||
check::<dyn MetadataCast<usize>, usize>(); //[traits]~ ERROR not satisfied | ||
|
||
// this could pass in the future, but for now it matches the behavior of `as` casts | ||
#[cfg(traits)] | ||
check::<DynMetadata<dyn Trait>, DynMetadata<dyn Send>>(); //[traits]~ ERROR not satisfied | ||
} | ||
|
||
fn lifetimes_ok<'a, 'b>() { | ||
// changing lifetimes of trait object in DynMetadata is OK | ||
check::<DynMetadata<dyn Trait + 'a>, DynMetadata<dyn Trait + 'b>>(); | ||
check::<DynMetadata<dyn Trait + 'b>, DynMetadata<dyn Trait + 'a>>(); | ||
|
||
// otherwise, require equal lifetimes | ||
check::<&'a (), &'a ()>(); | ||
} | ||
fn lifetimes_err1<'a, 'b>() { | ||
check::<DynMetadata<&'a ()>, DynMetadata<&'b ()>>(); | ||
//[lifetimes]~^ ERROR may not live long enough | ||
//[lifetimes]~| ERROR may not live long enough | ||
} | ||
fn lifetimes_err2<'a, 'b>() { | ||
check::<&'a (), &'b ()>(); | ||
//[lifetimes]~^ ERROR may not live long enough | ||
//[lifetimes]~| ERROR may not live long enough | ||
} | ||
|
||
fn do_casts<'a>(thin: &mut i32, slice: &mut [i32], trait_object: &mut dyn Trait) { | ||
let _: *mut u8 = cast(thin); | ||
let _: *mut u8 = cast(slice); | ||
let _: *mut [u8] = cast(slice); | ||
let _: *mut u8 = cast(trait_object); | ||
let _: *mut (dyn Trait + Send + 'a) = cast(trait_object); | ||
|
||
|
||
#[cfg(traits)] | ||
{ | ||
let _: *mut [u8] = cast(thin); //[traits]~ ERROR not satisfied | ||
let _: *mut dyn Trait = cast(thin); //[traits]~ ERROR not satisfied | ||
let _: *mut [u8] = cast(trait_object); //[traits]~ ERROR not satisfied | ||
let _: *mut dyn Trait = cast(slice); //[traits]~ ERROR not satisfied | ||
let _: *mut dyn Trait2 = cast(trait_object); //[traits]~ ERROR not satisfied | ||
|
||
// may be allowed in the future | ||
let _: *mut dyn Send = cast(trait_object); //[traits]~ ERROR not satisfied | ||
} | ||
} |
Oops, something went wrong.