Skip to content

Commit 0efa72e

Browse files
committed
Rust: Minor tweaks
1 parent e71b3d7 commit 0efa72e

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,12 +2154,13 @@ private predicate methodCallHasImplCandidate(MethodCall mc, Impl impl) {
21542154

21552155
private module BlanketImplementation {
21562156
/**
2157-
* Holds if `impl` is a blanket implementation, that is, an implementation of a
2158-
* trait for a type parameter.
2157+
* Gets the type parameter for which `impl` is a blanket implementation, if
2158+
* any.
21592159
*/
21602160
private TypeParamItemNode getBlanketImplementationTypeParam(Impl impl) {
21612161
result = impl.(ImplItemNode).resolveSelfTy() and
21622162
result = impl.getGenericParamList().getAGenericParam() and
2163+
// This impl block is not superseded by the expansion of an attribute macro.
21632164
not exists(impl.getAttributeMacroExpansion())
21642165
}
21652166

@@ -2173,8 +2174,17 @@ private module BlanketImplementation {
21732174
}
21742175

21752176
/**
2176-
* Holds if `impl1` and `impl2` are duplicates and `impl2` is more "canonical"
2177-
* than `impl1`.
2177+
* Holds if `impl1` and `impl2` are duplicates and `impl2` is strictly more
2178+
* "canonical" than `impl1`.
2179+
*
2180+
* Libraries can often occur several times in the database for different
2181+
* library versions. This causes the same blanket implementations to exist
2182+
* multiple times, and these add no useful information.
2183+
*
2184+
* We detect these duplicates based on some simple heuristics (same trait
2185+
* name, file name, etc.). For these duplicates we select the one with the
2186+
* greatest file name (which usually is also the one with the greatest library
2187+
* version in the path)
21782188
*/
21792189
predicate duplicatedImpl(Impl impl1, Impl impl2) {
21802190
exists(string fileName, string traitName, int arity, string tpName |
@@ -2185,23 +2195,10 @@ private module BlanketImplementation {
21852195
)
21862196
}
21872197

2188-
predicate hasNoDuplicates(Impl impl) {
2198+
predicate isCanonicalImpl(Impl impl) {
21892199
not duplicatedImpl(impl, _) and isBlanketImplementation(impl)
21902200
}
21912201

2192-
/**
2193-
* We currently consider blanket implementations to be in scope "globally",
2194-
* even though they actually need to be imported to be used. One downside of
2195-
* this is that the libraries included in the database can often occur several
2196-
* times for different library versions. This causes the same blanket
2197-
* implementations to exist multiple times, and these add no useful
2198-
* information.
2199-
*
2200-
* We detect these duplicates based on some files heuristic (same trait name,
2201-
* file name, etc.). For these duplicates we select the one with the greatest
2202-
* file name (which usually is also the one with the greatest library version
2203-
* in the path)
2204-
*/
22052202
Impl getCanonicalImpl(Impl impl) {
22062203
result =
22072204
max(Impl impl0, Location l |
@@ -2210,7 +2207,7 @@ private module BlanketImplementation {
22102207
impl0 order by l.getFile().getAbsolutePath(), l.getStartLine()
22112208
)
22122209
or
2213-
hasNoDuplicates(impl) and result = impl
2210+
isCanonicalImpl(impl) and result = impl
22142211
}
22152212

22162213
predicate isCanonicalBlanketImplementation(Impl impl) { impl = getCanonicalImpl(impl) }
@@ -2223,11 +2220,10 @@ private module BlanketImplementation {
22232220
t =
22242221
min(Trait trait, int i |
22252222
trait = getBlanketImplementationTypeParam(impl).resolveBound(i) and
2226-
// Exclude traits that are "trivial" in the sense that they are known to
2227-
// not narrow things down very much.
2223+
// Exclude traits that are known to not narrow things down very much.
22282224
not trait.getName().getText() =
22292225
[
2230-
"Sized", "Clone", "Fn", "FnOnce", "FnMut",
2226+
"Sized", "Clone",
22312227
// The auto traits
22322228
"Send", "Sync", "Unpin", "UnwindSafe", "RefUnwindSafe"
22332229
]
@@ -2257,7 +2253,7 @@ private module BlanketImplementation {
22572253
f = impl.resolveTraitTy().getAssocItem(name)
22582254
) and
22592255
// If the method is already available through one of the trait bounds on the
2260-
// type parameter (because they share a common trait ancestor) then ignore
2256+
// type parameter (because they share a common ancestor trait) then ignore
22612257
// it.
22622258
not getBlanketImplementationTypeParam(impl).resolveABound().(TraitItemNode).getASuccessor(name) =
22632259
f

0 commit comments

Comments
 (0)