Skip to content

stdlib: remove Boolean protocol #3567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func testObjectiveCBridgeStubURLAppendPath() {
for _ in 0 ..< 10_000 {
var url = startUrl
for _ in 0 ..< 10 {
try! url.appendPathComponent("foo")
url.appendPathComponent("foo")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func testObjectiveCBridgeStubURLAppendPathRef() {
for _ in 0 ..< 10_000 {
var url = startUrl
for _ in 0 ..< 10 {
url = try! url.appendingPathComponent("foo")
url = url.appendingPathComponent("foo")
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,6 @@ public struct AssertionResult : CustomStringConvertible {
self._isPass = isPass
}

public var boolValue: Bool {
return _isPass
}

public func withDescription(_ description: String) -> AssertionResult {
var result = self
result.description += description
Expand Down Expand Up @@ -486,13 +482,13 @@ public func expectUnreachableCatch(_ error: Error, ${TRACE}) {
}

public func expectTrue(_ actual: AssertionResult, ${TRACE}) {
if !actual.boolValue {
if !actual._isPass {
expectationFailure("expected: true", trace: ${trace})
}
}

public func expectFalse(_ actual: AssertionResult, ${TRACE}) {
if actual.boolValue {
if actual._isPass {
expectationFailure("expected: false", trace: ${trace})
}
}
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ extension Bool : _ExpressibleByBuiltinBooleanLiteral, ExpressibleByBooleanLitera
extension Bool {
// This is a magic entry point known to the compiler.
@_transparent
public func _getBuiltinLogicValue() -> Builtin.Int1 {
public // COMPILER_INTRINSIC
func _getBuiltinLogicValue() -> Builtin.Int1 {
return _value
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/1_stdlib/PrintBoolean.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ PrintTests.test("CustomStringConvertible") {
expectTrue(any is CustomStringConvertible)
}

hasDescription(Bool(true))
hasDescription(CBool(true))
hasDescription(true as Bool)
hasDescription(true as CBool)
}

PrintTests.test("Printable") {
expectPrinted("true", CBool(true))
expectPrinted("false", CBool(false))
expectPrinted("true", Bool(true))
expectPrinted("false", Bool(false))
expectPrinted("true", true as CBool)
expectPrinted("false", false as CBool)

expectPrinted("true", true as Bool)
expectPrinted("false", false as Bool)

expectPrinted("true", true)
expectPrinted("false", false)
}
Expand Down
91 changes: 45 additions & 46 deletions test/1_stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,33 @@ Runtime.test("_canBeClass") {

//===----------------------------------------------------------------------===//

protocol MyBoolean {
var boolValue : Bool { get }
}

extension Bool : MyBoolean {
var boolValue : Bool { return self }
}

// The protocol should be defined in the standard library, otherwise the cast
// does not work.
typealias P1 = MyBoolean
typealias P1 = CustomReflectable
typealias P2 = CustomStringConvertible
protocol Q1 {}

extension P1 {
var success: Bool {
print(String(customMirror))
return String(customMirror) == "Mirror for ()"
}
}

// A small struct that can be stored inline in an opaque buffer.
struct StructConformsToP1 : MyBoolean, Q1 {
var boolValue: Bool {
return true
struct StructConformsToP1 : CustomReflectable, Q1 {
var customMirror: Mirror {
return Mirror(reflecting: ())
}
}

// A small struct that can be stored inline in an opaque buffer.
struct Struct2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
struct Struct2ConformsToP1<T : CustomReflectable> : CustomReflectable, Q1 {
init(_ value: T) {
self.value = value
}
var boolValue: Bool {
return value.boolValue
var customMirror: Mirror {
return value.customMirror
}
var value: T
}
Expand Down Expand Up @@ -143,18 +142,18 @@ struct Struct4ConformsToP2<T : CustomStringConvertible> : CustomStringConvertibl

struct StructDoesNotConformToP1 : Q1 {}

class ClassConformsToP1 : MyBoolean, Q1 {
var boolValue: Bool {
return true
class ClassConformsToP1 : CustomReflectable, Q1 {
var customMirror: Mirror {
return Mirror(reflecting: ())
}
}

class Class2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
class Class2ConformsToP1<T : CustomReflectable> : CustomReflectable, Q1 {
init(_ value: T) {
self.value = [value]
}
var boolValue: Bool {
return value[0].boolValue
var customMirror: Mirror {
return value[0].customMirror
}
// FIXME: should be "var value: T", but we don't support it now.
var value: Array<T>
Expand All @@ -164,12 +163,12 @@ class ClassDoesNotConformToP1 : Q1 {}

Runtime.test("dynamicCasting with as") {
var someP1Value = StructConformsToP1()
var someP1Value2 = Struct2ConformsToP1(true)
var someP1Value2 = Struct2ConformsToP1(StructConformsToP1())
var someNotP1Value = StructDoesNotConformToP1()
var someP2Value = Struct3ConformsToP2()
var someP2Value2 = Struct4ConformsToP2(Struct3ConformsToP2())
var someP1Ref = ClassConformsToP1()
var someP1Ref2 = Class2ConformsToP1(true)
var someP1Ref2 = Class2ConformsToP1(ClassConformsToP1())
var someNotP1Ref = ClassDoesNotConformToP1()

expectTrue(someP1Value is P1)
Expand Down Expand Up @@ -209,57 +208,57 @@ Runtime.test("dynamicCasting with as") {
expectTrue(someP1Ref2 as AnyObject is P1)
expectFalse(someNotP1Ref as AnyObject is P1)

expectTrue((someP1Value as P1).boolValue)
expectTrue((someP1Value2 as P1).boolValue)
expectTrue((someP1Value as P1).success)
expectTrue((someP1Value2 as P1).success)
expectEqual("10 20 30 40", (someP2Value as P2).description)
expectEqual("10 20 30 40 50 60 70 80", (someP2Value2 as P2).description)

expectTrue((someP1Ref as P1).boolValue)
expectTrue((someP1Ref2 as P1).boolValue)
expectTrue((someP1Ref as P1).success)
expectTrue((someP1Ref2 as P1).success)

expectTrue(((someP1Value as Q1) as! P1).boolValue)
expectTrue(((someP1Value2 as Q1) as! P1).boolValue)
expectTrue(((someP1Value as Q1) as! P1).success)
expectTrue(((someP1Value2 as Q1) as! P1).success)
expectEqual("10 20 30 40", ((someP2Value as Q1) as! P2).description)
expectEqual("10 20 30 40 50 60 70 80",
((someP2Value2 as Q1) as! P2).description)
expectTrue(((someP1Ref as Q1) as! P1).boolValue)
expectTrue(((someP1Ref2 as Q1) as! P1).boolValue)
expectTrue(((someP1Ref as Q1) as! P1).success)
expectTrue(((someP1Ref2 as Q1) as! P1).success)

expectTrue(((someP1Value as Any) as! P1).boolValue)
expectTrue(((someP1Value2 as Any) as! P1).boolValue)
expectTrue(((someP1Value as Any) as! P1).success)
expectTrue(((someP1Value2 as Any) as! P1).success)
expectEqual("10 20 30 40", ((someP2Value as Any) as! P2).description)
expectEqual("10 20 30 40 50 60 70 80",
((someP2Value2 as Any) as! P2).description)
expectTrue(((someP1Ref as Any) as! P1).boolValue)
expectTrue(((someP1Ref2 as Any) as! P1).boolValue)
expectTrue(((someP1Ref as Any) as! P1).success)
expectTrue(((someP1Ref2 as Any) as! P1).success)

expectTrue(((someP1Ref as AnyObject) as! P1).boolValue)
expectTrue(((someP1Ref as AnyObject) as! P1).success)

expectEmpty((someNotP1Value as? P1))
expectEmpty((someNotP1Ref as? P1))

expectTrue(((someP1Value as Q1) as? P1)!.boolValue)
expectTrue(((someP1Value2 as Q1) as? P1)!.boolValue)
expectTrue(((someP1Value as Q1) as? P1)!.success)
expectTrue(((someP1Value2 as Q1) as? P1)!.success)
expectEmpty(((someNotP1Value as Q1) as? P1))
expectEqual("10 20 30 40", ((someP2Value as Q1) as? P2)!.description)
expectEqual("10 20 30 40 50 60 70 80",
((someP2Value2 as Q1) as? P2)!.description)
expectTrue(((someP1Ref as Q1) as? P1)!.boolValue)
expectTrue(((someP1Ref2 as Q1) as? P1)!.boolValue)
expectTrue(((someP1Ref as Q1) as? P1)!.success)
expectTrue(((someP1Ref2 as Q1) as? P1)!.success)
expectEmpty(((someNotP1Ref as Q1) as? P1))

expectTrue(((someP1Value as Any) as? P1)!.boolValue)
expectTrue(((someP1Value2 as Any) as? P1)!.boolValue)
expectTrue(((someP1Value as Any) as? P1)!.success)
expectTrue(((someP1Value2 as Any) as? P1)!.success)
expectEmpty(((someNotP1Value as Any) as? P1))
expectEqual("10 20 30 40", ((someP2Value as Any) as? P2)!.description)
expectEqual("10 20 30 40 50 60 70 80",
((someP2Value2 as Any) as? P2)!.description)
expectTrue(((someP1Ref as Any) as? P1)!.boolValue)
expectTrue(((someP1Ref2 as Any) as? P1)!.boolValue)
expectTrue(((someP1Ref as Any) as? P1)!.success)
expectTrue(((someP1Ref2 as Any) as? P1)!.success)
expectEmpty(((someNotP1Ref as Any) as? P1))

expectTrue(((someP1Ref as AnyObject) as? P1)!.boolValue)
expectTrue(((someP1Ref2 as AnyObject) as? P1)!.boolValue)
expectTrue(((someP1Ref as AnyObject) as? P1)!.success)
expectTrue(((someP1Ref2 as AnyObject) as? P1)!.success)
expectEmpty(((someNotP1Ref as AnyObject) as? P1))

let doesThrow: (Int) throws -> Int = { $0 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public struct ObjCBool {
self.value = value
}

/// \brief Allow use in a Boolean context.
public var boolValue: Bool {
return value
}
Expand All @@ -29,7 +28,6 @@ public struct ObjCBool {
self.value = value
}

/// \brief Allow use in a Boolean context.
public var boolValue: Bool {
if value == 0 { return false }
return true
Expand Down
2 changes: 0 additions & 2 deletions test/Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public struct ObjCBool {
self.value = value
}

/// \brief Allow use in a Boolean context.
public var boolValue: Bool {
return value
}
Expand All @@ -29,7 +28,6 @@ public struct ObjCBool {
self.value = value
}

/// \brief Allow use in a Boolean context.
public var boolValue: Bool {
if value == 0 { return false }
return true
Expand Down
27 changes: 27 additions & 0 deletions test/Interpreter/bool_as_generic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-run-simple-swift | FileCheck %s
// REQUIRES: executable_test
// <rdar://problem/13986638> Missing Bool metadata when Bool is used as a generic
// parameter or existential value

prefix operator !! {}
infix operator &&& {}

protocol BooleanProtocol {
var boolValue: Bool { get }
}
extension Bool : BooleanProtocol {
var boolValue: Bool { return self }
}

prefix func !!<T : BooleanProtocol>(x: T) -> Bool {
return x.boolValue
}

func &&&(x: BooleanProtocol, y: @autoclosure () -> BooleanProtocol) -> Bool {
return x.boolValue ? y().boolValue : false
}

print(!!true) // CHECK: true
print(!!false) // CHECK: false
print(true &&& true) // CHECK: true
print(true &&& false) // CHECK: false
1 change: 0 additions & 1 deletion test/SILGen/Inputs/ObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
public struct ObjCBool {
var value : UInt8

/// \brief Allow use in a Boolean context.
public var boolValue: Bool {
if value == 0 { return false }
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

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

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

func f<T : Boolean>(_ b: T) {
protocol BooleanProtocol {
var boolValue: Bool { get }
}
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)'}}
extension Bool : BooleanProtocol {
var boolValue: Bool { return self }
}
func f<T : BooleanProtocol>(_ b: T) {
}
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)'}}
11 changes: 5 additions & 6 deletions validation-test/stdlib/FixedPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,13 @@ BoolTestSuite.test("init()") {

BoolTestSuite.test("Boolean") {
do {
var v: Bool = false
var v = false
expectType(Bool.self, &v)
expectFalse(v)
}
do {
var v: Bool = true
var v = true
expectType(Bool.self, &v)
expectTrue(v)
}
}
Expand All @@ -367,10 +369,7 @@ BoolTestSuite.test("CustomStringConvertible") {
}

BoolTestSuite.test("Equatable,Hashable") {
checkHashable(true, false, false)
checkHashable(false, false, true)
checkHashable(true, true, true)

checkHashable([false, true], equalityOracle: { $0 == $1 })
expectNotEqual(false.hashValue, true.hashValue)
}

Expand Down