forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtyping.swift
56 lines (44 loc) · 2.57 KB
/
subtyping.swift
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
// RUN: %target-typecheck-verify-swift
protocol CustomStringConvertible {
func print()
}
struct TestFormat {}
protocol FormattedPrintable : CustomStringConvertible {
func print(_: TestFormat)
}
struct IsPrintable1 : CustomStringConvertible {
func print() {}
}
func accept_creates_Printable (_: () -> CustomStringConvertible) {}
func accept_creates_FormattedPrintable (_: () -> FormattedPrintable) {}
func fp_to_p(_ fp: FormattedPrintable) -> CustomStringConvertible { return fp; }
func p_to_fp(_ p: CustomStringConvertible) -> FormattedPrintable { }
func p_to_ip1(_ p: CustomStringConvertible) -> IsPrintable1 { }
func protocolConformance(ac1: @autoclosure () -> CustomStringConvertible,
ac2: @autoclosure () -> FormattedPrintable,
ip1: @autoclosure () -> IsPrintable1) {
var f1 : (_ fp : FormattedPrintable) -> CustomStringConvertible = fp_to_p
var f2 : (_ p : CustomStringConvertible) -> FormattedPrintable = p_to_fp
let f3 : (_ p : CustomStringConvertible) -> IsPrintable1 = p_to_ip1
// FIXME: closures make ABI conversions explicit. rdar://problem/19517003
f1 = { f2($0) } // okay
f1 = { f3($0) } // okay
f2 = f1 // expected-error{{cannot assign value of type '(any FormattedPrintable) -> any CustomStringConvertible' to type '(any CustomStringConvertible) -> any FormattedPrintable'}}
accept_creates_Printable(ac1)
accept_creates_Printable({ ac2() })
accept_creates_Printable({ ip1() })
accept_creates_FormattedPrintable(ac1) // expected-error{{cannot convert value of type '() -> any CustomStringConvertible' to expected argument type '() -> any FormattedPrintable'}}
accept_creates_FormattedPrintable(ac2)
accept_creates_FormattedPrintable(ip1) // expected-error{{cannot convert value of type '() -> IsPrintable1' to expected argument type '() -> any FormattedPrintable'}}
}
func p_gen_to_fp(_: () -> CustomStringConvertible) -> FormattedPrintable {}
func fp_gen_to_p(_: () -> FormattedPrintable) -> CustomStringConvertible {}
func nonTrivialNested() {
// FIXME: closures make ABI conversions explicit. rdar://problem/19517003
var f1 : (_ : () -> CustomStringConvertible) -> CustomStringConvertible = { p_gen_to_fp($0) }
let f2 : (_ : () -> CustomStringConvertible) -> FormattedPrintable = p_gen_to_fp
let f3 : (_ : () -> FormattedPrintable) -> CustomStringConvertible = fp_gen_to_p
f1 = { f2($0) } // okay
f1 = f3 // expected-error{{cannot assign value of type '(() -> any FormattedPrintable) -> any CustomStringConvertible' to type '(() -> any CustomStringConvertible) -> any CustomStringConvertible'}}
_ = f1
}