Skip to content

Commit 9adf294

Browse files
committed
Unified: Basic type inference
1 parent 7a7496a commit 9adf294

32 files changed

Lines changed: 5359 additions & 354 deletions

swift/ql/test/library-tests/type-inference/classes.swift

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,44 @@
33
class MathUtils {
44
// MathUtils.square
55
static func square(x: Int) -> Int {
6-
return x * x // $ type=x:Int
6+
return x * x // $ type=x:Int
77
}
88

99
// MathUtils.cube
1010
class func cube(x: Int) -> Int {
11-
return x * x * x // $ type=x:Int
11+
return x * x * x // $ type=x:Int
1212
}
1313
}
1414

1515
func testStaticMethods() {
16-
let s = MathUtils.square(x: 4) // $ type=s:Int target=MathUtils.square
17-
let cu = MathUtils.cube(x: 3) // $ type=cu:Int target=MathUtils.cube
16+
let s = MathUtils.square(x: 4) // $ type=s:Int target=MathUtils.square
17+
let cu = MathUtils.cube(x: 3) // $ type=cu:Int target=MathUtils.cube
1818
}
1919

2020
// --- Simple overloading ---
2121

2222
class Overloaded {
2323
// Overloaded.process(_:Int)
2424
func process(_ x: Int) -> Int {
25-
return x + 1 // $ type=x:Int
25+
return x + 1 // $ type=x:Int
2626
}
2727

2828
// Overloaded.process(_:String)
2929
func process(_ s: String) -> String {
30-
return s // $ type=s:String
30+
return s // $ type=s:String
3131
}
3232
}
3333

3434
func testOverloading() {
35-
let o = Overloaded() // $ target=init()
36-
let r1 = o.process(42) // $ type=r1:Int target=Overloaded.process(_:Int)
37-
let r2 = o.process("hello") // $ type=r2:String target=Overloaded.process(_:String)
35+
let o = Overloaded() // $ target=init()
36+
let r1 = o.process(42) // $ type=r1:Int target=Overloaded.process(_:Int)
37+
let r2 = o.process("hello") // $ type=r2:String target=Overloaded.process(_:String)
3838
}
3939

4040
// --- Structs and methods ---
4141

4242
struct Matrix {
43-
var data : [[Int]]
43+
var data: [[Int]]
4444

4545
// Matrix.init
4646
init(data: [[Int]]) {
@@ -54,37 +54,37 @@ struct Matrix {
5454
}
5555

5656
func testSubscripts() {
57-
let m = Matrix(data: [[1, 2], [3, 4]]) // $ target=Matrix.init
58-
let rc = m.rowCount() // $ type=rc:Int target=Matrix.rowCount
57+
let m = Matrix(data: [[1, 2], [3, 4]]) // $ target=Matrix.init
58+
let rc = m.rowCount() // $ type=rc:Int target=Matrix.rowCount
5959
}
6060

6161
// --- Nested types ---
6262

6363
class Outer {
6464
class Inner {
65-
var value : Int
65+
var value: Int
6666

6767
// Outer.Inner.init
6868
init(value: Int) {
69-
self.value = value // $ type=value:Int
69+
self.value = value // $ type=value:Int
7070
}
7171

7272
// Outer.Inner.getValue
7373
func getValue() -> Int {
74-
return self.value // $ type=.value:Int
74+
return self.value // $ type=.value:Int
7575
}
7676
}
7777
}
7878

7979
func testNestedTypes() {
80-
let inner = Outer.Inner(value: 99) // $ type=inner:Inner target=Outer.Inner.init
81-
let v = inner.getValue() // $ type=v:Int target=Outer.Inner.getValue
80+
let inner = Outer.Inner(value: 99) // $ type=inner:Inner target=Outer.Inner.init
81+
let v = inner.getValue() // $ type=v:Int target=Outer.Inner.getValue
8282
}
8383

8484
// --- Method chaining ---
8585

8686
class Builder {
87-
var value : Int = 0
87+
var value: Int = 0
8888

8989
// Builder.init
9090
init() {}
@@ -103,13 +103,13 @@ class Builder {
103103

104104
// Builder.build
105105
func build() -> Int {
106-
return value // $ type=.value:Int
106+
return value // $ type=.value:Int
107107
}
108108
}
109109

110110
func testChaining() {
111-
let b = Builder() // $ type=b:Builder target=Builder.init
112-
let result = b.set(10).add(5).build() // $ type=result:Int target=Builder.set target=Builder.add target=Builder.build
111+
let b = Builder() // $ type=b:Builder target=Builder.init
112+
let result = b.set(10).add(5).build() // $ type=result:Int target=Builder.set target=Builder.add target=Builder.build
113113
}
114114

115115
// --- Default parameter values ---
@@ -120,42 +120,42 @@ class Config {
120120

121121
// Config.setup
122122
func setup(retries: Int = 3, timeout: Double = 30.0) -> Int {
123-
return retries // $ type=retries:Int
123+
return retries // $ type=retries:Int
124124
}
125125
}
126126

127127
func testDefaultParams() {
128-
let cfg = Config() // $ type=cfg:Config target=Config.init
129-
let r1 = cfg.setup() // $ type=r1:Int target=Config.setup
130-
let r2 = cfg.setup(retries: 5) // $ type=r2:Int target=Config.setup
131-
let r3 = cfg.setup(retries: 2, timeout: 60.0) // $ type=r3:Int target=Config.setup
128+
let cfg = Config() // $ type=cfg:Config target=Config.init
129+
let r1 = cfg.setup() // $ type=r1:Int target=Config.setup
130+
let r2 = cfg.setup(retries: 5) // $ type=r2:Int target=Config.setup
131+
let r3 = cfg.setup(retries: 2, timeout: 60.0) // $ type=r3:Int target=Config.setup
132132
}
133133

134134
// --- Computed properties accessed via methods ---
135135

136136
class Temperature {
137-
var celsius : Double
137+
var celsius: Double
138138

139139
// Temperature.init
140140
init(celsius: Double) {
141-
self.celsius = celsius // $ type=celsius:Double
141+
self.celsius = celsius // $ type=celsius:Double
142142
}
143143

144144
// Temperature.toCelsius
145145
func toCelsius() -> Double {
146-
return celsius // $ type=.celsius:Double
146+
return celsius // $ type=.celsius:Double
147147
}
148148

149149
// Temperature.toFahrenheit
150150
func toFahrenheit() -> Double {
151-
return celsius * 9.0 / 5.0 + 32.0 // $ type=.celsius:Double
151+
return celsius * 9.0 / 5.0 + 32.0 // $ type=.celsius:Double
152152
}
153153
}
154154

155155
func testTemperature() {
156-
let t = Temperature(celsius: 100.0) // $ type=t:Temperature target=Temperature.init
157-
let c = t.toCelsius() // $ type=c:Double target=Temperature.toCelsius
158-
let f = t.toFahrenheit() // $ type=f:Double target=Temperature.toFahrenheit
156+
let t = Temperature(celsius: 100.0) // $ type=t:Temperature target=Temperature.init
157+
let c = t.toCelsius() // $ type=c:Double target=Temperature.toCelsius
158+
let f = t.toFahrenheit() // $ type=f:Double target=Temperature.toFahrenheit
159159
}
160160

161161
// --- Inheritance with overriding ---
@@ -170,10 +170,10 @@ class Animal {
170170
}
171171
}
172172

173-
class Dog : Animal {
173+
class Dog: Animal {
174174
// Dog.init
175175
override init() {
176-
super.init() // $ target=Animal.init
176+
super.init() // $ target=Animal.init
177177
}
178178

179179
// Dog.speak
@@ -187,10 +187,10 @@ class Dog : Animal {
187187
}
188188
}
189189

190-
class Cat : Animal {
190+
class Cat: Animal {
191191
// Cat.init
192192
override init() {
193-
super.init() // $ target=Animal.init
193+
super.init() // $ target=Animal.init
194194
}
195195

196196
// Cat.speak
@@ -200,18 +200,18 @@ class Cat : Animal {
200200
}
201201

202202
func testOverriding() {
203-
let d = Dog() // $ type=d:Dog target=Dog.init
204-
let ds = d.speak() // $ type=ds:String target=Dog.speak
205-
let df = d.fetch() // $ type=df:String target=Dog.fetch
203+
let d = Dog() // $ type=d:Dog target=Dog.init
204+
let ds = d.speak() // $ type=ds:String target=Dog.speak
205+
let df = d.fetch() // $ type=df:String target=Dog.fetch
206206

207-
let ct = Cat() // $ type=ct:Cat target=Cat.init
208-
let cs = ct.speak() // $ type=cs:String target=Cat.speak
207+
let ct = Cat() // $ type=ct:Cat target=Cat.init
208+
let cs = ct.speak() // $ type=cs:String target=Cat.speak
209209
}
210210

211211
// --- Mutating methods on structs ---
212212

213213
struct Counter {
214-
var count : Int = 0
214+
var count: Int = 0
215215

216216
// Counter.increment
217217
mutating func increment() {
@@ -220,12 +220,12 @@ struct Counter {
220220

221221
// Counter.getCount
222222
func getCount() -> Int {
223-
return count // $ type=.count:Int
223+
return count // $ type=.count:Int
224224
}
225225
}
226226

227227
func testMutating() {
228-
var ctr = Counter() // $ type=ctr:Counter target=init()
229-
ctr.increment() // $ target=Counter.increment
230-
let val = ctr.getCount() // $ type=val:Int target=Counter.getCount
228+
var ctr = Counter() // $ type=ctr:Counter target=init()
229+
ctr.increment() // $ target=Counter.increment
230+
let val = ctr.getCount() // $ type=val:Int target=Counter.getCount
231231
}

swift/ql/test/library-tests/type-inference/generics.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22

33
// identity
44
func identity<T>(_ x: T) -> T {
5-
return x // $ type=x:T
5+
return x // $ type=x:T
66
}
77

88
// makePair
99
func makePair<A, B>(_ a: A, _ b: B) -> (A, B) {
10-
return (a, b) // $ type=a:A
10+
return (a, b) // $ type=a:A
1111
}
1212

1313
func testGenericFunctions() {
14-
let i = identity(42) // $ type=i:Int target=identity
15-
let s = identity("hello") // $ type=s:String target=identity
16-
let p = makePair(1, "two") // $ target=makePair
14+
let i = identity(42) // $ type=i:Int target=identity
15+
let s = identity("hello") // $ type=s:String target=identity
16+
let p = makePair(1, "two") // $ target=makePair
1717
}
1818

1919
// --- Generic structs ---
2020

2121
struct Pair<A, B> {
22-
var first : A
23-
var second : B
22+
var first: A
23+
var second: B
2424

2525
// Pair.init
2626
init(first: A, second: B) {
27-
self.first = first // $ type=first:A
28-
self.second = second // $ type=second:B
27+
self.first = first // $ type=first:A
28+
self.second = second // $ type=second:B
2929
}
3030

3131
// Pair.getFirst
3232
func getFirst() -> A {
33-
return first // $ type=.first:A
33+
return first // $ type=.first:A
3434
}
3535

3636
// Pair.getSecond
3737
func getSecond() -> B {
38-
return second // $ type=.second:B
38+
return second // $ type=.second:B
3939
}
4040
}
4141

4242
func testGenericStruct() {
43-
let p = Pair(first: 1, second: "x") // $ target=Pair.init type=p@Pair<A>:Int type=p@Pair<B>:String
44-
let f = p.getFirst() // $ type=f:Int target=Pair.getFirst
45-
let sc = p.getSecond() // $ type=sc:String target=Pair.getSecond
43+
let p = Pair(first: 1, second: "x") // $ target=Pair.init type=p@Pair<A>:Int type=p@Pair<B>:String
44+
let f = p.getFirst() // $ type=f:Int target=Pair.getFirst
45+
let sc = p.getSecond() // $ type=sc:String target=Pair.getSecond
4646
}
4747

4848
// --- Enums with associated values ---
@@ -56,20 +56,20 @@ enum Result<T> {
5656
// Result.getValue
5757
func getValue() -> T? {
5858
switch self {
59-
case .success(let v): // $ target=Result.success
60-
return v // $ type=v:T
61-
case .failure: // $ target=Result.failure
59+
case .success(let v): // $ target=Result.success
60+
return v // $ type=v:T
61+
case .failure: // $ target=Result.failure
6262
return nil
6363
}
6464
}
6565
}
6666

6767
func testEnum() {
68-
let r = Result.success(42) // $ type=r@Result<T>:Int target=Result.success
69-
let v = r.getValue() // $ target=Result.getValue
68+
let r = Result.success(42) // $ type=r@Result<T>:Int target=Result.success
69+
let v = r.getValue() // $ target=Result.getValue
7070

71-
let r2 : Result<Int> = .success(42) // $ type=r2@Result<T>:Int target=Result.success
72-
let v2 = r2.getValue() // $ target=Result.getValue
71+
let r2: Result<Int> = .success(42) // $ type=r2@Result<T>:Int target=Result.success
72+
let v2 = r2.getValue() // $ target=Result.getValue
7373
}
7474

7575
// --- Closures and type inference ---
@@ -80,8 +80,8 @@ func applyTransform<T, U>(_ value: T, _ transform: (T) -> U) -> U {
8080
}
8181

8282
func testClosures() {
83-
let result = applyTransform(5, { x in x * 2 }) // $ target=applyTransform type=result:Int
84-
let strings = applyTransform(10, { x in String(x) }) // $ target=applyTransform type=strings:String
83+
let result = applyTransform(5, { x in x * 2 }) // $ target=applyTransform type=result:Int
84+
let strings = applyTransform(10, { x in String(x) }) // $ target=applyTransform type=strings:String
8585
}
8686

8787
// --- Generic class with constraints ---
@@ -97,37 +97,37 @@ protocol MyProtocol {
9797
}
9898

9999
class Wrapper<T: MyProtocol> {
100-
var inner : T
100+
var inner: T
101101

102102
// Wrapper.init
103103
init(_ inner: T) {
104-
self.inner = inner // $ type=inner:T
104+
self.inner = inner // $ type=inner:T
105105
}
106106

107107
// Wrapper.get
108108
func get() -> T {
109-
return inner // $ type=.inner:T
109+
return inner // $ type=.inner:T
110110
}
111111

112112
// Wrapper.callFoo
113113
func callFoo() -> T {
114-
let x = inner.foo(); // $ type=x:T target=MyProtocol.foo
114+
let x = inner.foo() // $ type=x:T target=MyProtocol.foo
115115
return x
116116
}
117117

118118
// Wrapper.callBar
119119
func callBar() -> T.MyType {
120-
let x = inner.bar(); // $ type=x:MyType target=MyProtocol.bar
120+
let x = inner.bar() // $ type=x:MyType target=MyProtocol.bar
121121
return x
122122
}
123123
}
124124

125-
extension Int : MyProtocol {
125+
extension Int: MyProtocol {
126126
typealias MyType = String
127-
127+
128128
// Int.foo
129129
func foo() -> Int {
130-
return self * 2 // $ type=self:Int
130+
return self * 2 // $ type=self:Int
131131
}
132132

133133
// Int.bar
@@ -137,7 +137,7 @@ extension Int : MyProtocol {
137137
}
138138

139139
func testConstrainedGeneric() {
140-
let w = Wrapper(42) // $ type=w@Wrapper<T>:Int target=Wrapper.init
141-
let v = w.get() // $ type=v:Int target=Wrapper.get
142-
let z = w.callFoo() // $ type=z:Int target=Wrapper.callFoo
140+
let w = Wrapper(42) // $ type=w@Wrapper<T>:Int target=Wrapper.init
141+
let v = w.get() // $ type=v:Int target=Wrapper.get
142+
let z = w.callFoo() // $ type=z:Int target=Wrapper.callFoo
143143
}

0 commit comments

Comments
 (0)