-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Intrinsic for type_name_of_id
to power a better impl Debug for TypeId
?
#61533
Comments
I think this is a cool idea. However, to implement this we would probably have to add some sort of type table to each application binary. That seems like it could become extremely expensive as each instantiation of a generic type would be stored separately. |
The amount of extra memory needed is fairly limited, as most of these type names will already have ended up somewhere in the binary. So we could just have the other places refer to the table entries instad of having their own version of the name. For release builds we could even just skip this table entirely and fall back to id printing. Alternatively we can make the |
Ostensibly we need something like this to deal with the soundness hole in #10389? |
oh, you mean the solution where each |
Interesting ideas! If I had my druthers it would be feasible to use this in release mode as well, as I am interested in using the output for logging. |
@oli-obk Something in that spirit yeah. |
I imagine this as follows. We would have a: trait TypeName {
fn name(&self) -> &'static str;
} that is implemented for all types: impl<T: ?Sized> TypeName for T {
#[cfg(rtti)]
#[lang_item = "type_name_blanket_impl"]
fn name(&self) -> &'static str;
#[cfg(not(rtti))]
fn name(&self) -> &'static str { "rtti-disabled" }
} Then we would have a global compiler switch, e.g., |
(cross-linking) |
Update: finally opened #95845 with a (v0) mangling-based |
Would it be possible to avoid the explosion of binary size if we simply avoided storing table entries for types that don't have The main problem I can see with this is the generic impl for I don't know if existing vtables for types that get turned into That is perhaps a way to do it? Might also improve optimisation speed if this isn't already the case, since you avoid many instantiations of trait code for generic impls. |
The Rust compiler uses monomorphization, i.e. only the instances of generic functions (which includes A good way to think about it is that the possible set of types is effectively infinite - even if in specific cases there may be, at most, e.g. (on 64-targets) only 264 (This is, incidentally, a significant historical factor in Shame #95845 didn't happen, it would've been a good way to get this capability into Rust. Arguably that approach could be made opt-in, or handled via debuginfo instead (e.g. labeling a |
Currently
TypeId
s have uninformative derivedDebug
impls:This results in fairly poor Debug output for dynamic types like
anymap
.I think it could be quite nice for debugging/logging/etc to allow printing the type name from a
TypeId
in the Debug impl. It would provide an out of the box improvement to debugging existing dynamic typing tools, and IIUC the contents of Debug impls in the standard library are not considered stable so there's neither a breaking change here nor a de facto stabilization of the type_name representation.I assume this would need to rely on some unstable intrinsic being exposed to get the type_name of an ID at run time, but I'm not really aware what would be needed.
Thoughts? cc @oli-obk as we had discussed this a bit on IRC.
The text was updated successfully, but these errors were encountered: