Skip to content

Commit 2a3d2b8

Browse files
committed
Merge branch 'main' into redsun82/update-kotlin-2.3.20
2 parents 4e6d94b + c66679c commit 2a3d2b8

File tree

8 files changed

+271
-45
lines changed

8 files changed

+271
-45
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module Impl {
3232
* Gets the `index`th type bound of this type parameter, if any.
3333
*
3434
* This includes type bounds directly on this type parameter and bounds from
35-
* any `where` clauses for this type parameter.
35+
* any `where` clauses for this type parameter, but restricted to `where`
36+
* clauses from the item that declares this type parameter.
3637
*/
3738
TypeBound getTypeBound(int index) {
3839
result =
@@ -43,13 +44,36 @@ module Impl {
4344
* Gets a type bound of this type parameter.
4445
*
4546
* This includes type bounds directly on this type parameter and bounds from
46-
* any `where` clauses for this type parameter.
47+
* any `where` clauses for this type parameter, but restricted to `where`
48+
* clauses from the item that declares this type parameter.
4749
*/
4850
TypeBound getATypeBound() { result = this.getTypeBound(_) }
4951

5052
/** Holds if this type parameter has at least one type bound. */
5153
predicate hasTypeBound() { exists(this.getATypeBound()) }
5254

55+
/**
56+
* Gets the `index`th additional type bound of this type parameter,
57+
* which applies to `constrainingItem`, if any.
58+
*
59+
* For example, in
60+
*
61+
* ```rust
62+
* impl<T> SomeType<T> where T: Clone {
63+
* fn foo() where T: Debug { }
64+
* }
65+
* ```
66+
*
67+
* The constraint `Debug` additionally applies to `T` in `foo`.
68+
*/
69+
TypeBound getAdditionalTypeBound(Item constrainingItem, int index) {
70+
result =
71+
rank[index + 1](int i, int j |
72+
|
73+
this.(TypeParamItemNode).getAdditionalTypeBoundAt(constrainingItem, i, j) order by i, j
74+
)
75+
}
76+
5377
override string toAbbreviatedString() { result = this.getName().getText() }
5478

5579
override string toStringImpl() { result = this.getName().getText() }

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,21 +1162,39 @@ private Path getWherePredPath(WherePred wp) { result = wp.getTypeRepr().(PathTyp
11621162
final class TypeParamItemNode extends NamedItemNode, TypeItemNode instanceof TypeParam {
11631163
/** Gets a where predicate for this type parameter, if any */
11641164
pragma[nomagic]
1165-
private WherePred getAWherePred() {
1165+
private WherePred getAWherePred(ItemNode constrainingItem, boolean isAdditional) {
11661166
exists(ItemNode declaringItem |
1167+
this = declaringItem.getTypeParam(_) and
11671168
this = resolvePath(getWherePredPath(result)) and
1168-
result = declaringItem.getADescendant() and
1169-
this = declaringItem.getADescendant()
1169+
result = constrainingItem.getADescendant()
1170+
|
1171+
constrainingItem = declaringItem and
1172+
isAdditional = false
1173+
or
1174+
constrainingItem = declaringItem.getADescendant() and
1175+
isAdditional = true
11701176
)
11711177
}
11721178

11731179
pragma[nomagic]
11741180
TypeBound getTypeBoundAt(int i, int j) {
11751181
exists(TypeBoundList tbl | result = tbl.getBound(j) |
1176-
tbl = super.getTypeBoundList() and i = 0
1182+
tbl = super.getTypeBoundList() and
1183+
i = 0
11771184
or
11781185
exists(WherePred wp |
1179-
wp = this.getAWherePred() and
1186+
wp = this.getAWherePred(_, false) and
1187+
tbl = wp.getTypeBoundList() and
1188+
wp = any(WhereClause wc).getPredicate(i)
1189+
)
1190+
)
1191+
}
1192+
1193+
pragma[nomagic]
1194+
TypeBound getAdditionalTypeBoundAt(Item constrainingItem, int i, int j) {
1195+
exists(TypeBoundList tbl | result = tbl.getBound(j) |
1196+
exists(WherePred wp |
1197+
wp = this.getAWherePred(constrainingItem, true) and
11801198
tbl = wp.getTypeBoundList() and
11811199
wp = any(WhereClause wc).getPredicate(i)
11821200
)
@@ -1197,6 +1215,15 @@ final class TypeParamItemNode extends NamedItemNode, TypeItemNode instanceof Typ
11971215

11981216
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
11991217

1218+
pragma[nomagic]
1219+
ItemNode resolveAdditionalBound(ItemNode constrainingItem) {
1220+
result =
1221+
resolvePath(this.getAdditionalTypeBoundAt(constrainingItem, _, _)
1222+
.getTypeRepr()
1223+
.(PathTypeRepr)
1224+
.getPath())
1225+
}
1226+
12001227
override string getName() { result = TypeParam.super.getName().getText() }
12011228

12021229
override Namespace getNamespace() { result.isType() }

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

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private newtype TAssocFunctionType =
8787
}
8888

8989
bindingset[abs, constraint, tp]
90+
pragma[inline_late]
9091
private Type getTraitConstraintTypeAt(
9192
TypeAbstraction abs, TypeMention constraint, TypeParameter tp, TypePath path
9293
) {
@@ -203,7 +204,7 @@ class AssocFunctionType extends MkAssocFunctionType {
203204
}
204205

205206
pragma[nomagic]
206-
Trait getALookupTrait(Type t) {
207+
private Trait getALookupTrait(Type t) {
207208
result = t.(TypeParamTypeParameter).getTypeParam().(TypeParamItemNode).resolveABound()
208209
or
209210
result = t.(SelfTypeParameter).getTrait()
@@ -213,23 +214,47 @@ Trait getALookupTrait(Type t) {
213214
result = t.(DynTraitType).getTrait()
214215
}
215216

216-
/**
217-
* Gets the type obtained by substituting in relevant traits in which to do function
218-
* lookup, or `t` itself when no such trait exist.
219-
*/
220217
pragma[nomagic]
221-
Type substituteLookupTraits(Type t) {
218+
private Trait getAdditionalLookupTrait(ItemNode i, Type t) {
219+
result =
220+
t.(TypeParamTypeParameter)
221+
.getTypeParam()
222+
.(TypeParamItemNode)
223+
.resolveAdditionalBound(i.getImmediateParent*())
224+
}
225+
226+
bindingset[n, t]
227+
pragma[inline_late]
228+
Trait getALookupTrait(AstNode n, Type t) {
229+
result = getALookupTrait(t)
230+
or
231+
result = getAdditionalLookupTrait(any(ItemNode i | n = i.getADescendant()), t)
232+
}
233+
234+
bindingset[i, t]
235+
pragma[inline_late]
236+
private Type substituteLookupTraits0(ItemNode i, Type t) {
222237
not exists(getALookupTrait(t)) and
238+
not exists(getAdditionalLookupTrait(i, t)) and
223239
result = t
224240
or
225241
result = TTrait(getALookupTrait(t))
242+
or
243+
result = TTrait(getAdditionalLookupTrait(i, t))
226244
}
227245

228246
/**
229-
* Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order.
247+
* Gets the type obtained by substituting in relevant traits in which to do function
248+
* lookup, or `t` itself when no such trait exists, in the context of AST node `n`.
230249
*/
250+
bindingset[n, t]
251+
pragma[inline_late]
252+
Type substituteLookupTraits(AstNode n, Type t) {
253+
result = substituteLookupTraits0(any(ItemNode i | n = i.getADescendant()), t)
254+
}
255+
231256
pragma[nomagic]
232-
Type getNthLookupType(Type t, int n) {
257+
private Type getNthLookupType(Type t, int n) {
233258
not exists(getALookupTrait(t)) and
234259
result = t and
235260
n = 0
@@ -244,24 +269,66 @@ Type getNthLookupType(Type t, int n) {
244269
}
245270

246271
/**
247-
* Gets the index of the last `substituteLookupTraits` type for `t`.
272+
* Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order,
273+
* in the context of AST node `node`.
248274
*/
275+
bindingset[node, t]
276+
pragma[inline_late]
277+
Type getNthLookupType(AstNode node, Type t, int n) {
278+
exists(ItemNode i | node = i.getADescendant() |
279+
if exists(getAdditionalLookupTrait(i, t))
280+
then
281+
result =
282+
TTrait(rank[n + 1](Trait trait, int j |
283+
trait = [getALookupTrait(t), getAdditionalLookupTrait(i, t)] and
284+
j = idOfTypeParameterAstNode(trait)
285+
|
286+
trait order by j
287+
))
288+
else result = getNthLookupType(t, n)
289+
)
290+
}
291+
249292
pragma[nomagic]
250-
int getLastLookupTypeIndex(Type t) { result = max(int n | exists(getNthLookupType(t, n))) }
293+
private int getLastLookupTypeIndex(Type t) { result = max(int n | exists(getNthLookupType(t, n))) }
294+
295+
/**
296+
* Gets the index of the last `substituteLookupTraits` type for `t`,
297+
* in the context of AST node `node`.
298+
*/
299+
bindingset[node, t]
300+
pragma[inline_late]
301+
int getLastLookupTypeIndex(AstNode node, Type t) {
302+
if exists(getAdditionalLookupTrait(node, t))
303+
then result = max(int n | exists(getNthLookupType(node, t, n)))
304+
else result = getLastLookupTypeIndex(t)
305+
}
306+
307+
signature class ArgSig {
308+
/** Gets the type of this argument at `path`. */
309+
Type getTypeAt(TypePath path);
310+
311+
/** Gets the enclosing item node of this argument. */
312+
ItemNode getEnclosingItemNode();
313+
314+
/** Gets a textual representation of this argument. */
315+
string toString();
316+
317+
/** Gets the location of this argument. */
318+
Location getLocation();
319+
}
251320

252321
/**
253322
* A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
254323
* traits when checking whether argument types are instantiations of function
255324
* types.
256325
*/
257-
module ArgIsInstantiationOf<
258-
HasTypeTreeSig Arg, IsInstantiationOfInputSig<Arg, AssocFunctionType> Input>
259-
{
326+
module ArgIsInstantiationOf<ArgSig Arg, IsInstantiationOfInputSig<Arg, AssocFunctionType> Input> {
260327
final private class ArgFinal = Arg;
261328

262329
private class ArgSubst extends ArgFinal {
263330
Type getTypeAt(TypePath path) {
264-
result = substituteLookupTraits(super.getTypeAt(path)) and
331+
result = substituteLookupTraits0(this.getEnclosingItemNode(), super.getTypeAt(path)) and
265332
not result = TNeverType() and
266333
not result = TUnknownType()
267334
}
@@ -318,6 +385,8 @@ signature module ArgsAreInstantiationsOfInputSig {
318385

319386
Location getLocation();
320387

388+
ItemNode getEnclosingItemNode();
389+
321390
Type getArgType(FunctionPosition pos, TypePath path);
322391

323392
predicate hasTargetCand(ImplOrTraitItemNode i, Function f);
@@ -366,6 +435,8 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
366435

367436
FunctionPosition getPos() { result = pos }
368437

438+
ItemNode getEnclosingItemNode() { result = call.getEnclosingItemNode() }
439+
369440
Location getLocation() { result = call.getLocation() }
370441

371442
Type getTypeAt(TypePath path) { result = call.getArgType(pos, path) }

0 commit comments

Comments
 (0)