Skip to content

Commit 87c6e84

Browse files
committed
dbg
1 parent 0ce3a9a commit 87c6e84

File tree

8 files changed

+125
-114
lines changed

8 files changed

+125
-114
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Trait traitBound) {
2222
// Exclude traits that are known to not narrow things down very much.
2323
not trait.getName().getText() =
2424
[
25-
"Sized", "Clone",
25+
"Sized", "Clone", "Destruct",
2626
// The auto traits
2727
"Send", "Sync", "Unpin", "UnwindSafe", "RefUnwindSafe"
2828
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ private predicate hasEquivalentPositionalSibling(
212212
*/
213213
pragma[nomagic]
214214
predicate functionResolutionDependsOnArgument(
215-
ImplItemNode impl, Function f, TypeParameter traitTp, FunctionPosition pos
215+
ImplItemNode impl, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path
216216
) {
217217
exists(string functionName |
218-
functionResolutionDependsOnArgumentCand(impl, f, functionName, traitTp, pos, _)
218+
functionResolutionDependsOnArgumentCand(impl, f, functionName, traitTp, pos, path)
219219
|
220220
if functionResolutionDependsOnPositionalArgumentCand(impl, f, functionName, traitTp)
221221
then any()

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

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ signature module ArgsAreInstantiationsOfInputSig {
375375
*
376376
* `pos` is one of the positions in `f` in which the relevant type occours.
377377
*/
378-
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos);
378+
predicate toCheck(
379+
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path
380+
);
379381

380382
/** A call whose argument types are to be checked. */
381383
class Call {
@@ -399,10 +401,10 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
399401
private predicate toCheckRanked(
400402
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, int rnk
401403
) {
402-
Input::toCheck(i, f, traitTp, pos) and
404+
Input::toCheck(i, f, traitTp, pos, _) and
403405
traitTp =
404406
rank[rnk + 1](TypeParameter traitTp0, int j |
405-
Input::toCheck(i, f, traitTp0, _) and
407+
Input::toCheck(i, f, traitTp0, _, _) and
406408
j = getTypeParameterId(traitTp0)
407409
|
408410
traitTp0 order by j
@@ -411,42 +413,56 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
411413

412414
pragma[nomagic]
413415
private predicate toCheck(
414-
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos,
416+
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path,
415417
AssocFunctionType t
416418
) {
417-
Input::toCheck(i, f, traitTp, pos) and
419+
Input::toCheck(i, f, traitTp, pos, path) and
418420
t.appliesTo(f, i, pos)
419421
}
420422

421423
private newtype TCallAndPos =
422-
MkCallAndPos(Input::Call call, FunctionPosition pos) { exists(call.getArgType(pos, _)) }
424+
MkCallAndPos(Input::Call call, ImplOrTraitItemNode i, Function f, FunctionPosition pos) {
425+
call.hasTargetCand(i, f) and
426+
toCheck(i, f, _, pos, _, _)
427+
}
423428

424429
/** A call tagged with a position. */
425430
private class CallAndPos extends MkCallAndPos {
426431
Input::Call call;
432+
ImplOrTraitItemNode i;
433+
Function f;
427434
FunctionPosition pos;
428435

429-
CallAndPos() { this = MkCallAndPos(call, pos) }
436+
CallAndPos() { this = MkCallAndPos(call, i, f, pos) }
430437

431438
Input::Call getCall() { result = call }
432439

433-
FunctionPosition getPos() { result = pos }
434-
435440
Location getLocation() { result = call.getLocation() }
436441

437-
Type getTypeAt(TypePath path) { result = call.getArgType(pos, path) }
442+
Type getTypeAt(TypePath path) {
443+
result = call.getArgType(pos, path)
444+
or
445+
exists(
446+
TypeParameter tp, FunctionPosition pos0, TypePath path0, TypePath path1, TypePath path2
447+
|
448+
Input::toCheck(i, f, tp, pos0, path0) and
449+
Input::toCheck(i, f, tp, pos, path1) and
450+
pos0 != pos and
451+
result = call.getArgType(pos0, path0.appendInverse(path2)) and
452+
path = path1.append(path2)
453+
)
454+
}
438455

439-
string toString() { result = call.toString() + " [arg " + pos + "]" }
456+
string toString() { result = call.toString() }
440457
}
441458

442459
pragma[nomagic]
443460
private predicate potentialInstantiationOf0(
444-
CallAndPos cp, Input::Call call, TypeParameter traitTp, FunctionPosition pos, Function f,
445-
TypeAbstraction abs, AssocFunctionType constraint
461+
CallAndPos cp, Input::Call call, TypeParameter traitTp, FunctionPosition pos, TypePath path,
462+
Function f, TypeAbstraction abs, AssocFunctionType constraint
446463
) {
447-
cp = MkCallAndPos(call, pragma[only_bind_into](pos)) and
448-
call.hasTargetCand(abs, f) and
449-
toCheck(abs, f, traitTp, pragma[only_bind_into](pos), constraint)
464+
cp = MkCallAndPos(call, abs, f, pos) and
465+
toCheck(abs, f, traitTp, pos, path, constraint)
450466
}
451467

452468
private module ArgIsInstantiationOfToIndexInput implements
@@ -457,7 +473,7 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
457473
CallAndPos cp, TypeAbstraction abs, AssocFunctionType constraint
458474
) {
459475
exists(Input::Call call, TypeParameter traitTp, FunctionPosition pos, int rnk, Function f |
460-
potentialInstantiationOf0(cp, call, traitTp, pos, f, abs, constraint) and
476+
potentialInstantiationOf0(cp, call, traitTp, pos, _, f, abs, constraint) and
461477
toCheckRanked(abs, f, traitTp, pos, rnk)
462478
|
463479
rnk = 0
@@ -466,7 +482,9 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
466482
)
467483
}
468484

469-
predicate relevantConstraint(AssocFunctionType constraint) { toCheck(_, _, _, _, constraint) }
485+
predicate relevantConstraint(AssocFunctionType constraint) {
486+
toCheck(_, _, _, _, _, constraint)
487+
}
470488
}
471489

472490
private module ArgIsInstantiationOfToIndex =
@@ -477,7 +495,7 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
477495
Input::Call call, ImplOrTraitItemNode i, Function f, int rnk
478496
) {
479497
exists(FunctionPosition pos |
480-
ArgIsInstantiationOfToIndex::argIsInstantiationOf(MkCallAndPos(call, pos), i, _) and
498+
ArgIsInstantiationOfToIndex::argIsInstantiationOf(MkCallAndPos(call, i, f, pos), i, _) and
481499
call.hasTargetCand(i, f) and
482500
toCheckRanked(i, f, _, pos, rnk)
483501
|
@@ -506,20 +524,20 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
506524
predicate potentialInstantiationOf(
507525
CallAndPos cp, TypeAbstraction abs, AssocFunctionType constraint
508526
) {
509-
potentialInstantiationOf0(cp, _, _, _, _, abs, constraint)
527+
potentialInstantiationOf0(cp, _, _, _, _, _, abs, constraint)
510528
}
511529

512-
predicate relevantConstraint(AssocFunctionType constraint) { toCheck(_, _, _, _, constraint) }
530+
predicate relevantConstraint(AssocFunctionType constraint) {
531+
toCheck(_, _, _, _, _, constraint)
532+
}
513533
}
514534

515535
private module ArgsAreNotInstantiationOf =
516536
ArgIsInstantiationOf<CallAndPos, ArgsAreNotInstantiationOfInput>;
517537

518538
pragma[nomagic]
519-
private predicate argsAreNotInstantiationsOf0(
520-
Input::Call call, FunctionPosition pos, ImplOrTraitItemNode i
521-
) {
522-
ArgsAreNotInstantiationOf::argIsNotInstantiationOf(MkCallAndPos(call, pos), i, _, _)
539+
private predicate argsAreNotInstantiationsOf0(Input::Call call, ImplOrTraitItemNode i, Function f) {
540+
ArgsAreNotInstantiationOf::argIsNotInstantiationOf(MkCallAndPos(call, i, f, _), i, _, _)
523541
}
524542

525543
/**
@@ -528,10 +546,10 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
528546
*/
529547
pragma[nomagic]
530548
predicate argsAreNotInstantiationsOf(Input::Call call, ImplOrTraitItemNode i, Function f) {
531-
exists(FunctionPosition pos |
532-
argsAreNotInstantiationsOf0(call, pos, i) and
533-
call.hasTargetCand(i, f) and
534-
Input::toCheck(i, f, _, pos)
535-
)
549+
// exists(FunctionPosition pos |
550+
argsAreNotInstantiationsOf0(call, i, f) // and
551+
// call.hasTargetCand(i, f) and
552+
// Input::toCheck(i, f, _, pos, _)
553+
// )
536554
}
537555
}

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ private module MethodResolution {
21412141
pragma[nomagic]
21422142
Method resolveCallTarget(ImplOrTraitItemNode i) {
21432143
result = this.resolveCallTargetCand(i) and
2144-
not FunctionOverloading::functionResolutionDependsOnArgument(i, result, _, _)
2144+
not FunctionOverloading::functionResolutionDependsOnArgument(i, result, _, _, _)
21452145
or
21462146
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(this, i, result)
21472147
}
@@ -2377,11 +2377,14 @@ private module MethodResolution {
23772377
* types of parameters, when needed to disambiguate the call.
23782378
*/
23792379
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
2380-
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
2381-
FunctionOverloading::functionResolutionDependsOnArgument(i, f, traitTp, pos)
2380+
predicate toCheck(
2381+
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path
2382+
) {
2383+
FunctionOverloading::functionResolutionDependsOnArgument(i, f, traitTp, pos, path)
23822384
}
23832385

23842386
class Call extends MethodCallCand {
2387+
// Call() { mc_ = Debug::getRelevantLocatable() } // todo
23852388
Type getArgType(FunctionPosition pos, TypePath path) {
23862389
result = mc_.getArgumentTypeAt(pos.asArgumentPosition(), path)
23872390
or
@@ -2666,24 +2669,29 @@ private module NonMethodResolution {
26662669
(
26672670
traitTp = TSelfTypeParameter(trait)
26682671
or
2669-
FunctionOverloading::functionResolutionDependsOnArgument(impl, implFunction, traitTp, pos)
2672+
FunctionOverloading::functionResolutionDependsOnArgument(impl, implFunction, traitTp, pos, _)
26702673
)
26712674
}
26722675

26732676
pragma[nomagic]
26742677
private predicate functionResolutionDependsOnArgument(
2675-
ImplItemNode impl, NonMethodFunction f, FunctionPosition pos, TypeParameter traitTp
2678+
ImplItemNode impl, NonMethodFunction f, FunctionPosition pos, TypeParameter traitTp,
2679+
TypePath path
26762680
) {
2677-
functionResolutionDependsOnArgument0(_, _, pos, impl, f, _, traitTp)
2681+
functionResolutionDependsOnArgument0(_, _, pos, impl, f, path, traitTp)
26782682
or
26792683
f = impl.getASuccessor(_) and
26802684
not impl.(Impl).hasTrait() and
2681-
traitTp = TTypeParamTypeParameter(impl.resolveSelfTy().getTypeParam(0)) and
2685+
traitTp = TTypeParamTypeParameter(impl.resolveSelfTy().getTypeParam(_)) and
26822686
not f.hasSelfParam() and
26832687
(
2688+
path = TypePath::singleton(traitTp) and
26842689
pos.isSelf()
26852690
or
2686-
pos.getTypeMention(f) = any(ImplSelfMention self).getParentNode*()
2691+
exists(TypeParameter tp0 |
2692+
tp0 = resolveImplSelfTypeAt(impl, TypePath::singleton(traitTp)) and
2693+
pos.getTypeMention(f).getTypeAt(path) = tp0
2694+
)
26872695
)
26882696
}
26892697

@@ -2792,7 +2800,7 @@ private module NonMethodResolution {
27922800
result = this.getPathResolutionResolved() and
27932801
result = i.getASuccessor(_) and
27942802
not exists(this.resolveCallTargetViaPathResolution()) and
2795-
functionResolutionDependsOnArgument(i, result, _, _)
2803+
functionResolutionDependsOnArgument(i, result, _, _, _)
27962804
}
27972805

27982806
pragma[nomagic]
@@ -2840,7 +2848,7 @@ private module NonMethodResolution {
28402848
ItemNode resolveCallTargetViaPathResolution() {
28412849
not this.hasTrait() and
28422850
result = this.getPathResolutionResolved() and
2843-
not functionResolutionDependsOnArgument(_, result, _, _)
2851+
not functionResolutionDependsOnArgument(_, result, _, _, _)
28442852
}
28452853

28462854
/**
@@ -2849,7 +2857,7 @@ private module NonMethodResolution {
28492857
pragma[nomagic]
28502858
NonMethodFunction resolveCallTargetViaTypeInference(ImplOrTraitItemNode i) {
28512859
result = this.resolveCallTargetBlanketCand(i) and
2852-
not FunctionOverloading::functionResolutionDependsOnArgument(_, result, _, _)
2860+
not FunctionOverloading::functionResolutionDependsOnArgument(_, result, _, _, _)
28532861
or
28542862
NonMethodArgsAreInstantiationsOfBlanket::argsAreInstantiationsOf(this, i, result)
28552863
or
@@ -2938,8 +2946,10 @@ private module NonMethodResolution {
29382946
private module NonMethodArgsAreInstantiationsOfBlanketInput implements
29392947
ArgsAreInstantiationsOfInputSig
29402948
{
2941-
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
2942-
functionResolutionDependsOnArgument(i, f, pos, traitTp)
2949+
predicate toCheck(
2950+
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path
2951+
) {
2952+
functionResolutionDependsOnArgument(i, f, pos, traitTp, path)
29432953
}
29442954

29452955
final class Call extends NonMethodCall {
@@ -2959,11 +2969,13 @@ private module NonMethodResolution {
29592969
private module NonMethodArgsAreInstantiationsOfNonBlanketInput implements
29602970
ArgsAreInstantiationsOfInputSig
29612971
{
2962-
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
2963-
functionResolutionDependsOnArgument(i, f, pos, traitTp)
2972+
predicate toCheck(
2973+
ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path
2974+
) {
2975+
functionResolutionDependsOnArgument(i, f, pos, traitTp, path)
29642976
or
29652977
// Also match against the trait function itself
2966-
FunctionOverloading::traitTypeParameterOccurrence(i, f, _, pos, _, traitTp) and
2978+
FunctionOverloading::traitTypeParameterOccurrence(i, f, _, pos, path, traitTp) and
29672979
traitTp = TSelfTypeParameter(i)
29682980
}
29692981

@@ -2974,11 +2986,12 @@ private module NonMethodResolution {
29742986
}
29752987

29762988
// private predicate testhasTargetCand(
2977-
// ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos
2989+
// ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos,
2990+
// TypePath path
29782991
// ) {
29792992
// this = Debug::getRelevantLocatable() and
29802993
// this.hasTargetCand(i, f) and
2981-
// toCheck(i, f, traitTp, pos)
2994+
// toCheck(i, f, traitTp, pos, path)
29822995
// }
29832996
predicate hasTargetCand(ImplOrTraitItemNode i, Function f) {
29842997
f = this.resolveCallTargetNonBlanketCand(i)
@@ -4160,8 +4173,8 @@ private module Debug {
41604173
Locatable getRelevantLocatable() {
41614174
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
41624175
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
4163-
filepath.matches("%/overloading.rs") and
4164-
startline = 276
4176+
filepath.matches("%/main.rs") and
4177+
startline = 109
41654178
)
41664179
}
41674180

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ private module MkTypeMention<getAdditionalPathTypeAtSig/2 getAdditionalPathTypeA
403403
}
404404
}
405405

406+
pragma[nomagic]
407+
TypeMention getASelfMention(ImplItemNode impl) {
408+
result = impl.getAnItemInSelfScope().getADescendant() and
409+
forex(Type t, TypePath path | t = resolveImplSelfTypeAt(impl, path) |
410+
t = result.getTypeAt(path)
411+
)
412+
}
413+
406414
class PathTypeReprMention extends TypeMentionImpl, PathTypeRepr {
407415
private PathTypeMention path;
408416

rust/ql/test/library-tests/dataflow/sources/net/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
multipleResolvedTargets
22
| main.rs:2220:9:2220:31 | ... .my_add(...) |
3-
| main.rs:2222:9:2222:29 | ... .my_add(...) |
43
| main.rs:2720:13:2720:17 | x.f() |

0 commit comments

Comments
 (0)