Skip to content

Commit 0e2ff25

Browse files
committed
Rust: Apply suggestions review
1 parent 570be12 commit 0e2ff25

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
@@ -1237,6 +1237,10 @@ private predicate implSiblingCandidate(
12371237
Impl impl, TraitItemNode trait, Type rootType, TypeMention selfTy
12381238
) {
12391239
trait = impl.(ImplItemNode).resolveTraitTy() and
1240+
// If `impl` has an expansion from a macro attribute, then it's been
1241+
// superseded by the output of the expansion (and usually the expansion
1242+
// contains the same `impl` block so considering both would give spurious
1243+
// siblings).
12401244
not exists(impl.getAttributeMacroExpansion()) and
12411245
// We use this for resolving methods, so exclude traits that do not have methods.
12421246
exists(Function f | f = trait.getASuccessor(_) and f.getParamList().hasSelfParam()) and
@@ -1296,12 +1300,13 @@ private predicate methodTypeAtPath(Function f, int pos, TypePath path, Type type
12961300
}
12971301

12981302
/**
1299-
* Holds if resolving the method in `impl` with the name `methodName` requires
1300-
* inspecting the types of applied _arguments_ in order to determine whether it
1301-
* is the correct resolution.
1303+
* Holds if resolving the method `f` in `impl` with the name `methodName`
1304+
* requires inspecting the types of applied _arguments_ in order to determine
1305+
* whether it is the correct resolution.
13021306
*/
1307+
pragma[nomagic]
13031308
private predicate methodResolutionDependsOnArgument(
1304-
Impl impl, string methodName, int pos, TypePath path, Type type
1309+
Impl impl, string methodName, Function f, int pos, TypePath path, Type type
13051310
) {
13061311
/*
13071312
* As seen in the example below, when an implementation has a sibling for a
@@ -1317,8 +1322,8 @@ private predicate methodResolutionDependsOnArgument(
13171322
* // ^ `path` = "T"
13181323
* }
13191324
* impl MyAdd<i64> for i64 {
1320-
* fn method(&self, value: i64) -> Self { ... }
1321-
* // ^^^ `type` = i64
1325+
* fn method(&self, value: Foo<i64>) -> Self { ... }
1326+
* // ^^^ `type` = i64
13221327
* }
13231328
* ```
13241329
*
@@ -1331,7 +1336,8 @@ private predicate methodResolutionDependsOnArgument(
13311336
exists(TraitItemNode trait |
13321337
implHasSibling(impl, trait) and
13331338
traitTypeParameterOccurrence(trait, methodName, pos, path) and
1334-
methodTypeAtPath(getMethodSuccessor(impl, methodName), pos, path, type)
1339+
methodTypeAtPath(getMethodSuccessor(impl, methodName), pos, path, type) and
1340+
f = getMethodSuccessor(impl, methodName)
13351341
)
13361342
}
13371343

@@ -1341,10 +1347,11 @@ private Function getMethodFromImpl(MethodCall mc) {
13411347
IsInstantiationOf<MethodCall, IsInstantiationOfInput>::isInstantiationOf(mc, impl, _) and
13421348
result = getMethodSuccessor(impl, mc.getMethodName())
13431349
|
1344-
not methodResolutionDependsOnArgument(impl, _, _, _, _)
1350+
not methodResolutionDependsOnArgument(impl, _, _, _, _, _) and
1351+
result = getMethodSuccessor(impl, mc.getMethodName())
13451352
or
13461353
exists(int pos, TypePath path, Type type |
1347-
methodResolutionDependsOnArgument(impl, mc.getMethodName(), pos, path, type) and
1354+
methodResolutionDependsOnArgument(impl, mc.getMethodName(), result, pos, path, type) and
13481355
inferType(mc.getArgument(pos), path) = type
13491356
)
13501357
)

0 commit comments

Comments
 (0)