Skip to content

Rust: Resolve function calls to traits methods #19575

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
May 27, 2025

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented May 24, 2025

This PR adds resolution for function calls to trait methods ATrait::some_method(obj, ...).

Key changes:

  • Introduce an abstract MethodCall abstract class that is extended by the different types of expressions that can lead to method calls. Later this class can also be extended by overloaded operators that perform method calls.
  • Added a methodCandidateTrait predicate, which is similar to the existing methodCandidate predicate but which also takes a trait that the method should come from. potentialInstantiationOf now accounts both for method calls that target a specific trait.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label May 24, 2025
@paldepind paldepind force-pushed the rust/function-call-method branch from 051938a to e778cbe Compare May 24, 2025 08:26
Copy link

@BoomchainLabs BoomchainLabs left a comment

Choose a reason for hiding this comment

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

Lgtm

// trait bound on the type parameter.
result = getTypeParameterMethod(receiver.getTypeAt(TypePath::nil()), receiver.getField())
)
Function resolveMethodCallTarget(MethodCall mc) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This resolveMethodCallTarget was previously getStaticTarget inside MethodCallExpr. But since we now need for CallExpr as well, I've moved it in here.

methodCandidateTrait(rootType, mc.getTrait(), name, arity, impl)
or
not exists(mc.getTrait()) and
methodCandidate(rootType, name, arity, impl)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new disjunct and methodCandidateTrait is the crux of the change.

@paldepind paldepind marked this pull request as ready for review May 26, 2025 07:11
@paldepind paldepind requested a review from a team as a code owner May 26, 2025 07:11
hvitved
hvitved previously approved these changes May 26, 2025
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

LGTM. Let's wait for DCA before merging.

*/
pragma[nomagic]
private predicate methodCandidateTrait(Type type, Trait trait, string name, int arity, Impl impl) {
trait = resolvePath(impl.getTrait().(PathTypeRepr).getPath()) and
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, trait = resolvePath(impl.(ImplItemNode).getTraitPath())

@paldepind
Copy link
Contributor Author

DCA seems fine to me.

"Missing call targets" went down, but I don't think these trait calls where counted as missing before (because there where resolved to the trait itself), so I think the reduction in missing call targets is a secondary effect since some types are now more accurate, and hence some normal method calls can now be resolved.

@paldepind paldepind merged commit bf2cfab into github:main May 27, 2025
17 checks passed
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.

3 participants