Skip to content

Commit e4cfb86

Browse files
committed
debug
1 parent 5109ebf commit e4cfb86

File tree

3 files changed

+101
-44
lines changed

3 files changed

+101
-44
lines changed

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

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,9 @@ private Type testinferCallExprBaseType(
12831283
CallExprBaseMatchingInput::Access a, string state, CallExprBaseMatchingInput::AccessPosition apos,
12841284
TypePath path
12851285
) {
1286-
a.getLocation().getStartLine() = 73 and
1287-
a.fromSource() and
1286+
// a.getLocation().getStartLine() = 73 and
1287+
// a.fromSource() and
1288+
a = Debug::getRelevantLocatable() and
12881289
result = CallExprBaseMatching::inferAccessType(a, state, apos, path)
12891290
}
12901291

@@ -1302,6 +1303,7 @@ private Type inferCallExprBaseType0(
13021303
CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos, AstNode n,
13031304
string state, TypePath path
13041305
) {
1306+
// a = Debug::getRelevantLocatable() and // todo: remove
13051307
exists(TypePath path0 |
13061308
n = a.getNodeAt(apos) and
13071309
result = CallExprBaseMatching::inferAccessType(a, state, apos, path0)
@@ -1320,8 +1322,7 @@ private Type inferCallExprBaseType0(
13201322
}
13211323

13221324
private Type testinferCallExprBaseType(AstNode n, TypePath path) {
1323-
n.getLocation().getStartLine() = 1286 and
1324-
n.fromSource() and
1325+
n = Debug::getRelevantLocatable() and
13251326
result = inferCallExprBaseType(n, path)
13261327
}
13271328

@@ -1343,7 +1344,7 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
13431344
(
13441345
not apos.getArgumentPosition().isSelf()
13451346
or
1346-
state = ["", ";", ".ref;ref"]
1347+
state = ["", ";"]
13471348
) and
13481349
path = path0
13491350
or
@@ -2068,11 +2069,16 @@ final class MethodCall extends Call {
20682069

20692070
pragma[nomagic]
20702071
private predicate isMethodCall0(Type rootType, string name, int arity, string derefChain) {
2071-
rootType = this.getACandidateReceiverTypeAt(TypePath::nil(), derefChain) and //, mcd.getTypeAt(TypePath::singleton(TRefTypeParameter()))] and
2072+
rootType = this.getACandidateReceiverTypeRefsAt(TypePath::nil(), derefChain) and //, mcd.getTypeAt(TypePath::singleton(TRefTypeParameter()))] and
20722073
name = this.getMethodName() and
20732074
arity = this.getNumberOfArguments()
20742075
}
20752076

2077+
private predicate testisMethodCall0(Type rootType, string name, int arity, string derefChain) {
2078+
this.isMethodCall0(rootType, name, arity, derefChain) and
2079+
this = Debug::getRelevantLocatable()
2080+
}
2081+
20762082
/**
20772083
* Gets a [candidate receiver type][1] of this method call.
20782084
*
@@ -2119,12 +2125,27 @@ final class MethodCall extends Call {
21192125
this.fromSource()
21202126
}
21212127

2128+
pragma[nomagic]
2129+
private Type foogetACandidateReceiverTypeAtNoMatch(
2130+
TypePath path, string derefChain, Type rootType, string name, int arity, Impl impl
2131+
) {
2132+
this = Debug::getRelevantLocatable() and
2133+
this.receiverImplicitlyBorrowed() and
2134+
result = this.getACandidateReceiverTypeAt(path, derefChain) and
2135+
this.isMethodCall0(rootType, name, arity, derefChain + ";") and
2136+
not exists(resolveNonImplMethodCallTarget(rootType, name, arity)) and
2137+
not exists(this.getTrait()) and
2138+
methodCandidate(rootType, name, arity, impl) and
2139+
IsInstantiationOf<MethodCallDerefChainRef, IsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallDerefChainRef(this,
2140+
derefChain + ";"), impl, _)
2141+
}
2142+
21222143
pragma[nomagic]
21232144
private Type getACandidateReceiverTypeAtNoMatch(TypePath path, string derefChain) {
21242145
this.receiverImplicitlyBorrowed() and
21252146
result = this.getACandidateReceiverTypeAt(path, derefChain) and
21262147
exists(Type rootType, string name, int arity |
2127-
this.isMethodCall0(rootType, name, arity, derefChain)
2148+
this.isMethodCall0(rootType, name, arity, derefChain + ";")
21282149
|
21292150
forall(Impl impl |
21302151
methodCandidateTrait(rootType, this.getTrait(), name, arity, impl)
@@ -2134,15 +2155,15 @@ final class MethodCall extends Call {
21342155
|
21352156
IsInstantiationOf<MethodCallDerefChainRef, IsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallDerefChainRef(this,
21362157
derefChain + ";"), impl, _)
2137-
)
2158+
) and
2159+
not exists(resolveNonImplMethodCallTarget(rootType, name, arity))
21382160
)
21392161
}
21402162

21412163
pragma[nomagic]
2142-
private Type testgetACandidateReceiverTypeAtNoMatch(TypePath path, string derefChain) {
2143-
result = this.getACandidateReceiverTypeAtNoMatch(path, derefChain) and
2144-
this.getLocation().getStartLine() = 1286 and
2145-
this.fromSource()
2164+
private Type testgetACandidateReceiverTypeAtRef(TypePath path, string derefChain) {
2165+
result = this.getACandidateReceiverTypeAtRef(path, derefChain) and
2166+
this = Debug::getRelevantLocatable()
21462167
}
21472168

21482169
pragma[nomagic]
@@ -2173,17 +2194,25 @@ final class MethodCall extends Call {
21732194
}
21742195

21752196
pragma[nomagic]
2176-
private Type testgetACandidateReceiverTypeAtRef(TypePath path, string derefChain) {
2177-
result = this.getACandidateReceiverTypeAtRef(path, derefChain) and
2178-
this.getLocation().getStartLine() = 1286 and
2179-
this.fromSource()
2197+
private Type testgetACandidateReceiverTypeAtRefNoMatch(TypePath path, string derefChain) {
2198+
result = this.getACandidateReceiverTypeAtRefNoMatch(path, derefChain) and
2199+
this = Debug::getRelevantLocatable()
2200+
}
2201+
2202+
pragma[nomagic]
2203+
private Type testgetACandidateReceiverTypeAtRefNoMatch2(
2204+
TypePath path, string derefChain, Type rootType, string name, int arity
2205+
) {
2206+
result = this.getACandidateReceiverTypeAtNoMatch(path, derefChain) and
2207+
this.isMethodCall0(rootType, name, arity, derefChain) and
2208+
this = Debug::getRelevantLocatable()
21802209
}
21812210

21822211
pragma[nomagic]
21832212
private Type getACandidateReceiverTypeAtRefNoMatch(TypePath path, string derefChain) {
21842213
result = this.getACandidateReceiverTypeAtNoMatch(path, derefChain) and
21852214
exists(Type rootType, string name, int arity |
2186-
this.isMethodCall0(rootType, name, arity, derefChain)
2215+
this.isMethodCall0(rootType, name, arity, derefChain + ";ref")
21872216
|
21882217
forall(Impl impl |
21892218
methodCandidateTrait(rootType, this.getTrait(), name, arity, impl)
@@ -2193,7 +2222,8 @@ final class MethodCall extends Call {
21932222
|
21942223
IsInstantiationOf<MethodCallDerefChainRef, IsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallDerefChainRef(this,
21952224
derefChain + ";ref"), impl, _)
2196-
)
2225+
) and
2226+
not exists(resolveNonImplMethodCallTarget(rootType, name, arity))
21972227
)
21982228
}
21992229

@@ -2218,19 +2248,22 @@ final class MethodCall extends Call {
22182248
t0 = this.getACandidateReceiverTypeAtRef(path0, derefChain) and
22192249
derefChainRef = derefChain + ";ref"
22202250
|
2221-
path.isEmpty() and
2222-
result = TRefType()
2223-
or
2224-
path = TypePath::cons(TRefTypeParameter(), path0) and
2251+
// path.isEmpty() and
2252+
// result = TRefType()
2253+
// or
2254+
// path = TypePath::cons(TRefTypeParameter(), path0) and
2255+
// result = t0
2256+
path = path0 and
22252257
result = t0
22262258
)
22272259
}
22282260

22292261
pragma[nomagic]
22302262
private Type testgetACandidateReceiverTypeRefsAt(TypePath path, string derefChain) {
22312263
result = this.getACandidateReceiverTypeRefsAt(path, derefChain) and
2232-
this.getLocation().getStartLine() = 1286 and
2233-
this.fromSource()
2264+
this = Debug::getRelevantLocatable()
2265+
// this.getLocation().getStartLine() = 1286 and
2266+
// this.fromSource()
22342267
}
22352268
// /** An alias for `getCandidateReceiverTypeAt`, needed to implement `HasTypeTreeSig`. */
22362269
// Type getTypeAt(TypePath path) { result = this.getACandidateReceiverTypeRefsAt(path) }
@@ -2295,6 +2328,7 @@ private module IsInstantiationOfInput implements IsInstantiationOfInputSig<Metho
22952328
predicate potentialInstantiationOf(
22962329
MethodCallDerefChainRef mc, TypeAbstraction impl, TypeMention constraint
22972330
) {
2331+
// mc.getMethodCall() = Debug::getRelevantLocatable() and // todo
22982332
exists(Type rootType, string name, int arity, MethodCall mc0 |
22992333
isMethodCall(mc, mc0, rootType, name, arity) and
23002334
constraint = impl.(ImplTypeAbstraction).getSelfTy()
@@ -2521,24 +2555,37 @@ private Function getDynTraitMethod(DynTraitType traitObject, string name, int ar
25212555
result = getMethodSuccessor(traitObject.getTrait(), name, arity)
25222556
}
25232557

2558+
bindingset[t, name, arity]
2559+
pragma[inline_late]
2560+
private Function resolveNonImplMethodCallTarget(Type t, string name, int arity) {
2561+
// The type of the receiver is a type parameter and the method comes from a
2562+
// trait bound on the type parameter.
2563+
result = getTypeParameterMethod(t, name, arity)
2564+
or
2565+
// The type of the receiver is an `impl Trait` type.
2566+
result = getImplTraitMethod(t, name, arity)
2567+
or
2568+
// The type of the receiver is a trait object `dyn Trait` type.
2569+
result = getDynTraitMethod(t, name, arity)
2570+
}
2571+
25242572
pragma[nomagic]
25252573
private Function resolveMethodCallTarget(MethodCallDerefChainRef mcd) {
25262574
// The method comes from an `impl` block targeting the type of the receiver.
25272575
result = getMethodFromImpl(mcd)
25282576
or
2529-
exists(Type rootType, string name, int arity | isMethodCall(mcd, _, rootType, name, arity) |
2530-
// The type of the receiver is a type parameter and the method comes from a
2531-
// trait bound on the type parameter.
2532-
result = getTypeParameterMethod(rootType, name, arity)
2533-
or
2534-
// The type of the receiver is an `impl Trait` type.
2535-
result = getImplTraitMethod(rootType, name, arity)
2536-
or
2537-
// The type of the receiver is a trait object `dyn Trait` type.
2538-
result = getDynTraitMethod(rootType, name, arity)
2577+
exists(Type rootType, string name, int arity |
2578+
isMethodCall(mcd, _, rootType, name, arity) and
2579+
result = resolveNonImplMethodCallTarget(rootType, name, arity)
25392580
)
25402581
}
25412582

2583+
private Function testresolveMethodCallTarget(MethodCallDerefChainRef mcd) {
2584+
// result = resolveMethodCallTarget(mcd) and
2585+
result = getMethodFromImpl(mcd) and
2586+
mcd.getMethodCall() = Debug::getRelevantLocatable()
2587+
}
2588+
25422589
pragma[nomagic]
25432590
private predicate assocFuncResolutionDependsOnArgument(Function f, Impl impl, int pos) {
25442591
functionResolutionDependsOnArgument(impl, _, f, pos, _, _) and
@@ -2853,11 +2900,13 @@ Type inferType(AstNode n) { result = inferType(n, TypePath::nil()) }
28532900

28542901
/** Provides predicates for debugging the type inference implementation. */
28552902
private module Debug {
2856-
private Locatable getRelevantLocatable() {
2903+
Locatable getRelevantLocatable() {
28572904
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
28582905
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
2859-
filepath.matches("%/dereference.rs") and
2860-
startline = 171
2906+
filepath.matches("%/crates/wdk-macros/src/lib.rs") and
2907+
endline = [255 .. 256]
2908+
// filepath.matches("%/main.rs") and
2909+
// startline = 708
28612910
)
28622911
}
28632912

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,11 @@ inferType
493493
| dereference.rs:182:18:182:19 | &S | | file://:0:0:0:0 | & |
494494
| dereference.rs:182:18:182:19 | &S | &T | dereference.rs:147:5:147:13 | S |
495495
| dereference.rs:182:19:182:19 | S | | dereference.rs:147:5:147:13 | S |
496+
| dereference.rs:183:13:183:13 | y | | dereference.rs:147:5:147:13 | S |
497+
| dereference.rs:183:13:183:13 | y | | {EXTERNAL LOCATION} | i64 |
496498
| dereference.rs:183:17:183:17 | S | | dereference.rs:147:5:147:13 | S |
499+
| dereference.rs:183:17:183:23 | S.foo() | | dereference.rs:147:5:147:13 | S |
500+
| dereference.rs:183:17:183:23 | S.foo() | | {EXTERNAL LOCATION} | i64 |
497501
| dereference.rs:184:13:184:13 | z | | dereference.rs:147:5:147:13 | S |
498502
| dereference.rs:184:13:184:13 | z | | {EXTERNAL LOCATION} | i64 |
499503
| dereference.rs:184:17:184:24 | (...) | | file://:0:0:0:0 | & |
@@ -6649,6 +6653,9 @@ testFailures
66496653
| dereference.rs:182:13:182:13 | x | Fixed missing result: type=x:S |
66506654
| dereference.rs:182:17:182:26 | ... .foo() | Fixed missing result: target=MyTrait1::foo1 |
66516655
| dereference.rs:182:17:182:26 | ... .foo() | Unexpected result: target=MyTrait1::foo2 |
6656+
| dereference.rs:183:13:183:13 | y | Fixed missing result: type=y:S |
6657+
| dereference.rs:183:17:183:23 | S.foo() | Fixed missing result: target=MyTrait1::foo1 |
6658+
| dereference.rs:183:17:183:23 | S.foo() | Unexpected result: target=MyTrait1::foo2 |
66526659
| dereference.rs:184:13:184:13 | z | Fixed missing result: type=z:i64 |
66536660
| dereference.rs:184:17:184:30 | ... .foo() | Fixed missing result: target=MyTrait1::foo2 |
66546661
| dereference.rs:184:17:184:30 | ... .foo() | Unexpected result: target=MyTrait1::foo1 |

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,14 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
692692
not path.isEmpty() and
693693
app.getTypeAt(path) != t
694694
)
695-
or
696-
// `app` uses inconsistent type parameter instantiations
697-
exists(TypeParameter tp |
698-
potentialInstantiationOf(app, abs, tm) and
699-
app.getTypeAt(getNthTypeParameterPath(tm, tp, _)) !=
700-
app.getTypeAt(getNthTypeParameterPath(tm, tp, _))
701-
)
695+
// or
696+
// // `app` uses inconsistent type parameter instantiations
697+
// exists(TypeParameter tp, Type t1, Type t2 |
698+
// potentialInstantiationOf(app, abs, tm) and
699+
// t1 = app.getTypeAt(getNthTypeParameterPath(tm, tp, _)) and
700+
// t2 = app.getTypeAt(getNthTypeParameterPath(tm, tp, _)) and
701+
// t1 != t2
702+
// )
702703
}
703704
}
704705

0 commit comments

Comments
 (0)