-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Support trait objects in type info reflection #151239
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
base: main
Are you sure you want to change the base?
Support trait objects in type info reflection #151239
Conversation
|
|
|
I don't mind if anyone forks the branch to implement the for<'a> part, or even make a separate PR for a better implementation |
| Type { | ||
| kind: DynTrait( | ||
| DynTrait { | ||
| super_traits: [ | ||
| TypeId(0xf726af39bcd0090512f636802780d009), | ||
| TypeId(0xd3eba1307d3a0b58acd77b80e4532fbf), | ||
| ], | ||
| is_auto: false, | ||
| auto_traits: [ | ||
| TypeId(0x0d5e48167084e668b711d10061f0446a), | ||
| ], | ||
| }, | ||
| ), | ||
| size: None, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trait A {}
trait B: A {}
trait C: B {}The quoted dump is the type info for dyn C + Send. How do we determine whether it has trait C?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't think I fully understand the question. Could you elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clear, I replaced the type ID number with type name in the following examples.
First, TypeId(dyn C) != TypeId(dyn C + Send), then the type info of dyn C + Send is
Type {
kind: DynTrait(
DynTrait {
super_traits: [
TypeId(B),
TypeId(A),
],
is_auto: false,
auto_traits: [
TypeId(Send),
],
},
),
size: None,
}
For the current PR implementation, if given a dyn C + Send (or type ID of it), we can know there are A, B and Send but no way of knowing C.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not include C in it since C is not a super trait of itself. I am assuming you want a field that represents the TypeId of the trait object itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly, dyn C and dyn C + Send are two different things, so it's not "the trait object itself".
Intuitively, I think DynTrait's layout should probably look something like this. Take dyn C + Send as an example again.
Type {
kind: DynTrait(
DynTrait {
predicates: [ Predicate(Trait(C)), Predicate(Trait(Send)) ]
},
),
size: None,
}
Predicate {
trait,
negative: bool, // Not sure if possible, #144241
}
Trait(C) = Trait {
supers: [ Trait(B), Trait(A) ],
is_auto: false,
}
Trait(Send) = Trait {
supers: [],
is_auto: true,
}
Trait(B) = ...
Trait(A) = ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes a lot of sense! I will go with this
|
An ICE occurs when trying to get the type_info of a dyn trait, in which one of it's supertrait has for<'a> (eg |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
Tracking issue: #146922
I can't seem to get it to work correctly with
dyn for<'a> Foo<'a>, though it works fine for normaldyn Footrait objectsr? @oli-obk