Skip to content

Rust: Disambiguate some method calls based on argument types #19749

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
Jun 13, 2025

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Jun 13, 2025

This PR handles some cases where resolving the correct method requires inspecting the types of arguments. In such cases we currently resolve multiple targets, and this PR should reduce that.

I've tried to do the simplest possible thing in this PR, but I think this should be quite effective for common cases where this occurs.

Quick eval'ing getMethodFromImpl on rust shows a reduction of results by around 50%. DCA will show us if we also loose desirable things.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 13, 2025
@paldepind paldepind force-pushed the rust/impl-parameter-resolution branch 3 times, most recently from eb1b0fb to 3a6f5ca Compare June 13, 2025 08:53
@paldepind paldepind marked this pull request as ready for review June 13, 2025 09:00
@paldepind paldepind requested a review from a team as a code owner June 13, 2025 09:00
@paldepind
Copy link
Contributor Author

DCA shows that we loose call targets, which is not too surprising given that the PR is about pruning call targets.

One guess as to why we might loose correct targets, is if we (for some other reason) can't infer the type of an arguments for a call that now requires a type at an argument.

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.

Thanks a lot for doing this, certainly a very good first approximation.

* // ^ `path` = "T"
* }
* impl MyAdd<i64> for i64 {
* fn method(&self, value: i64) -> Self { ... }
Copy link
Contributor

Choose a reason for hiding this comment

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

Either it should be Foo<i64>, or remove Foo above.

Impl impl, TraitItemNode trait, Type rootType, TypeMention selfTy
) {
trait = impl.(ImplItemNode).resolveTraitTy() and
not exists(impl.getAttributeMacroExpansion()) and
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps a comment why this restriction is needed?

/** Gets a method from an `impl` block that matches the method call `mc`. */
private Function getMethodFromImpl(MethodCall mc) {
exists(Impl impl |
IsInstantiationOf<MethodCall, IsInstantiationOfInput>::isInstantiationOf(mc, impl, _) and
result = getMethodSuccessor(impl, mc.getMethodName())
|
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest the following diff, just to be sure that the method being restricted by methodResolutionDependsOnArgument is the same as the method being returned:

diff --git a/rust/ql/lib/codeql/rust/internal/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/TypeInference.qll
index 6ea2df6550c..bc80a1a14f3 100644
--- a/rust/ql/lib/codeql/rust/internal/TypeInference.qll
+++ b/rust/ql/lib/codeql/rust/internal/TypeInference.qll
@@ -1275,12 +1275,13 @@ private predicate methodTypeAtPath(Function f, int pos, TypePath path, Type type
 }
 
 /**
- * Holds if resolving the method in `impl` with the name `methodName` requires
+ * Holds if resolving the method `f` in `impl` with the name `methodName` requires
  * inspecting the types of applied _arguments_ in order to determine whether it
  * is the correct resolution.
  */
+pragma[nomagic]
 private predicate methodResolutionDependsOnArgument(
-  Impl impl, string methodName, int pos, TypePath path, Type type
+  Impl impl, Function f, string methodName, int pos, TypePath path, Type type
 ) {
   /*
    * As seen in the example below, when an implementation has a sibling for a
@@ -1310,20 +1311,21 @@ private predicate methodResolutionDependsOnArgument(
   exists(TraitItemNode trait |
     implHasSibling(impl, trait) and
     traitTypeParameterOccurrence(trait, methodName, pos, path) and
-    methodTypeAtPath(getMethodSuccessor(impl, methodName), pos, path, type)
+    methodTypeAtPath(f, pos, path, type) and
+    f = getMethodSuccessor(impl, methodName)
   )
 }
 
 /** Gets a method from an `impl` block that matches the method call `mc`. */
 private Function getMethodFromImpl(MethodCall mc) {
   exists(Impl impl |
-    IsInstantiationOf<MethodCall, IsInstantiationOfInput>::isInstantiationOf(mc, impl, _) and
-    result = getMethodSuccessor(impl, mc.getMethodName())
+    IsInstantiationOf<MethodCall, IsInstantiationOfInput>::isInstantiationOf(mc, impl, _)
   |
-    not methodResolutionDependsOnArgument(impl, _, _, _, _)
+    not methodResolutionDependsOnArgument(impl, _, _, _, _, _) and
+    result = getMethodSuccessor(impl, mc.getMethodName())
     or
     exists(int pos, TypePath path, Type type |
-      methodResolutionDependsOnArgument(impl, mc.getMethodName(), pos, path, type) and
+      methodResolutionDependsOnArgument(impl, result, mc.getMethodName(), pos, path, type) and
       inferType(mc.getArgument(pos), path) = type
     )
   )

@paldepind paldepind force-pushed the rust/impl-parameter-resolution branch 2 times, most recently from 0e2ff25 to 1d55771 Compare June 13, 2025 13:02
@paldepind paldepind force-pushed the rust/impl-parameter-resolution branch from 1d55771 to ef15df3 Compare June 13, 2025 13:44
@paldepind
Copy link
Contributor Author

I've applied the suggestions and rebased.

@paldepind paldepind merged commit 0a6d884 into github:main Jun 13, 2025
17 checks passed
@paldepind paldepind deleted the rust/impl-parameter-resolution branch June 13, 2025 14:14
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