1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
//===--- FunctionConversion.swift -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
import StdlibUnittest
var FunctionConversionTestSuite = TestSuite("FunctionConversion")
protocol Quilt {
var n: Int8 { get }
}
protocol Patchwork : Quilt {}
struct Trivial : Patchwork {
let n: Int8
}
struct Loadable : Patchwork {
let c: Fabric
var n: Int8 {
return c.n
}
init(n: Int8) {
c = Fabric(n: n)
}
}
struct AddrOnly : Patchwork {
let a: Any
var n: Int8 {
return a as! Int8
}
init(n: Int8) {
a = n
}
}
class Fabric {
let n: Int8
init(n: Int8) {
self.n = n
}
}
class Parent : Patchwork {
let n: Int8
required init(n: Int8) {
self.n = n
}
}
class Child : Parent {}
func t1(s: Trivial?) -> Trivial {
return Trivial(n: s!.n * 2)
}
func l1(s: Loadable?) -> Loadable {
return Loadable(n: s!.n * 2)
}
func a1(s: AddrOnly?) -> AddrOnly {
return AddrOnly(n: s!.n * 2)
}
FunctionConversionTestSuite.test("Optional") {
let g11: (Trivial) -> Trivial? = t1
let g12: (Trivial?) -> Trivial? = t1
expectEqual(22, g11(Trivial(n: 11))?.n)
expectEqual(24, g12(Trivial(n: 12))?.n)
let g21: (Loadable?) -> Loadable? = l1
expectEqual(42, g21(Loadable(n: 21))?.n)
let g31: (AddrOnly?) -> AddrOnly? = a1
expectEqual(62, g31(AddrOnly(n: 31))?.n)
}
func t2(s: Quilt) -> Trivial {
return s as! Trivial
}
func t3(s: Quilt?) -> Trivial {
return s! as! Trivial
}
func l2(s: Quilt) -> Loadable {
return s as! Loadable
}
func l3(s: Quilt?) -> Loadable {
return s! as! Loadable
}
func a2(s: Quilt) -> AddrOnly {
return s as! AddrOnly
}
func a3(s: Quilt?) -> AddrOnly {
return s! as! AddrOnly
}
FunctionConversionTestSuite.test("Existential") {
let g11: (Trivial) -> Patchwork = t2
let g12: (Trivial?) -> Patchwork = t3
let g13: (Patchwork) -> Patchwork = t2
expectEqual(11, g11(Trivial(n: 11)).n)
expectEqual(12, g12(Trivial(n: 12)).n)
expectEqual(13, g13(Trivial(n: 13)).n)
let g21: (Loadable) -> Patchwork = l2
let g22: (Loadable?) -> Patchwork = l3
let g23: (Patchwork) -> Patchwork = l2
expectEqual(21, g21(Loadable(n: 21)).n)
expectEqual(22, g22(Loadable(n: 22)).n)
expectEqual(23, g23(Loadable(n: 23)).n)
let g31: (AddrOnly) -> Patchwork = a2
let g32: (AddrOnly) -> Patchwork = a3
let g33: (Patchwork) -> Patchwork = a2
expectEqual(31, g31(AddrOnly(n: 31)).n)
expectEqual(32, g32(AddrOnly(n: 32)).n)
expectEqual(33, g33(AddrOnly(n: 33)).n)
}
func em(t: Quilt.Type?) -> Trivial.Type {
return t! as! Trivial.Type
}
FunctionConversionTestSuite.test("ExistentialMetatype") {
let g1: (Trivial.Type) -> Patchwork.Type = em
let g2: (Trivial.Type?) -> Patchwork.Type = em
let g3: (Patchwork.Type) -> Patchwork.Type = em
let g4: (Patchwork.Type) -> Any = em
let result1 = g1(Trivial.self)
let result2 = g2(Trivial.self)
let result3 = g3(Trivial.self)
let result4 = g4(Trivial.self)
expectEqual(true, result1 == Trivial.self)
expectEqual(true, result2 == Trivial.self)
expectEqual(true, result3 == Trivial.self)
expectEqual(true, result4 as! Trivial.Type == Trivial.self)
}
func c1(p: Parent) -> (Child, Trivial) {
return (Child(n: p.n), Trivial(n: p.n))
}
func c2(p: Parent?) -> (Child, Trivial) {
return (Child(n: p!.n), Trivial(n: p!.n))
}
FunctionConversionTestSuite.test("ClassUpcast") {
let g1: (Child) -> (Parent, Trivial?) = c1
let g2: (Child) -> (Parent?, Trivial?) = c2
expectEqual(g1(Child(n: 2)).0.n, 2)
expectEqual(g2(Child(n: 4)).0!.n, 4)
}
func cm1(p: Parent.Type) -> (Child.Type, Trivial) {
return (Child.self, Trivial(n: 0))
}
func cm2(p: Parent.Type?) -> (Child.Type, Trivial) {
return (Child.self, Trivial(n: 0))
}
FunctionConversionTestSuite.test("ClassMetatypeUpcast") {
let g1: (Child.Type) -> (Parent.Type, Trivial?) = cm1
let g2: (Child.Type) -> (Parent.Type, Trivial?) = cm2
let g3: (Child.Type?) -> (Parent.Type?, Trivial?) = cm2
let result1 = g1(Child.self)
let result2 = g2(Child.self)
let result3 = g3(Child.self)
expectEqual(true, result1.0 == Child.self)
expectEqual(true, result2.0 == Child.self)
expectEqual(true, result3.0! == Child.self)
}
func sq(i: Int) -> Int {
return i * i
}
func f1(f: Any) -> (Int) -> Int {
return f as! ((Int) -> Int)
}
FunctionConversionTestSuite.test("FuncExistential") {
let g11: (@escaping (Int) -> Int) -> Any = f1
// This used to be but a conversion from a noescape closure to Any is an
// oxymoron. The type checker should really forbid this.
// let g11: ((Int) -> Int) -> Any = f1
expectEqual(100, f1(f: g11(sq))(10))
}
func generic1<T>(t: Parent) -> (T, Trivial) {
return (t as! T, Trivial(n: 0))
}
func generic2<T : Parent>(f: @escaping (Parent) -> (T, Trivial), t: T) -> (Child) -> (Parent, Trivial?) {
return f
}
FunctionConversionTestSuite.test("ClassArchetypeUpcast") {
let g11: (Child) -> (Parent, Trivial?) = generic2(f: generic1, t: Child(n: 10))
expectEqual(10, g11(Child(n: 10)).0.n)
}
func doesNotThrow() {}
FunctionConversionTestSuite.test("ThrowVariance") {
let g: () throws -> () = doesNotThrow
do { try print(g()) } catch {}
}
class A: Quilt {
var n: Int8 {
return 42
}
}
func rdar35702810_arr<T: Quilt>(type: T.Type, _ fn: ([T]?) -> Int8) -> Int8 {
let x: [T] = [A() as! T]
return fn(x)
}
func rdar35702810_map<T: Quilt>(type: T.Type, _ fn: ([String: T]) -> Int8) -> Int8 {
let x: [String: T] = ["ultimate question": A() as! T]
return fn(x)
}
FunctionConversionTestSuite.test("CollectionUpCastsInFuncParameters") {
let fn_arr: ([Quilt]?) -> Int8 = { v in v![0].n }
let fn_map: ([String: Quilt]) -> Int8 = { v in v["ultimate question"]!.n }
expectEqual(rdar35702810_arr(type: A.self, fn_arr), 42)
expectEqual(rdar35702810_map(type: A.self, fn_map), 42)
}
protocol X: Hashable {}
class B: X {
var hashValue: Int { return 42 }
func hash(into hasher: inout Hasher) {}
static func == (lhs: B, rhs: B) -> Bool {
return true
}
}
func rdar35702810_arr_hashable<T: X>(type: T.Type, _ fn: ([T]?) -> Int) -> Int {
let x: [T] = [B() as! T]
return fn(x)
}
func rdar35702810_map_hashable<T: X>(type: T.Type, _ fn: ([String: T]) -> Int) -> Int {
let x: [String: T] = ["ultimate question": B() as! T]
return fn(x)
}
func rdar35702810_set_hashable<T: X>(type: T.Type, _ fn: (Set<T>) -> Int) -> Int {
let x: Set<T> = [B() as! T]
return fn(x)
}
FunctionConversionTestSuite.test("CollectionUpCastsWithHashableInFuncParameters") {
let fn_arr: ([AnyHashable]?) -> Int = { v in v![0].hashValue }
let fn_map: ([String: AnyHashable]) -> Int = { v in v["ultimate question"]!.hashValue }
let fn_set: (Set<AnyHashable>) -> Int = { v in v.first!.hashValue }
expectEqual(rdar35702810_arr_hashable(type: B.self, fn_arr), 42)
expectEqual(rdar35702810_map_hashable(type: B.self, fn_map), 42)
expectEqual(rdar35702810_set_hashable(type: B.self, fn_set), 42)
}
func takesTwo(_ fn: ((AnyObject, AnyObject)) -> (),
_ a: AnyObject,
_ b: AnyObject) {
fn((a, b))
}
func takesTwoGeneric<T>(_ fn: (T) -> (), _ a: T) {
fn(a)
}
FunctionConversionTestSuite.test("SE0110") {
func callback1(_: AnyObject, _: AnyObject) {}
func callback2(_: __owned AnyObject, _: __owned AnyObject) {}
takesTwo(callback1, LifetimeTracked(0), LifetimeTracked(0))
takesTwo(callback2, LifetimeTracked(0), LifetimeTracked(0))
takesTwoGeneric(callback1, (LifetimeTracked(0), LifetimeTracked(0)))
takesTwoGeneric(callback2, (LifetimeTracked(0), LifetimeTracked(0)))
}
runAllTests()
|