You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reflection relies on TypeId for a lot of things. And to get this TypeId it seems sensible to do something like value.type_id(). However, in a majority of cases within reflection code, this is not what you want.
A lot of times, value isn't a concrete type, but a type-erased dyn Reflect trait object. This means the TypeId returned by value.type_id() is actually the TypeId of the trait object, not the underlying type.
Instead, what you really want is to get the TypeId from the associated TypeInfo. This can be done either by calling and unwrapping value.get_represented_type_info() or, in 0.15, calling value.reflect_type_info() (assuming value is a dyn Reflect and not a dyn PartialReflect). Then with the TypeInfo you can call info.type_id().
In other words, this is wrong:
let value:Box<dynReflect> = Box::new(123u32);let type_id = value.type_id();assert_eq!(type_id,TypeId::of::<u32>());
This is right:
let value:Box<dynReflect> = Box::new(123u32);let type_id = value.get_represented_type_info().unwrap().type_id();assert_eq!(type_id,TypeId::of::<u32>());
Solution
We should add a lint to warn against possibly incorrect usages of std::any::Any::type_id when the type of the receiver is dyn Reflect (or dyn PartialReflect starting in 0.15).
It should point out the potential footgun with using std::any::Any::type_id and point the user to an appropriate alternative: Reflect::get_represented_type_info (or Reflect::reflect_type_info/PartialReflect::get_represented_type_info for 0.15).
Possible name: reflect_object_type_id?
Possible group: SUSPICIOUS
The text was updated successfully, but these errors were encountered:
Problem
Reflection relies on
TypeId
for a lot of things. And to get thisTypeId
it seems sensible to do something likevalue.type_id()
. However, in a majority of cases within reflection code, this is not what you want.A lot of times,
value
isn't a concrete type, but a type-eraseddyn Reflect
trait object. This means theTypeId
returned byvalue.type_id()
is actually theTypeId
of the trait object, not the underlying type.Instead, what you really want is to get the
TypeId
from the associatedTypeInfo
. This can be done either by calling and unwrappingvalue.get_represented_type_info()
or, in 0.15, callingvalue.reflect_type_info()
(assuming value is adyn Reflect
and not adyn PartialReflect
). Then with theTypeInfo
you can callinfo.type_id()
.In other words, this is wrong:
This is right:
Solution
We should add a lint to warn against possibly incorrect usages of
std::any::Any::type_id
when the type of the receiver isdyn Reflect
(ordyn PartialReflect
starting in 0.15).It should point out the potential footgun with using
std::any::Any::type_id
and point the user to an appropriate alternative:Reflect::get_represented_type_info
(orReflect::reflect_type_info
/PartialReflect::get_represented_type_info
for 0.15).Possible name:
reflect_object_type_id
?Possible group:
SUSPICIOUS
The text was updated successfully, but these errors were encountered: