File: struct_extra_inhabitants.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 (202 lines) | stat: -rw-r--r-- 7,823 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
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
// RUN: %empty-directory(%t)

// -- build resilient library
// RUN: %target-build-swift -whole-module-optimization -enable-library-evolution -module-name ExtraInhabitantResilientTypes -emit-module-path %t/ExtraInhabitantResilientTypes.swiftmodule -parse-as-library -c -o %t/ExtraInhabitantResilientTypes.o %S/Inputs/struct_extra_inhabitants_ExtraInhabitantResilientTypes.swift

// -- run tests
// RUN: %target-build-swift -parse-stdlib -Xfrontend -verify-type-layout -Xfrontend PairWithPointerFirst -Xfrontend -verify-type-layout -Xfrontend PairWithPointerSecond -Xfrontend -verify-type-layout -Xfrontend PairWithPointerSecondAndPhantomParam_Int -Xfrontend -verify-type-layout -Xfrontend GenericPairWithPointerFirst_Int -Xfrontend -verify-type-layout -Xfrontend GenericPairWithPointerFirst_AnyObject -Xfrontend -verify-type-layout -Xfrontend GenericPairWithPointerSecond_Int -Xfrontend -verify-type-layout -Xfrontend GenericPairWithPointerSecond_AnyObject -Xfrontend -verify-type-layout -Xfrontend StringAlike32 -Xfrontend -verify-type-layout -Xfrontend StringAlike64 -I %t -o %t/a.out.tests %s %t/ExtraInhabitantResilientTypes.o
// RUN: %target-codesign %t/a.out.tests
// RUN: %target-run %t/a.out.tests 2>&1

// Type layout verifier is only compiled into the runtime in asserts builds.
// REQUIRES: swift_stdlib_asserts

// REQUIRES: executable_test
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

// CHECK-NOT: Type verification

import Swift
import ExtraInhabitantResilientTypes
import StdlibUnittest

// Enum layout should use extra inhabitants from any struct field
// for fixed-layout types.
struct PairWithPointerFirst {
  var a: AnyObject
  var b: Int
}

struct PairWithPointerSecond {
  var a: Int
  var b: AnyObject
}

struct PairWithPointerSecondAndPhantomParam<X> {
  var a: Int
  var b: AnyObject
}

struct StringAlike64 {
  var a: Int
  var b: Builtin.BridgeObject
}
struct StringAlike32 {
  var a,b,c: Int
  var d: Builtin.BridgeObject
}

// TODO: Runtime struct instantiation still only considers the first argument

struct GenericPair<T, U> {
  var a: T
  var b: U
}

struct GenericSamePair<T> {
  var a: T
  var b: T
}

struct GenericPairPlusPointer<T, U> {
  var a: T
  var b: U
  var c: UnsafeRawPointer
}

struct GenericSamePairPlusPointer<T> {
  var a: T
  var b: T
  var c: UnsafeRawPointer
}

struct GenericPairWithPointerFirst<T> {
  var a: AnyObject
  var b: T
}

struct GenericPairWithPointerSecond<T> {
  var a: T
  var b: AnyObject
}

struct GenericFullHouse<T, U> {
  var a, b, c: T
  var d, e: U
}

struct ResilientPairWithXIFirst {
  var a: ResilientXI
  var b: ResilientNoXI
}

struct ResilientPairWithXISecond {
  var a: ResilientNoXI
  var b: ResilientXI
}

// Typealiases for the type layout verifier

typealias PairWithPointerSecondAndPhantomParam_Int
 = PairWithPointerSecondAndPhantomParam<Int>

typealias GenericPairWithPointerFirst_Int
 = GenericPairWithPointerFirst<Int>

typealias GenericPairWithPointerFirst_AnyObject
 = GenericPairWithPointerFirst<AnyObject>

typealias GenericPairWithPointerSecond_Int
 = GenericPairWithPointerSecond<Int>

typealias GenericPairWithPointerSecond_AnyObject
 = GenericPairWithPointerSecond<AnyObject>

var tests = TestSuite("extra inhabitants of structs")

@inline(never)
func expectHasAtLeastThreeExtraInhabitants<T>(_: T.Type,
                                              nil theNil: T???,
                                              someNil: T???,
                                              someSomeNil: T???,
                                 file: String = #file, line: UInt = #line) {
  expectEqual(MemoryLayout<T>.size, MemoryLayout<T???>.size,
              "\(T.self) has at least three extra inhabitants",
              file: file, line: line)

  expectNil(theNil,
            "\(T.self) extra inhabitant should agree in generic and concrete " +
            "context")

  expectNil(someNil!,
            "\(T.self) extra inhabitant should agree in generic and concrete " +
            "context")

  expectNil(someSomeNil!!,
            "\(T.self) extra inhabitant should agree in generic and concrete " +
            "context")
}

@inline(never)
func expectHasExtraInhabitant<T>(_: T.Type, nil theNil: T?,
                                 file: String = #file, line: UInt = #line) {
  expectEqual(MemoryLayout<T>.size, MemoryLayout<T?>.size,
              "\(T.self) has extra inhabitant",
              file: file, line: line)

  expectNil(theNil,
            "\(T.self) extra inhabitant should agree in generic and concrete " +
            "context")
}

func expectHasNoExtraInhabitant<T>(_: T.Type,
                                   file: String = #file, line: UInt = #line) {
  expectNotEqual(MemoryLayout<T>.size, MemoryLayout<T?>.size,
                 "\(T.self) does not have extra inhabitant",
                 file: file, line: line)
}

tests.test("types that have extra inhabitants") {
  expectHasExtraInhabitant(PairWithPointerFirst.self, nil: nil)
  expectHasExtraInhabitant(PairWithPointerSecond.self, nil: nil)
  expectHasExtraInhabitant(PairWithPointerSecondAndPhantomParam<Int>.self, nil: nil)
  expectHasExtraInhabitant(PairWithPointerSecondAndPhantomParam<AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairWithPointerFirst<Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairWithPointerFirst<AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairWithPointerSecond<Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairWithPointerSecond<AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(ResilientPairWithXIFirst.self, nil: nil)
  expectHasExtraInhabitant(ResilientPairWithXISecond.self, nil: nil)
  expectHasExtraInhabitant(StringAlike64.self, nil: nil)
  expectHasExtraInhabitant(StringAlike32.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<AnyObject, Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<Int, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<AnyObject, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<AnyObject, UnsafeRawPointer>.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<UnsafeRawPointer, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPair<UnsafeRawPointer, UnsafeRawPointer>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairPlusPointer<Int, Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairPlusPointer<AnyObject, Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairPlusPointer<Int, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericPairPlusPointer<AnyObject, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericSamePair<UnsafeRawPointer>.self, nil: nil)
  expectHasExtraInhabitant(GenericSamePair<AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericSamePairPlusPointer<UnsafeRawPointer>.self, nil: nil)
  expectHasExtraInhabitant(GenericSamePairPlusPointer<AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericSamePairPlusPointer<Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<AnyObject, Int>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<Int, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<AnyObject, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<AnyObject, UnsafeRawPointer>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<UnsafeRawPointer, AnyObject>.self, nil: nil)
  expectHasExtraInhabitant(GenericFullHouse<UnsafeRawPointer, UnsafeRawPointer>.self, nil: nil)
}

tests.test("types that have more than one extra inhabitant") {
  expectHasAtLeastThreeExtraInhabitants(StringAlike64.self, nil: nil, someNil: .some(nil), someSomeNil: .some(.some(nil)))
  expectHasAtLeastThreeExtraInhabitants(StringAlike32.self, nil: nil, someNil: .some(nil), someSomeNil: .some(.some(nil)))
}

runAllTests()