-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Path resolution for inherited associated items #18808
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
Rust: Path resolution for inherited associated items #18808
Conversation
38fc730
to
ab74d90
Compare
or | ||
// an inherited function, either from a trait bound or from an `impl` block | ||
exists(ItemNode node | | ||
result = node.(ItemNode).getASuccessorRec(name) and | ||
result instanceof Function | ||
| | ||
node = this.(TraitItemNode).resolveABound() | ||
or | ||
this = node.(ImplItemNode).resolveSelfTy() | ||
) |
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.
or | |
// an inherited function, either from a trait bound or from an `impl` block | |
exists(ItemNode node | | |
result = node.(ItemNode).getASuccessorRec(name) and | |
result instanceof Function | |
| | |
node = this.(TraitItemNode).resolveABound() | |
or | |
this = node.(ImplItemNode).resolveSelfTy() | |
) | |
// a trait has access to the associated items of its supertraits | |
result = this.(TraitItemNode).resolveABound().getASuccessorRec(name) | |
or | |
// items made available by an implementation where `this` is the implementing type | |
exists(ItemNode node | | |
this = node.(ImplItemNode).resolveSelfTy() and | |
result = node.getASuccessorRec(name) | |
) |
Thoughts:
- Splits this into two cases which I think is easier to read.
- Doesn't use the word "inherited". The word makes sense, but also has some OO baggage and Rust is usually said to not have inheritance.
- Removes the
instanceof Function
. From reading the docs I don't think that restriction is correct. This text seems to indicate that subtraits get access to everything (not just functions). The example here shows how a path (color::Color::WHITE
) can refer to aconst
in animpl
block. If that is correct, it would probably be a good idea to include tests for those things as well. - Is the use of
resolveABound
precise? A trait can have bounds that are not onSelf
and those don't lead to a supertrait.
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.
All good points.
Splits this into two cases which I think is easier to read.
Will do.
Removes the instanceof Function. From reading the docs I don't think that restriction is correct. This text seems to indicate that subtraits get access to everything (not just functions). The example here shows how a path (color::Color::WHITE) can refer to a const in an impl block. If that is correct, it would probably be a good idea to include tests for those things as well.
I'll instead formulate the restriction positively using AssocItem
; for example, sub traits should not have access to type parameters of super traits.
Is the use of resolveABound precise? A trait can have bounds that are not on Self and those don't lead to a supertrait.
resolveABound
only looks at TypeBoundList
, and not WhereClause
(which should be done follow-up).
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'll instead formulate the restriction positively using AssocItem; for example, sub traits should not have access to type parameters of super traits.
I see. AssocItem
is exactly what we need then 👍
resolveABound
only looks at TypeBoundList, and not WhereClause (which should be done follow-up).
Great, thanks :)
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.
LGTM! 🎉
Functions inherited through trait bounds or
impl
blocks are now modeled in the path resolution logic.