Skip to content

Commit 103b6b8

Browse files
authored
Merge pull request #3567 from apple/stdlib-remove-boolean
2 parents 5110f13 + e72bece commit 103b6b8

File tree

12 files changed

+100
-77
lines changed

12 files changed

+100
-77
lines changed

benchmark/single-source/unit-tests/ObjectiveCBridgingStubs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func testObjectiveCBridgeStubURLAppendPath() {
169169
for _ in 0 ..< 10_000 {
170170
var url = startUrl
171171
for _ in 0 ..< 10 {
172-
try! url.appendPathComponent("foo")
172+
url.appendPathComponent("foo")
173173
}
174174
}
175175
}

benchmark/single-source/unit-tests/ObjectiveCNoBridgingStubs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func testObjectiveCBridgeStubURLAppendPathRef() {
134134
for _ in 0 ..< 10_000 {
135135
var url = startUrl
136136
for _ in 0 ..< 10 {
137-
url = try! url.appendingPathComponent("foo")
137+
url = url.appendingPathComponent("foo")
138138
}
139139
}
140140
}

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,6 @@ public struct AssertionResult : CustomStringConvertible {
453453
self._isPass = isPass
454454
}
455455

456-
public var boolValue: Bool {
457-
return _isPass
458-
}
459-
460456
public func withDescription(_ description: String) -> AssertionResult {
461457
var result = self
462458
result.description += description
@@ -486,13 +482,13 @@ public func expectUnreachableCatch(_ error: Error, ${TRACE}) {
486482
}
487483

488484
public func expectTrue(_ actual: AssertionResult, ${TRACE}) {
489-
if !actual.boolValue {
485+
if !actual._isPass {
490486
expectationFailure("expected: true", trace: ${trace})
491487
}
492488
}
493489

494490
public func expectFalse(_ actual: AssertionResult, ${TRACE}) {
495-
if actual.boolValue {
491+
if actual._isPass {
496492
expectationFailure("expected: false", trace: ${trace})
497493
}
498494
}

stdlib/public/core/Bool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ extension Bool : _ExpressibleByBuiltinBooleanLiteral, ExpressibleByBooleanLitera
108108
extension Bool {
109109
// This is a magic entry point known to the compiler.
110110
@_transparent
111-
public func _getBuiltinLogicValue() -> Builtin.Int1 {
111+
public // COMPILER_INTRINSIC
112+
func _getBuiltinLogicValue() -> Builtin.Int1 {
112113
return _value
113114
}
114115
}

test/1_stdlib/PrintBoolean.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ PrintTests.test("CustomStringConvertible") {
1111
expectTrue(any is CustomStringConvertible)
1212
}
1313

14-
hasDescription(Bool(true))
15-
hasDescription(CBool(true))
14+
hasDescription(true as Bool)
15+
hasDescription(true as CBool)
1616
}
1717

1818
PrintTests.test("Printable") {
19-
expectPrinted("true", CBool(true))
20-
expectPrinted("false", CBool(false))
21-
22-
expectPrinted("true", Bool(true))
23-
expectPrinted("false", Bool(false))
24-
19+
expectPrinted("true", true as CBool)
20+
expectPrinted("false", false as CBool)
21+
22+
expectPrinted("true", true as Bool)
23+
expectPrinted("false", false as Bool)
24+
2525
expectPrinted("true", true)
2626
expectPrinted("false", false)
2727
}

test/1_stdlib/Runtime.swift.gyb

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,33 @@ Runtime.test("_canBeClass") {
6666

6767
//===----------------------------------------------------------------------===//
6868

69-
protocol MyBoolean {
70-
var boolValue : Bool { get }
71-
}
72-
73-
extension Bool : MyBoolean {
74-
var boolValue : Bool { return self }
75-
}
76-
7769
// The protocol should be defined in the standard library, otherwise the cast
7870
// does not work.
79-
typealias P1 = MyBoolean
71+
typealias P1 = CustomReflectable
8072
typealias P2 = CustomStringConvertible
8173
protocol Q1 {}
8274

75+
extension P1 {
76+
var success: Bool {
77+
print(String(customMirror))
78+
return String(customMirror) == "Mirror for ()"
79+
}
80+
}
81+
8382
// A small struct that can be stored inline in an opaque buffer.
84-
struct StructConformsToP1 : MyBoolean, Q1 {
85-
var boolValue: Bool {
86-
return true
83+
struct StructConformsToP1 : CustomReflectable, Q1 {
84+
var customMirror: Mirror {
85+
return Mirror(reflecting: ())
8786
}
8887
}
8988

9089
// A small struct that can be stored inline in an opaque buffer.
91-
struct Struct2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
90+
struct Struct2ConformsToP1<T : CustomReflectable> : CustomReflectable, Q1 {
9291
init(_ value: T) {
9392
self.value = value
9493
}
95-
var boolValue: Bool {
96-
return value.boolValue
94+
var customMirror: Mirror {
95+
return value.customMirror
9796
}
9897
var value: T
9998
}
@@ -143,18 +142,18 @@ struct Struct4ConformsToP2<T : CustomStringConvertible> : CustomStringConvertibl
143142

144143
struct StructDoesNotConformToP1 : Q1 {}
145144

146-
class ClassConformsToP1 : MyBoolean, Q1 {
147-
var boolValue: Bool {
148-
return true
145+
class ClassConformsToP1 : CustomReflectable, Q1 {
146+
var customMirror: Mirror {
147+
return Mirror(reflecting: ())
149148
}
150149
}
151150

152-
class Class2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
151+
class Class2ConformsToP1<T : CustomReflectable> : CustomReflectable, Q1 {
153152
init(_ value: T) {
154153
self.value = [value]
155154
}
156-
var boolValue: Bool {
157-
return value[0].boolValue
155+
var customMirror: Mirror {
156+
return value[0].customMirror
158157
}
159158
// FIXME: should be "var value: T", but we don't support it now.
160159
var value: Array<T>
@@ -164,12 +163,12 @@ class ClassDoesNotConformToP1 : Q1 {}
164163

165164
Runtime.test("dynamicCasting with as") {
166165
var someP1Value = StructConformsToP1()
167-
var someP1Value2 = Struct2ConformsToP1(true)
166+
var someP1Value2 = Struct2ConformsToP1(StructConformsToP1())
168167
var someNotP1Value = StructDoesNotConformToP1()
169168
var someP2Value = Struct3ConformsToP2()
170169
var someP2Value2 = Struct4ConformsToP2(Struct3ConformsToP2())
171170
var someP1Ref = ClassConformsToP1()
172-
var someP1Ref2 = Class2ConformsToP1(true)
171+
var someP1Ref2 = Class2ConformsToP1(ClassConformsToP1())
173172
var someNotP1Ref = ClassDoesNotConformToP1()
174173

175174
expectTrue(someP1Value is P1)
@@ -209,57 +208,57 @@ Runtime.test("dynamicCasting with as") {
209208
expectTrue(someP1Ref2 as AnyObject is P1)
210209
expectFalse(someNotP1Ref as AnyObject is P1)
211210

212-
expectTrue((someP1Value as P1).boolValue)
213-
expectTrue((someP1Value2 as P1).boolValue)
211+
expectTrue((someP1Value as P1).success)
212+
expectTrue((someP1Value2 as P1).success)
214213
expectEqual("10 20 30 40", (someP2Value as P2).description)
215214
expectEqual("10 20 30 40 50 60 70 80", (someP2Value2 as P2).description)
216215

217-
expectTrue((someP1Ref as P1).boolValue)
218-
expectTrue((someP1Ref2 as P1).boolValue)
216+
expectTrue((someP1Ref as P1).success)
217+
expectTrue((someP1Ref2 as P1).success)
219218

220-
expectTrue(((someP1Value as Q1) as! P1).boolValue)
221-
expectTrue(((someP1Value2 as Q1) as! P1).boolValue)
219+
expectTrue(((someP1Value as Q1) as! P1).success)
220+
expectTrue(((someP1Value2 as Q1) as! P1).success)
222221
expectEqual("10 20 30 40", ((someP2Value as Q1) as! P2).description)
223222
expectEqual("10 20 30 40 50 60 70 80",
224223
((someP2Value2 as Q1) as! P2).description)
225-
expectTrue(((someP1Ref as Q1) as! P1).boolValue)
226-
expectTrue(((someP1Ref2 as Q1) as! P1).boolValue)
224+
expectTrue(((someP1Ref as Q1) as! P1).success)
225+
expectTrue(((someP1Ref2 as Q1) as! P1).success)
227226

228-
expectTrue(((someP1Value as Any) as! P1).boolValue)
229-
expectTrue(((someP1Value2 as Any) as! P1).boolValue)
227+
expectTrue(((someP1Value as Any) as! P1).success)
228+
expectTrue(((someP1Value2 as Any) as! P1).success)
230229
expectEqual("10 20 30 40", ((someP2Value as Any) as! P2).description)
231230
expectEqual("10 20 30 40 50 60 70 80",
232231
((someP2Value2 as Any) as! P2).description)
233-
expectTrue(((someP1Ref as Any) as! P1).boolValue)
234-
expectTrue(((someP1Ref2 as Any) as! P1).boolValue)
232+
expectTrue(((someP1Ref as Any) as! P1).success)
233+
expectTrue(((someP1Ref2 as Any) as! P1).success)
235234

236-
expectTrue(((someP1Ref as AnyObject) as! P1).boolValue)
235+
expectTrue(((someP1Ref as AnyObject) as! P1).success)
237236

238237
expectEmpty((someNotP1Value as? P1))
239238
expectEmpty((someNotP1Ref as? P1))
240239

241-
expectTrue(((someP1Value as Q1) as? P1)!.boolValue)
242-
expectTrue(((someP1Value2 as Q1) as? P1)!.boolValue)
240+
expectTrue(((someP1Value as Q1) as? P1)!.success)
241+
expectTrue(((someP1Value2 as Q1) as? P1)!.success)
243242
expectEmpty(((someNotP1Value as Q1) as? P1))
244243
expectEqual("10 20 30 40", ((someP2Value as Q1) as? P2)!.description)
245244
expectEqual("10 20 30 40 50 60 70 80",
246245
((someP2Value2 as Q1) as? P2)!.description)
247-
expectTrue(((someP1Ref as Q1) as? P1)!.boolValue)
248-
expectTrue(((someP1Ref2 as Q1) as? P1)!.boolValue)
246+
expectTrue(((someP1Ref as Q1) as? P1)!.success)
247+
expectTrue(((someP1Ref2 as Q1) as? P1)!.success)
249248
expectEmpty(((someNotP1Ref as Q1) as? P1))
250249

251-
expectTrue(((someP1Value as Any) as? P1)!.boolValue)
252-
expectTrue(((someP1Value2 as Any) as? P1)!.boolValue)
250+
expectTrue(((someP1Value as Any) as? P1)!.success)
251+
expectTrue(((someP1Value2 as Any) as? P1)!.success)
253252
expectEmpty(((someNotP1Value as Any) as? P1))
254253
expectEqual("10 20 30 40", ((someP2Value as Any) as? P2)!.description)
255254
expectEqual("10 20 30 40 50 60 70 80",
256255
((someP2Value2 as Any) as? P2)!.description)
257-
expectTrue(((someP1Ref as Any) as? P1)!.boolValue)
258-
expectTrue(((someP1Ref2 as Any) as? P1)!.boolValue)
256+
expectTrue(((someP1Ref as Any) as? P1)!.success)
257+
expectTrue(((someP1Ref2 as Any) as? P1)!.success)
259258
expectEmpty(((someNotP1Ref as Any) as? P1))
260259

261-
expectTrue(((someP1Ref as AnyObject) as? P1)!.boolValue)
262-
expectTrue(((someP1Ref2 as AnyObject) as? P1)!.boolValue)
260+
expectTrue(((someP1Ref as AnyObject) as? P1)!.success)
261+
expectTrue(((someP1Ref2 as AnyObject) as? P1)!.success)
263262
expectEmpty(((someNotP1Ref as AnyObject) as? P1))
264263

265264
let doesThrow: (Int) throws -> Int = { $0 }

test/Inputs/clang-importer-sdk/swift-modules-without-ns/ObjectiveC.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public struct ObjCBool {
1010
self.value = value
1111
}
1212

13-
/// \brief Allow use in a Boolean context.
1413
public var boolValue: Bool {
1514
return value
1615
}
@@ -29,7 +28,6 @@ public struct ObjCBool {
2928
self.value = value
3029
}
3130

32-
/// \brief Allow use in a Boolean context.
3331
public var boolValue: Bool {
3432
if value == 0 { return false }
3533
return true

test/Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public struct ObjCBool {
1010
self.value = value
1111
}
1212

13-
/// \brief Allow use in a Boolean context.
1413
public var boolValue: Bool {
1514
return value
1615
}
@@ -29,7 +28,6 @@ public struct ObjCBool {
2928
self.value = value
3029
}
3130

32-
/// \brief Allow use in a Boolean context.
3331
public var boolValue: Bool {
3432
if value == 0 { return false }
3533
return true
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-run-simple-swift | FileCheck %s
2+
// REQUIRES: executable_test
3+
// <rdar://problem/13986638> Missing Bool metadata when Bool is used as a generic
4+
// parameter or existential value
5+
6+
prefix operator !! {}
7+
infix operator &&& {}
8+
9+
protocol BooleanProtocol {
10+
var boolValue: Bool { get }
11+
}
12+
extension Bool : BooleanProtocol {
13+
var boolValue: Bool { return self }
14+
}
15+
16+
prefix func !!<T : BooleanProtocol>(x: T) -> Bool {
17+
return x.boolValue
18+
}
19+
20+
func &&&(x: BooleanProtocol, y: @autoclosure () -> BooleanProtocol) -> Bool {
21+
return x.boolValue ? y().boolValue : false
22+
}
23+
24+
print(!!true) // CHECK: true
25+
print(!!false) // CHECK: false
26+
print(true &&& true) // CHECK: true
27+
print(true &&& false) // CHECK: false

test/SILGen/Inputs/ObjectiveC.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
public struct ObjCBool {
66
var value : UInt8
77

8-
/// \brief Allow use in a Boolean context.
98
public var boolValue: Bool {
109
if value == 0 { return false }
1110
return true

validation-test/compiler_crashers_fixed/00017-llvm-foldingset-llvm-attributesetnode-nodeequals.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
// See http://swift.org/LICENSE.txt for license information
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

9-
// RUN: not %target-swift-frontend %s -parse -verify
9+
// RUN: %target-swift-frontend %s -parse -verify
1010

1111
// Issue found by https://github.com/jvasileff (John Vasileff)
1212
// This bug is NOT triggered when compiling with -O.
1313

14-
func f<T : Boolean>(_ b: T) {
14+
protocol BooleanProtocol {
15+
var boolValue: Bool { get }
1516
}
16-
f(true as Boolean) // expected-error {{cannot invoke 'f' with an argument list of type '(Boolean)'}} // expected-note {{expected an argument list of type '(T)'}}
17+
extension Bool : BooleanProtocol {
18+
var boolValue: Bool { return self }
19+
}
20+
func f<T : BooleanProtocol>(_ b: T) {
21+
}
22+
f(true as BooleanProtocol) // expected-error {{cannot invoke 'f' with an argument list of type '(BooleanProtocol)'}} // expected-note {{expected an argument list of type '(T)'}}

validation-test/stdlib/FixedPoint.swift.gyb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,13 @@ BoolTestSuite.test("init()") {
346346

347347
BoolTestSuite.test("Boolean") {
348348
do {
349-
var v: Bool = false
349+
var v = false
350+
expectType(Bool.self, &v)
350351
expectFalse(v)
351352
}
352353
do {
353-
var v: Bool = true
354+
var v = true
355+
expectType(Bool.self, &v)
354356
expectTrue(v)
355357
}
356358
}
@@ -367,10 +369,7 @@ BoolTestSuite.test("CustomStringConvertible") {
367369
}
368370

369371
BoolTestSuite.test("Equatable,Hashable") {
370-
checkHashable(true, false, false)
371-
checkHashable(false, false, true)
372-
checkHashable(true, true, true)
373-
372+
checkHashable([false, true], equalityOracle: { $0 == $1 })
374373
expectNotEqual(false.hashValue, true.hashValue)
375374
}
376375

0 commit comments

Comments
 (0)