-
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
Tracking issue for type_id stabilization #27745
Comments
It was pointed out it's one of the very few methods to be named with |
I suspect this is just some very old convention that wasn't updated during stabilization (since we didn't stabilize this API). |
Any progress here? |
I tried to tackle it, but it looks like the main problem is that (Though making an extern function const is probably dicey from a compiler perspective anyway, I'm not an expert on that). |
Removing nomination as this was an accidental mistake about what this issue was about. |
Any update on this? Is is discouraged to use it in current code? |
Is there any other way to do reflection or dynamic typing that will work? |
@jweinst1 the rest of the fn is<T>(&self) -> bool where T: Any;
fn downcast_ref<T>(&self) -> Option<&T> where T: Any;
fn downcast_mut<T>(&mut self) -> Option<&mut T> where T: Any;
fn downcast<T>(Box<self>) -> Result<Box<T>, Box<Any>> where T: Any ; In each of these the call site is specifying a specific |
I’ve just realized that |
I propose we make Once that's done, we can add a constant to @SimonSapin I don't think it's that easy since there exist methods in the wild that pass around trait objects ( |
It might be better off as an associated constant if possible. |
I might not be understanding, but I think that's what I suggested? That we have something like |
Oh right derp, there isn't anywhere to put the constant on TypeId itself since it isn't parameterized. |
Alright, I'm working on this and I think I managed to turn Edit: I solved the mistery, the const needs to be inserted in the |
I wonder if #10389 is relevant here |
Turn `type_id` into a constant intrinsic rust-lang#27745 The method `get_type_id` in `Any` is intended to support reflection. It's currently unstable in favor of using an associated constant instead. This PR makes the `type_id` intrinsic a constant intrinsic, the same as `size_of` and `align_of`, allowing `TypeId::of` to be a `const fn`, which will allow using an associated constant in `Any`.
It turns out that associated constants cannot be used in trait objects, because the constants aren't stored in the vtable. So this won't work: trait Any {
const TYPE_ID: TypeId = TypeId::of::<Self>();
} And this also won't work, because it just stores the trait Any {}
impl Any {
const TYPE_ID: TypeId = TypeId::of::<Self>();
} So assuming we still want to make TYPE_ID an associated constant, we need to add support for associated constants in trait objects, or make this a special case and add a dedicated field to the vtable which stores the |
I think this wouldn’t work? This code: fn foo(object: &Any) {
let type_id = object.get_type_id();
} … can call a trait method from a trait object. Accessing an associated constant would be Other code that does have a concrete So I propose keeping this method as a method, renaming to |
That seems reasonable. |
@rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Seems like we might as well make it a const method while we're stabilizing it. It should be pretty trivial since |
Unfortunate timing, considering a new syntax will be introduced for them. This would otherwise be a good opportunity to introduce small breaking changes to behavior. But I guess that's off topic here. |
The final comment period is now complete. |
Are there any plans to implement this, or RFCs that specify that? |
That's the exact motivation for stabilising this: as a trait method it is directly callable on trait objects extending This issue is even directly linked from comments in the We should stabilize this as-is - I don't see how performance is a concern since we already have |
From #49613 (comment) @Centril should this have the See also, #27745 (comment) |
@tmccombs unclear; I defer to @rust-lang/libs |
How likely is this to be stabilized? Will the |
Very, since FCP to stabilize is done. Yes, since that’s was part of the FCP proposal. Stabilization PR: #57834 |
Stabilize Any::get_type_id and rename to type_id FCP: rust-lang#27745 (comment) Closes rust-lang#27745.
Thank you @SimonSapin. I wasn't sure since the FCP was months ago. |
Yeah, sometimes we just forget to follow up. Pinging open issues like you did can help, thanks! |
This should have been part of rust-lang#57834 FCP: rust-lang#27745 (comment)
Stabilize std::error::Error::type_id This should have been part of #57834 FCP: #27745 (comment)
The method
get_type_id
inAny
is intended to support reflection. It's currently unstable in favor of using an associated constant instead.The text was updated successfully, but these errors were encountered: