-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Reflection TypeId::trait_info_of #152003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
9SonSteroids
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
9SonSteroids:trait_info_of
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Reflection TypeId::trait_info_of #152003
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| mod trait_info_of; | ||
| mod type_info; | ||
|
|
||
| use core::mem::*; | ||
|
|
||
This file contains hidden or 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,70 @@ | ||
| use std::any::TypeId; | ||
| use std::ptr::DynMetadata; | ||
|
|
||
| struct Garlic(i32); | ||
| trait Blah { | ||
| fn get_truth(&self) -> i32; | ||
| } | ||
| impl Blah for Garlic { | ||
| fn get_truth(&self) -> i32 { | ||
| self.0 * 21 | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_implements_trait() { | ||
| const { | ||
| assert!(TypeId::of::<Garlic>().trait_info_of::<dyn Blah>().is_some()); | ||
| assert!(TypeId::of::<Garlic>().trait_info_of::<dyn Blah + Send>().is_some()); | ||
| assert!(TypeId::of::<*const Box<Garlic>>().trait_info_of::<dyn Sync>().is_none()); | ||
| assert!(TypeId::of::<u8>().trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()).is_none()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_dyn_creation() { | ||
| let garlic = Garlic(2); | ||
| unsafe { | ||
| assert_eq!( | ||
| std::ptr::from_raw_parts::<dyn Blah>( | ||
| &raw const garlic, | ||
| const { TypeId::of::<Garlic>().trait_info_of::<dyn Blah>() }.unwrap().get_vtable() | ||
| ) | ||
| .as_ref() | ||
| .unwrap() | ||
| .get_truth(), | ||
| 42 | ||
| ); | ||
| } | ||
|
|
||
| assert_eq!( | ||
| const { | ||
| TypeId::of::<Garlic>() | ||
| .trait_info_of_trait_type_id(TypeId::of::<dyn Blah>()) | ||
| .unwrap() | ||
| }.get_vtable(), | ||
| unsafe { | ||
| crate::mem::transmute::<_, DynMetadata<*const ()>>( | ||
| const { | ||
| TypeId::of::<Garlic>().trait_info_of::<dyn Blah>() | ||
| }.unwrap().get_vtable(), | ||
| ) | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_incorrect_use() { | ||
| assert_eq!( | ||
| const { TypeId::of::<i32>().trait_info_of_trait_type_id(TypeId::of::<u32>()) }, | ||
| None | ||
| ); | ||
| } | ||
|
|
||
| trait DstTrait {} | ||
| impl DstTrait for [i32] {} | ||
|
|
||
| #[test] | ||
| fn dst_ice() { | ||
| assert!(const { TypeId::of::<[i32]>().trait_info_of::<dyn DstTrait>() }.is_none()); | ||
| } |
This file contains hidden or 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,20 @@ | ||
| //@ normalize-stderr: "\[u8; [0-9]+\]" -> "[u8; N]" | ||
| //! Test for https://github.com/rust-lang/rust/pull/152003 | ||
|
|
||
| #![feature(type_info)] | ||
|
|
||
| use std::any::TypeId; | ||
|
|
||
| trait Trait {} | ||
| impl Trait for [u8; usize::MAX] {} | ||
|
|
||
| fn main() {} | ||
|
|
||
| const _: () = const { | ||
| TypeId::of::<[u8; usize::MAX]>().trait_info_of_trait_type_id(TypeId::of::<dyn Trait>()); | ||
| //~^ ERROR values of the type `[u8; usize::MAX]` are too big for the target architecture | ||
| }; | ||
| const _: () = const { | ||
| TypeId::of::<[u8; usize::MAX]>().trait_info_of::<dyn Trait>(); | ||
| //~^ ERROR values of the type `[u8; usize::MAX]` are too big for the target architecture | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.