Skip to content

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

Merged
merged 3 commits into from
Feb 19, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Feb 18, 2025

Functions inherited through trait bounds or impl blocks are now modeled in the path resolution logic.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Feb 18, 2025
@hvitved hvitved force-pushed the rust/path-resolution-inherited-functions branch from 38fc730 to ab74d90 Compare February 18, 2025 12:49
@hvitved hvitved marked this pull request as ready for review February 18, 2025 13:02
@hvitved hvitved requested a review from paldepind February 18, 2025 13:02
Comment on lines 133 to 142
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()
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 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.
  • Is the use of resolveABound precise? A trait can have bounds that are not on Self and those don't lead to a supertrait.

Copy link
Contributor Author

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).

Copy link
Contributor

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 :)

@hvitved hvitved changed the title Rust: Path resolution for inherited functions Rust: Path resolution for inherited associated items Feb 19, 2025
@hvitved hvitved requested a review from paldepind February 19, 2025 14:30
Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉

@hvitved hvitved merged commit c22021a into github:main Feb 19, 2025
16 checks passed
@hvitved hvitved deleted the rust/path-resolution-inherited-functions branch February 19, 2025 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants