Skip to content

Commit ef15df3

Browse files
committed
Rust: Apply review suggestions
1 parent eacf034 commit ef15df3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,10 @@ private predicate implSiblingCandidate(
12411241
Impl impl, TraitItemNode trait, Type rootType, TypeMention selfTy
12421242
) {
12431243
trait = impl.(ImplItemNode).resolveTraitTy() and
1244+
// If `impl` has an expansion from a macro attribute, then it's been
1245+
// superseded by the output of the expansion (and usually the expansion
1246+
// contains the same `impl` block so considering both would give spurious
1247+
// siblings).
12441248
not exists(impl.getAttributeMacroExpansion()) and
12451249
// We use this for resolving methods, so exclude traits that do not have methods.
12461250
exists(Function f | f = trait.getASuccessor(_) and f.getParamList().hasSelfParam()) and
@@ -1300,12 +1304,13 @@ private predicate methodTypeAtPath(Function f, int pos, TypePath path, Type type
13001304
}
13011305

13021306
/**
1303-
* Holds if resolving the method in `impl` with the name `methodName` requires
1304-
* inspecting the types of applied _arguments_ in order to determine whether it
1305-
* is the correct resolution.
1307+
* Holds if resolving the method `f` in `impl` with the name `methodName`
1308+
* requires inspecting the types of applied _arguments_ in order to determine
1309+
* whether it is the correct resolution.
13061310
*/
1311+
pragma[nomagic]
13071312
private predicate methodResolutionDependsOnArgument(
1308-
Impl impl, string methodName, int pos, TypePath path, Type type
1313+
Impl impl, string methodName, Function f, int pos, TypePath path, Type type
13091314
) {
13101315
/*
13111316
* As seen in the example below, when an implementation has a sibling for a
@@ -1321,8 +1326,8 @@ private predicate methodResolutionDependsOnArgument(
13211326
* // ^ `path` = "T"
13221327
* }
13231328
* impl MyAdd<i64> for i64 {
1324-
* fn method(&self, value: i64) -> Self { ... }
1325-
* // ^^^ `type` = i64
1329+
* fn method(&self, value: Foo<i64>) -> Self { ... }
1330+
* // ^^^ `type` = i64
13261331
* }
13271332
* ```
13281333
*
@@ -1335,7 +1340,8 @@ private predicate methodResolutionDependsOnArgument(
13351340
exists(TraitItemNode trait |
13361341
implHasSibling(impl, trait) and
13371342
traitTypeParameterOccurrence(trait, methodName, pos, path) and
1338-
methodTypeAtPath(getMethodSuccessor(impl, methodName), pos, path, type)
1343+
methodTypeAtPath(getMethodSuccessor(impl, methodName), pos, path, type) and
1344+
f = getMethodSuccessor(impl, methodName)
13391345
)
13401346
}
13411347

@@ -1345,10 +1351,11 @@ private Function getMethodFromImpl(MethodCall mc) {
13451351
IsInstantiationOf<MethodCall, IsInstantiationOfInput>::isInstantiationOf(mc, impl, _) and
13461352
result = getMethodSuccessor(impl, mc.getMethodName())
13471353
|
1348-
not methodResolutionDependsOnArgument(impl, _, _, _, _)
1354+
not methodResolutionDependsOnArgument(impl, _, _, _, _, _) and
1355+
result = getMethodSuccessor(impl, mc.getMethodName())
13491356
or
13501357
exists(int pos, TypePath path, Type type |
1351-
methodResolutionDependsOnArgument(impl, mc.getMethodName(), pos, path, type) and
1358+
methodResolutionDependsOnArgument(impl, mc.getMethodName(), result, pos, path, type) and
13521359
inferType(mc.getArgument(pos), path) = type
13531360
)
13541361
)

0 commit comments

Comments
 (0)