Skip to content

Commit bf42971

Browse files
committed
Rust: Unify type inference logic for associated functions
1 parent d5bafbc commit bf42971

File tree

12 files changed

+670
-49909
lines changed

12 files changed

+670
-49909
lines changed

rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ module Impl {
3333
result = "impl " + trait + this.getSelfTy().toAbbreviatedString() + " { ... }"
3434
)
3535
}
36+
37+
/**
38+
* Holds if this is an inherent `impl` block, that is, one that does not implement a trait.
39+
*/
40+
pragma[nomagic]
41+
predicate isInherent() { not this.hasTrait() }
3642
}
3743
}

rust/ql/lib/codeql/rust/internal/typeinference/BlanketImplementation.qll

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ private predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Trait trait
4141
*/
4242
pragma[nomagic]
4343
predicate isBlanketLike(ImplItemNode i, TypePath blanketSelfPath, TypeParam blanketTypeParam) {
44-
blanketTypeParam = i.getBlanketImplementationTypeParam() and
45-
blanketSelfPath.isEmpty()
46-
or
47-
exists(TypeMention tm, Type root, TypeParameter tp |
48-
tm = i.(Impl).getSelfTy() and
49-
complexSelfRoot(root, tp) and
50-
tm.getType() = root and
51-
tm.getTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
52-
blanketSelfPath = TypePath::singleton(tp) and
53-
hasFirstNonTrivialTraitBound(blanketTypeParam, _)
44+
i.(Impl).hasTrait() and
45+
(
46+
blanketTypeParam = i.getBlanketImplementationTypeParam() and
47+
blanketSelfPath.isEmpty()
48+
or
49+
exists(TypeMention tm, Type root, TypeParameter tp |
50+
tm = i.(Impl).getSelfTy() and
51+
complexSelfRoot(root, tp) and
52+
tm.getType() = root and
53+
tm.getTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
54+
blanketSelfPath = TypePath::singleton(tp) and
55+
hasFirstNonTrivialTraitBound(blanketTypeParam, _)
56+
)
5457
)
5558
}
5659

rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ class FunctionPosition extends TFunctionPosition {
2929

3030
predicate isReturn() { this = TReturnFunctionPosition() }
3131

32+
/** Gets the corresponding position when function call syntax is used. */
33+
FunctionPosition getFunctionCallAdjusted() {
34+
(this.isReturn() or this.isTypeQualifier()) and
35+
result = this
36+
or
37+
this.isSelf() and result.asPosition() = 0
38+
or
39+
result.asPosition() = this.asPosition() + 1
40+
}
41+
3242
/** Gets the corresponding position when `f` is invoked via a function call. */
3343
bindingset[f]
3444
FunctionPosition getFunctionCallAdjusted(Function f) {
35-
this.isReturn() and
36-
result = this
37-
or
38-
if f.hasSelfParam()
39-
then
40-
this.isSelf() and result.asPosition() = 0
41-
or
42-
result.asPosition() = this.asPosition() + 1
43-
else result = this
45+
if f.hasSelfParam() then result = this.getFunctionCallAdjusted() else result = this
4446
}
4547

4648
TypeMention getTypeMention(Function f) {
@@ -197,7 +199,7 @@ class AssocFunctionType extends MkAssocFunctionType {
197199
exists(Function f, ImplOrTraitItemNode i, FunctionPosition pos | this.appliesTo(f, i, pos) |
198200
result = pos.getTypeMention(f)
199201
or
200-
pos.isSelf() and
202+
pos.isSelfOrTypeQualifier() and
201203
not f.hasSelfParam() and
202204
result = [i.(Impl).getSelfTy().(AstNode), i.(Trait).getName()]
203205
)
@@ -209,7 +211,7 @@ class AssocFunctionType extends MkAssocFunctionType {
209211
}
210212

211213
pragma[nomagic]
212-
private Trait getALookupTrait(Type t) {
214+
Trait getALookupTrait(Type t) {
213215
result = t.(TypeParamTypeParameter).getTypeParam().(TypeParamItemNode).resolveABound()
214216
or
215217
result = t.(SelfTypeParameter).getTrait()

0 commit comments

Comments
 (0)