File: subtyping.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (56 lines) | stat: -rw-r--r-- 2,630 bytes parent folder | download
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
}