Reflection TypeId::trait_info_of#152003
Conversation
|
Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr Some changes occurred to the CTFE machinery Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri The reflection data structures are tied exactly to the implementation cc @oli-obk |
|
Lovely, great job! |
|
The documentation of
The |
This comment has been minimized.
This comment has been minimized.
|
Trying to create a vtable for a DST results in an ICE with this PR. #![feature(type_info)]
use std::any::TypeId;
trait Trait {}
impl Trait for [i32] {}
fn main() {
const { TypeId::of::<[i32]>().trait_info_of_trait_type_id(TypeId::of::<dyn Trait>()) };
}Error output |
|
Failure to compute the layout of the type also results in an ICE with this PR. #![feature(type_info)]
use std::any::TypeId;
trait Trait {}
impl Trait for [u8; 1 << 63] {}
pub fn main() {
const { TypeId::of::<[u8; 1 << 63]>().trait_info_of_trait_type_id(TypeId::of::<dyn Trait>()) };
}Error output |
|
Do any of the issues mentioned so far apply to the existing functions on nightly, too? If so, we should address them there first and add tests for them |
|
Code & Error 1#![feature(type_info)]
fn main() {
dbg!(const {
std::any::TypeId::of::<[u8; 1<<63]>().info()
});
}Code & Error 2#![feature(type_info)]
trait Apply {
type Output<T>;
}
struct Identity;
impl Apply for Identity {
type Output<T> = T;
}
struct Thing<A: Apply>(<A as Apply>::Output<Self>);
/*
Effectively:
struct Thing_Identity(Thing_Identity);
*/
fn main() {
dbg!(const {
std::any::TypeId::of::<Thing<Identity>>().info()
});
}
I think neither |
According to my understanding DST's cannot be converted to dyn, right? So returning None would be correct? |
I don't get this ICE on arch linux, x86_64 cpu. I added a ui test for this. |
This is not a problem because both functions return None when attempting to get the vtable of a non dyn Type. |
|
bors r+ |
Yes
Your latest commit calls
That's not how it works. Here's code that produces a #![feature(type_info, ptr_metadata)]
use std::{any::TypeId, ptr::DynMetadata};
struct Thing;
trait Trait {}
impl Trait for Thing {}
pub fn main() {
let metadata: DynMetadata<*const ()> =
const { TypeId::of::<Thing>().trait_info_of_trait_type_id(TypeId::of::<dyn Trait>()) }
.unwrap()
.get_vtable();
println!("{metadata:?}");
} |
This comment has been minimized.
This comment has been minimized.
So what should we do? The trait_info_of_trait_type_id vtable is for unknown dyn type. We could make this function just return a bool. |
Guess I have to adapt it to from |
you can use |
This comment has been minimized.
This comment has been minimized.
01debba to
1c9248b
Compare
1c9248b to
0a6fce7
Compare
This is for #146922.
As #151236 was requested to be remade by someone I implemented the functionality as
TypeId::trait_info_ofwhich additionally allows getting the vtable pointer to builddynObjects in recursive reflection.It allows checking if a TypeId implements a trait. Since this is my first PR feel free to tell me if there are any formal issues.