File: test_backward_deploy_protocol.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 (104 lines) | stat: -rw-r--r-- 2,712 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
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
// RUN: %target-resilience-test --backward-deployment
// REQUIRES: executable_test

// https://github.com/apple/swift/issues/53303
// UNSUPPORTED: OS=windows-msvc

import StdlibUnittest
import backward_deploy_protocol


var BackwardDeployProtocolTest = TestSuite("BackwardDeployProtocol")

struct ConformsToOldWithDefault : OldProtocol {}

struct MyConcrete : OtherProtocol {}
struct ConformsToOldWithNonDefault : OldProtocol {
  typealias NewType = MyConcrete
}

BackwardDeployProtocolTest.test("OldProtocol") {
  if getVersion() == 1 {
    _ = ConformsToOldWithDefault().newMethod()
    _ = ConformsToOldWithNonDefault().newMethod()
  }
}

struct MyNewConforms : NewProtocol {
  func newMethod() {}
}

func dynamicCast<T, U>(_ t: T, _: U.Type) -> Bool {
  return t is U
}

BackwardDeployProtocolTest.test("NewProtocol") {
  if getVersion() == 1 {
    let x1 = NewConforms()
    let x2 = MyNewConforms()
    let x3 = ConformsToOldWithDefault()

    expectEqual(true, dynamicCast(x1, NewProtocol.self))
    expectEqual(true, dynamicCast(x2, NewProtocol.self))
    expectEqual(false, dynamicCast(x3, NewProtocol.self))
  }

  // Make sure that dynamic casts don't crash in the backward
  // deployment case.
  do {
    let x1 = ConformsToOldWithDefault()
    let x2 = MyConcrete()

    expectEqual(false, dynamicCast(x1, OtherProtocol.self))
    expectEqual(true, dynamicCast(x2, OtherProtocol.self))
  }
}

// Weak reference to a conformance descriptor from another module
public protocol RefinedProtocol : NewProtocol {}
extension NewConforms : RefinedProtocol {}

BackwardDeployProtocolTest.test("RefinedProtocol") {
  if getVersion() == 1 {
    let x1 = NewConforms()
    let x2 = MyNewConforms()
    let x3 = ConformsToOldWithDefault()

    expectEqual(true, dynamicCast(x1, RefinedProtocol.self))
    expectEqual(false, dynamicCast(x2, RefinedProtocol.self))
    expectEqual(false, dynamicCast(x3, RefinedProtocol.self))
  }
}

// Witness tables that are weak-linked for various reasons
BackwardDeployProtocolTest.test("WeakWitnessTables") {
  if getVersion() == 1 {
    func f1<T : OtherProtocol>(_: T) {}
    func f2<T : NewProtocol>(_: T) {}
    func f3<T : NewConformanceProtocol>(_: T) {}

    f1(OtherConforms())
    f2(NewConforms())
    f3(NewConformanceConforms())
  }
}

// Conditional conformance with weak-linked requirement
struct Box<T> {}

extension Box : OtherProtocol where T : NewProtocol {}

BackwardDeployProtocolTest.test("ConditionalConformance") {
  if getVersion() == 1 {
    let x1 = Box<MyNewConforms>()

    expectEqual(true, dynamicCast(x1, OtherProtocol.self))
  }

  do {
    let x2 = Box<Int>()
    expectEqual(false, dynamicCast(x2, OtherProtocol.self))
  }
}

runAllTests()