Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions swift/ql/test/library-tests/type-inference/basics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var topLevelDecl : Int = 0
0
topLevelDecl + 1 // $ type=topLevelDecl:Int

class C {
var myInt : Int
// C.init
init(n: Int) {
myInt = n // $ type=n:Int
}

// C.getMyInt
func getMyInt() -> Int {
return myInt // $ type=.myInt:Int
}
}

class Derived : C {
// Derived.init
init() {
super.init(n: 0) // $ type=super:C target=C.init
}

// Derived.callGetMyInt
func callGetMyInt() -> Int {
let x = getMyInt(); // $ type=x:Int target=C.getMyInt
return x
}
}

class Generic<T> {
var value : T
// Generic.init
init(v: T) {
value = v // $ type=v:T
}

// Generic.getValue
func getValue() -> T {
return value // $ type=.value:T
}
}

class GenericDerived : Generic<Int> {
// GenericDerived.init
init() {
super.init(v: 0) // $ type=super@Generic<T>:Int target=Generic.init
}
}

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

let gd = GenericDerived() // $ type=gd:GenericDerived target=GenericDerived.init
let y = gd.getValue() // $ type=y:Int target=Generic.getValue
}

// --- Extensions ---

extension C {
// C.doubled
func doubled() -> Int {
return myInt * 2 // $ type=.myInt:Int
}
}

func testExtension() {
let obj = C(n: 10) // $ target=C.init
let d = obj.doubled() // $ type=d:Int target=C.doubled
}
231 changes: 231 additions & 0 deletions swift/ql/test/library-tests/type-inference/classes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// --- Static methods ---

class MathUtils {
// MathUtils.square
static func square(x: Int) -> Int {
return x * x // $ type=x:Int
}

// MathUtils.cube
class func cube(x: Int) -> Int {
return x * x * x // $ type=x:Int
}
}

func testStaticMethods() {
let s = MathUtils.square(x: 4) // $ type=s:Int target=MathUtils.square
let cu = MathUtils.cube(x: 3) // $ type=cu:Int target=MathUtils.cube
}

// --- Simple overloading ---

class Overloaded {
// Overloaded.process(_:Int)
func process(_ x: Int) -> Int {
return x + 1 // $ type=x:Int
}

// Overloaded.process(_:String)
func process(_ s: String) -> String {
return s // $ type=s:String
}
}

func testOverloading() {
let o = Overloaded() // $ target=init()
let r1 = o.process(42) // $ type=r1:Int target=Overloaded.process(_:Int)
let r2 = o.process("hello") // $ type=r2:String target=Overloaded.process(_:String)
}

// --- Structs and methods ---

struct Matrix {
var data : [[Int]]

// Matrix.init
init(data: [[Int]]) {
self.data = data
}

// Matrix.rowCount
func rowCount() -> Int {
return data.count
}
}

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

// --- Nested types ---

class Outer {
class Inner {
var value : Int

// Outer.Inner.init
init(value: Int) {
self.value = value // $ type=value:Int
}

// Outer.Inner.getValue
func getValue() -> Int {
return self.value // $ type=.value:Int
}
}
}

func testNestedTypes() {
let inner = Outer.Inner(value: 99) // $ type=inner:Inner target=Outer.Inner.init
let v = inner.getValue() // $ type=v:Int target=Outer.Inner.getValue
}

// --- Method chaining ---

class Builder {
var value : Int = 0

// Builder.init
init() {}

// Builder.set
func set(_ v: Int) -> Builder {
value = v
return self
}

// Builder.add
func add(_ v: Int) -> Builder {
value += v
return self
}

// Builder.build
func build() -> Int {
return value // $ type=.value:Int
}
}

func testChaining() {
let b = Builder() // $ type=b:Builder target=Builder.init
let result = b.set(10).add(5).build() // $ type=result:Int target=Builder.set target=Builder.add target=Builder.build
}

// --- Default parameter values ---

class Config {
// Config.init
init() {}

// Config.setup
func setup(retries: Int = 3, timeout: Double = 30.0) -> Int {
return retries // $ type=retries:Int
}
}

func testDefaultParams() {
let cfg = Config() // $ type=cfg:Config target=Config.init
let r1 = cfg.setup() // $ type=r1:Int target=Config.setup
let r2 = cfg.setup(retries: 5) // $ type=r2:Int target=Config.setup
let r3 = cfg.setup(retries: 2, timeout: 60.0) // $ type=r3:Int target=Config.setup
}

// --- Computed properties accessed via methods ---

class Temperature {
var celsius : Double

// Temperature.init
init(celsius: Double) {
self.celsius = celsius // $ type=celsius:Double
}

// Temperature.toCelsius
func toCelsius() -> Double {
return celsius // $ type=.celsius:Double
}

// Temperature.toFahrenheit
func toFahrenheit() -> Double {
return celsius * 9.0 / 5.0 + 32.0 // $ type=.celsius:Double
}
}

func testTemperature() {
let t = Temperature(celsius: 100.0) // $ type=t:Temperature target=Temperature.init
let c = t.toCelsius() // $ type=c:Double target=Temperature.toCelsius
let f = t.toFahrenheit() // $ type=f:Double target=Temperature.toFahrenheit
}

// --- Inheritance with overriding ---

class Animal {
// Animal.init
init() {}

// Animal.speak
func speak() -> String {
return "..."
}
}

class Dog : Animal {
// Dog.init
override init() {
super.init() // $ target=Animal.init
}

// Dog.speak
override func speak() -> String {
return "Woof"
}

// Dog.fetch
func fetch() -> String {
return "ball"
}
}

class Cat : Animal {
// Cat.init
override init() {
super.init() // $ target=Animal.init
}

// Cat.speak
override func speak() -> String {
return "Meow"
}
}

func testOverriding() {
let d = Dog() // $ type=d:Dog target=Dog.init
let ds = d.speak() // $ type=ds:String target=Dog.speak
let df = d.fetch() // $ type=df:String target=Dog.fetch

let ct = Cat() // $ type=ct:Cat target=Cat.init
let cs = ct.speak() // $ type=cs:String target=Cat.speak
}

// --- Mutating methods on structs ---

struct Counter {
var count : Int = 0

// Counter.increment
mutating func increment() {
count += 1
}

// Counter.getCount
func getCount() -> Int {
return count // $ type=.count:Int
}
}

func testMutating() {
var ctr = Counter() // $ type=ctr:Counter target=init()
ctr.increment() // $ target=Counter.increment
let val = ctr.getCount() // $ type=val:Int target=Counter.getCount
}
Loading
Loading