File: OptionalGeneralizations.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (133 lines) | stat: -rw-r--r-- 3,937 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
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
// RUN: %target-run-simple-swift(-enable-experimental-feature Lifetimes)
// REQUIRES: executable_test
// REQUIRES: reflection
// REQUIRES: swift_feature_Lifetimes

import StdlibUnittest
import Swift

let suite = TestSuite("Optional")

defer { runAllTests() }

func isCopyable<T: ~Copyable & ~Escapable>(_: T.Type) -> Bool { false }
func isCopyable<T: ~Escapable>(_: T.Type) -> Bool { true }

func isBitwiseCopyable<T: ~Copyable & ~Escapable>(_: T.Type) -> Bool { false }
func isBitwiseCopyable<T: BitwiseCopyable & ~Escapable>(_: T.Type) -> Bool { true }

#if $NonescapableTypes
func isEscapable<T: ~Escapable & ~Copyable>(_: T.Type) -> Bool { false }
func isEscapable<T: ~Copyable>(_: T.Type) -> Bool { true }
#endif

struct TrivialStruct {}
struct NoncopyableStruct: ~Copyable {}
class RegularClass {}

#if $NonescapableTypes
struct NonescapableStruct: ~Escapable, BitwiseCopyable {}
struct NoncopyableNonescapableStruct: ~Copyable, ~Escapable {}
struct NonescapableNontrivialStruct: ~Escapable {
  let foo: RegularClass? = nil
}
#endif

suite.test("Copyability") {
  expectTrue(isCopyable(Optional<TrivialStruct>.self))
  expectFalse(isCopyable(Optional<NoncopyableStruct>.self))
  expectTrue(isCopyable(Optional<RegularClass>.self))
#if $NonescapableTypes
  expectTrue(isCopyable(Optional<NonescapableStruct>.self))
  expectFalse(isCopyable(Optional<NoncopyableNonescapableStruct>.self))
  expectTrue(isCopyable(Optional<NonescapableNontrivialStruct>.self))
#endif
}

suite.test("BitwiseCopyability") {
  expectTrue(isBitwiseCopyable(Optional<TrivialStruct>.self))
  expectFalse(isBitwiseCopyable(Optional<NoncopyableStruct>.self))
  expectFalse(isBitwiseCopyable(Optional<RegularClass>.self))
#if $NonescapableTypes
  expectTrue(isBitwiseCopyable(Optional<NonescapableStruct>.self))
  expectFalse(isBitwiseCopyable(Optional<NoncopyableNonescapableStruct>.self))
  expectFalse(isBitwiseCopyable(Optional<NonescapableNontrivialStruct>.self))
#endif
}

#if $NonescapableTypes
suite.test("Escapability") {
  expectTrue(isEscapable(Optional<TrivialStruct>.self))
  expectTrue(isEscapable(Optional<NoncopyableStruct>.self))
  expectTrue(isEscapable(Optional<RegularClass>.self))
  expectFalse(isEscapable(Optional<NonescapableStruct>.self))
  expectFalse(isEscapable(Optional<NoncopyableNonescapableStruct>.self))
  expectFalse(isEscapable(Optional<NonescapableNontrivialStruct>.self))
}
#endif

func apply<T, U>(
  _ input: T,
  _ body: (T) -> U
) -> U {
  body(input)
}

func apply2<T: ~Copyable, U: ~Copyable>(
  _ input: consuming T,
  _ body: (consuming T) -> U
) -> U {
  body(input)
}

suite.test("Initializer references") {
  do {
    let r = apply(TrivialStruct(), Optional.init)
    expectTrue(r != nil)
  }

  do {
    let r = apply2(NoncopyableStruct(), Optional.init)
    expectTrue(r != nil)
  }
}

suite.test("expectNotNil()") {
  func opt1<T: ~Copyable>(_ t: consuming T) -> T? { Optional.some(t) }
  _ = expectNotNil(opt1(TrivialStruct()))
  _ = expectNotNil(opt1(NoncopyableStruct()))
  _ = expectNotNil(opt1(RegularClass()))
#if $NonescapableTypes
  @_lifetime(copy t)
  func opt2<T: ~Copyable & ~Escapable>(_ t: consuming T) -> T? { t }

  let ne = NonescapableStruct()
  _ = expectNotNil(opt2(ne))

  let ncne = NoncopyableNonescapableStruct()
  _ = expectNotNil(opt2(ncne))

  let nent = NonescapableNontrivialStruct()
  _ = expectNotNil(opt2(nent))
#endif // $NonescapableTypes
}

suite.test("expectNil()") {
  func opt1<T: ~Copyable>(_ t: consuming T) -> T? { nil }
  expectNil(opt1(TrivialStruct()))
  expectNil(opt1(NoncopyableStruct()))
  expectNil(opt1(RegularClass()))
#if $NonescapableTypes
  @_lifetime(copy t)
  func opt2<T: ~Copyable & ~Escapable>(_ t: consuming T) -> T? { nil }

  let ne = NonescapableStruct()
  expectNil(opt2(ne))

  let ncne = NoncopyableNonescapableStruct()
  expectNil(opt2(ncne))

  let nent = NonescapableNontrivialStruct()
  expectNil(opt2(nent))
#endif // $NonescapableTypes
}