Skip to content

Commit ab9a603

Browse files
committed
wip
1 parent 629758f commit ab9a603

18 files changed

Lines changed: 1108 additions & 290 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
30063006
exists(e)
30073007
}
30083008

3009+
private Type getInferredType0(AccessEnvironment e, AccessPosition apos, TypePath path) {
3010+
result = this.getInferredType(e, apos, path) and
3011+
exists(this.getTarget(e))
3012+
}
3013+
30093014
Declaration getTarget(AccessEnvironment e) { result = super.getTarget(e) }
30103015
}
30113016
}
@@ -3095,6 +3100,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
30953100
* Holds if `invocation` resolves to some target where the return type at `path`
30963101
* may have to be inferred from the context.
30973102
*/
3103+
// todo: also fields
30983104
pragma[nomagic]
30993105
predicate needsContextualTyping(Invocation invocation, TypePath path) {
31003106
exists(Callable target, TypeParameter tp |
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
var topLevelDecl : Int = 0
1+
var topLevelDecl: Int = 0
22
0
3-
topLevelDecl + 1 // $ type=topLevelDecl:Int
3+
topLevelDecl + 1 // $ type=topLevelDecl:Int
44

55
class C {
6-
var myInt : Int
6+
var myInt: Int
77
// C.init
88
init(n: Int) {
9-
myInt = n // $ type=n:Int
9+
myInt = n // $ type=n:Int
1010
}
1111

1212
// C.getMyInt
1313
func getMyInt() -> Int {
14-
return myInt // $ type=.myInt:Int
14+
return myInt // $ type=.myInt:Int
1515
}
1616
}
1717

18-
class Derived : C {
18+
class Derived: C {
1919
// Derived.init
2020
init() {
21-
super.init(n: 0) // $ type=super:C target=C.init
21+
super.init(n: 0) // $ type=super:C target=C.init
2222
}
2323

2424
// Derived.callGetMyInt
2525
func callGetMyInt() -> Int {
26-
let x = getMyInt(); // $ type=x:Int target=C.getMyInt
26+
let x = getMyInt() // $ type=x:Int target=C.getMyInt
2727
return x
2828
}
2929
}
3030

3131
class Generic<T> {
32-
var value : T
32+
var value: T
3333
// Generic.init
3434
init(v: T) {
35-
value = v // $ type=v:T
35+
value = v // $ type=v:T
3636
}
3737

3838
// Generic.getValue
3939
func getValue() -> T {
40-
return value // $ type=.value:T
40+
return value // $ type=.value:T
4141
}
4242
}
4343

44-
class GenericDerived : Generic<Int> {
44+
class GenericDerived: Generic<Int> {
4545
// GenericDerived.init
4646
init() {
47-
super.init(v: 0) // $ type=super@Generic<T>:Int target=Generic.init
47+
super.init(v: 0) // $ type=super@Generic<T>:Int target=Generic.init
4848
}
4949
}
5050

5151
func testGeneric() {
52-
let g = Generic(v: 42) // $ type=g@Generic<T>:Int target=Generic.init
53-
let x = g.getValue() // $ type=x:Int target=Generic.getValue
52+
let g = Generic(v: 42) // $ type=g@Generic<T>:Int target=Generic.init
53+
let x = g.getValue() // $ type=x:Int target=Generic.getValue
5454

55-
let gd = GenericDerived() // $ type=gd:GenericDerived target=GenericDerived.init
56-
let y = gd.getValue() // $ type=y:Int target=Generic.getValue
55+
let gd = GenericDerived() // $ type=gd:GenericDerived target=GenericDerived.init
56+
let y = gd.getValue() // $ type=y:Int target=Generic.getValue
5757
}
5858

5959
// --- Extensions ---
6060

6161
extension C {
6262
// C.doubled
6363
func doubled() -> Int {
64-
return myInt * 2 // $ type=.myInt:Int
64+
return myInt * 2 // $ type=.myInt:Int
6565
}
6666
}
6767

6868
func testExtension() {
69-
let obj = C(n: 10) // $ target=C.init
70-
let d = obj.doubled() // $ type=d:Int target=C.doubled
69+
let obj = C(n: 10) // $ target=C.init
70+
let d = obj.doubled() // $ type=d:Int target=C.doubled
7171
}

‎unified/ql/lib/codeql/unified/internal/ExprPositions.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ predicate isInTypeContext(Expr expr) {
2929
or
3030
expr = any(ClassLikeDeclaration c).getExtensionTarget()
3131
or
32+
expr = any(GenericTypeExpr gte).getATypeArgument()
33+
or
3234
expr.getParent() instanceof TypeConstraint
3335
or
3436
isInTypeContext(expr.getEnclosingExpr())
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
private import unified
22
private import AllDataFlow
3-
private import codeql.unified.internal.NameBinding as N
4-
5-
private Callable getCallableFromNameBinding(NameBinding binding) {
6-
binding = result.(FunctionDeclaration).getNameNode()
7-
}
3+
private import codeql.unified.internal.typeinference.TypeInference as T
84

95
DataFlowCallable viableCallable(DataFlowCall c) {
10-
exists(CallExpr call, Callable callable, NameBinding target |
11-
c.asExplicitCall() = call and
12-
target = N::getStaticBindingTarget(N::getIdentifierFromRef(call.getCallee())) and
13-
callable = getCallableFromNameBinding(target) and
14-
result.asSourceCallable() = callable
15-
)
6+
result.asSourceCallable() = T::resolveCallTarget(c.asExplicitCall(), _)
167
}

0 commit comments

Comments
 (0)