Skip to content

Commit cf3c41f

Browse files
committed
wip
1 parent b9de84f commit cf3c41f

File tree

10 files changed

+130
-109
lines changed

10 files changed

+130
-109
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ predicate traitTypeParameterOccurrence(
8686
TypeParameter traitTp
8787
) {
8888
f = trait.getAssocItem(functionName) and
89-
traitTp = getAssocFunctionTypeAt(f, trait, pos, path) and
89+
traitTp = getAssocFunctionTypeInclNonMethodSelfAt(f, trait, pos, path) and
9090
traitTp = trait.(TraitTypeAbstraction).getATypeParameter()
9191
}
9292

@@ -141,7 +141,7 @@ pragma[nomagic]
141141
private Type getAssocFunctionNonTypeParameterTypeAt(
142142
ImplItemNode impl, Function f, FunctionPosition pos, TypePath path
143143
) {
144-
result = getAssocFunctionTypeAt(f, impl, pos, path) and
144+
result = getAssocFunctionTypeInclNonMethodSelfAt(f, impl, pos, path) and
145145
not result instanceof TypeParameter
146146
}
147147

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ private newtype TAssocFunctionType =
8282
// through `i`. This ensures that `parent` is either a supertrait of `i` or
8383
// `i` in an `impl` block implementing `parent`.
8484
(parent = i or BaseTypes::rootTypesSatisfaction(_, TTrait(parent), i, _, _)) and
85-
exists(pos.getTypeMention(f))
85+
// We always include the `self` position, even for non-methods, where it is used
86+
// to match type qualifiers against the `impl` or trait type, such as in `Vec::new`.
87+
(exists(pos.getTypeMention(f)) or pos.isSelf())
8688
}
8789

8890
bindingset[abs, constraint, tp]
@@ -116,6 +118,22 @@ Type getAssocFunctionTypeAt(Function f, ImplOrTraitItemNode i, FunctionPosition
116118
)
117119
}
118120

121+
/**
122+
* Same as `getAssocFunctionTypeAt`, but also includes types at the `self` position
123+
* for non-methods.
124+
*/
125+
pragma[nomagic]
126+
Type getAssocFunctionTypeInclNonMethodSelfAt(
127+
Function f, ImplOrTraitItemNode i, FunctionPosition pos, TypePath path
128+
) {
129+
result = getAssocFunctionTypeAt(f, i, pos, path)
130+
or
131+
f = i.getASuccessor(_) and
132+
not f.hasSelfParam() and
133+
pos.isSelf() and
134+
result = resolveImplOrTraitType(i, path)
135+
}
136+
119137
/**
120138
* The type of an associated function at a given position, when its implicit
121139
* `Self` type parameter is specialized to a given trait or `impl` block.
@@ -174,7 +192,7 @@ class AssocFunctionType extends MkAssocFunctionType {
174192
Type getTypeAt(TypePath path) {
175193
exists(Function f, FunctionPosition pos, ImplOrTraitItemNode i, Type t |
176194
this.appliesTo(f, i, pos) and
177-
t = getAssocFunctionTypeAt(f, i, pos, path)
195+
t = getAssocFunctionTypeInclNonMethodSelfAt(f, i, pos, path)
178196
|
179197
not t instanceof SelfTypeParameter and
180198
result = t
@@ -184,9 +202,12 @@ class AssocFunctionType extends MkAssocFunctionType {
184202
}
185203

186204
private TypeMention getTypeMention() {
187-
exists(Function f, FunctionPosition pos |
188-
this.appliesTo(f, _, pos) and
205+
exists(Function f, ImplOrTraitItemNode i, FunctionPosition pos | this.appliesTo(f, i, pos) |
189206
result = pos.getTypeMention(f)
207+
or
208+
pos.isSelf() and
209+
not f.hasSelfParam() and
210+
result = [i.(Impl).getSelfTy().(TypeMention), i.(TypeMention)]
190211
)
191212
}
192213

@@ -294,8 +315,9 @@ module ArgIsInstantiationOf<
294315
*/
295316
signature module ArgsAreInstantiationsOfInputSig {
296317
/**
297-
* Holds if `f` implements a trait function with type parameter `traitTp`, where
298-
* we need to check that the type of `f` for `traitTp` is satisfied.
318+
* Holds if `f` implements (or is itself) a trait function with type parameter
319+
* `traitTp`, where we need to check that the type of `f` for `traitTp` is
320+
* satisfied.
299321
*
300322
* `pos` is one of the positions in `f` in which the relevant type occours.
301323
*/

0 commit comments

Comments
 (0)