Skip to content

Commit

Permalink
Remove the last parts of the Boolean protocol, finishing up:
Browse files Browse the repository at this point in the history
 SE-0109: Remove the Boolean protocol.

We still love you George, even if we forgot your e.
  • Loading branch information
lattner committed Jul 18, 2016
1 parent ce7751c commit af30ae3
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 180 deletions.
20 changes: 13 additions & 7 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ public func expectRandomAccessCollectionAssociatedTypes<X : RandomAccessCollecti
X.Indices.Index == X.Index,
X.Indices.SubSequence == X.Indices {}

public func expectIsBooleanType<X : Boolean>(_ x: inout X) -> X { return x }

public struct AssertionResult : CustomStringConvertible {
init(isPass: Bool) {
self._isPass = isPass
Expand Down Expand Up @@ -487,21 +485,29 @@ public func expectUnreachableCatch(_ error: Error, ${TRACE}) {
"error should not be thrown: \"\(error)\"", trace: ${trace})
}

%for BoolType in ['Bool', 'AssertionResult']:

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

public func expectFalse(_ actual: ${BoolType}, ${TRACE}) {
public func expectFalse(_ actual: AssertionResult, ${TRACE}) {
if actual.boolValue {
expectationFailure("expected: false", trace: ${trace})
}
}

%end
public func expectTrue(_ actual: Bool, ${TRACE}) {
if !actual {
expectationFailure("expected: true", trace: ${trace})
}
}

public func expectFalse(_ actual: Bool, ${TRACE}) {
if actual {
expectationFailure("expected: false", trace: ${trace})
}
}

public func expectEmpty<T>(_ value: T?, ${TRACE}) {
if value != nil {
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/SDK/CoreMedia/CMTimeRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ extension CMTimeRange {
return CMTimeRangeGetIntersection(self, otherRange)
}
public func containsTime(_ time: CMTime) -> Bool {
return CMTimeRangeContainsTime(self, time).boolValue
return CMTimeRangeContainsTime(self, time)
}
public func containsTimeRange(_ range: CMTimeRange) -> Bool {
return CMTimeRangeContainsTimeRange(self, range).boolValue
return CMTimeRangeContainsTimeRange(self, range)
}
}

Expand All @@ -76,10 +76,10 @@ extension CMTimeRange : Equatable {}

// CMTimeRangeEqual
public func == (range1: CMTimeRange, range2: CMTimeRange) -> Bool {
return CMTimeRangeEqual(range1, range2).boolValue
return CMTimeRangeEqual(range1, range2)
}

public func != (range1: CMTimeRange, range2: CMTimeRange) -> Bool {
return !CMTimeRangeEqual(range1, range2).boolValue
return !CMTimeRangeEqual(range1, range2)
}

10 changes: 5 additions & 5 deletions stdlib/public/SDK/XCTest/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,19 @@ public func XCTAssertNotNil(_ expression: @autoclosure () throws -> Any?, _ mess
}
}

public func XCTAssert(_ expression: @autoclosure () throws -> Boolean, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
public func XCTAssert(_ expression: @autoclosure () throws -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
// XCTAssert is just a cover for XCTAssertTrue.
XCTAssertTrue(expression, message, file: file, line: line)
}

public func XCTAssertTrue(_ expression: @autoclosure () throws -> Boolean, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
public func XCTAssertTrue(_ expression: @autoclosure () throws -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
let assertionType = _XCTAssertionType.`true`

// evaluate the expression exactly once
var expressionValueOptional: Bool?

let result = _XCTRunThrowableBlock {
expressionValueOptional = try expression().boolValue
expressionValueOptional = try expression()
}

switch result {
Expand All @@ -207,14 +207,14 @@ public func XCTAssertTrue(_ expression: @autoclosure () throws -> Boolean, _ mes
}
}

public func XCTAssertFalse(_ expression: @autoclosure () throws -> Boolean, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
public func XCTAssertFalse(_ expression: @autoclosure () throws -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Void {
let assertionType = _XCTAssertionType.`false`

// evaluate the expression exactly once
var expressionValueOptional: Bool?

let result = _XCTRunThrowableBlock {
expressionValueOptional = try expression().boolValue
expressionValueOptional = try expression()
}

switch result {
Expand Down
20 changes: 7 additions & 13 deletions stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/// A value type whose instances are either `true` or `false`.
///
/// `Bool` is the default type for Boolean values in Swift. Create instances of
/// `Bool` represents Boolean values in Swift. Create instances of
/// `Bool` by using one of the Boolean literals `true` and `false` or by
/// assigning the result of a Boolean method or operation to a variable or
/// constant.
Expand Down Expand Up @@ -69,6 +69,10 @@ public struct Bool {
@_versioned
@_transparent
internal init(_ v: Builtin.Int1) { self._value = v }

public init(_ value: Bool) {
self = value
}
}

extension Bool : _ExpressibleByBuiltinBooleanLiteral, ExpressibleByBooleanLiteral {
Expand Down Expand Up @@ -101,22 +105,12 @@ extension Bool : _ExpressibleByBuiltinBooleanLiteral, ExpressibleByBooleanLitera
}
}

extension Bool : Boolean {
extension Bool {
// This is a magic entry point known to the compiler.
@_transparent
public func _getBuiltinLogicValue() -> Builtin.Int1 {
return _value
}

/// This value expressed as a `Bool` instance; its value is identical to that
/// of the current instance.
@_transparent public var boolValue: Bool { return self }

/// Creates an instance representing the given logical value.
///
/// - Parameter value: The logical value for the new instance.
public init<T : Boolean>(_ value: T) {
self = value.boolValue
}
}

extension Bool : CustomStringConvertible {
Expand Down
13 changes: 6 additions & 7 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,22 @@ public func _getUnsafePointerToStoredProperties(_ x: AnyObject)
@_versioned
@_transparent
@_semantics("branchhint")
internal func _branchHint<C : Boolean>(_ actual: C, expected: Bool)
-> Bool {
return Bool(Builtin.int_expect_Int1(actual.boolValue._value, expected._value))
internal func _branchHint(_ actual: Bool, expected: Bool) -> Bool {
return Bool(Builtin.int_expect_Int1(actual._value, expected._value))
}

/// Optimizer hint that `x` is expected to be `true`.
@_transparent
@_semantics("fastpath")
public func _fastPath<C: Boolean>(_ x: C) -> Bool {
return _branchHint(x.boolValue, expected: true)
public func _fastPath(_ x: Bool) -> Bool {
return _branchHint(x, expected: true)
}

/// Optimizer hint that `x` is expected to be `false`.
@_transparent
@_semantics("slowpath")
public func _slowPath<C : Boolean>(_ x: C) -> Bool {
return _branchHint(x.boolValue, expected: false)
public func _slowPath(_ x: Bool) -> Bool {
return _branchHint(x, expected: false)
}

/// Optimizer hint that the code where this function is called is on the fast
Expand Down
44 changes: 2 additions & 42 deletions stdlib/public/core/CompilerProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,6 @@
// Intrinsic protocols shared with the compiler
//===----------------------------------------------------------------------===//

/// A type that represents a Boolean value.
///
/// Types that conform to the `Boolean` protocol can be used as the condition
/// in control statements, such as `if` and `while`, and in other contexts
/// that require a logical value, such as the `where` clause of a `case`
/// statement.
///
/// Swift uses only simple Boolean values in conditional contexts to help avoid
/// accidental programming errors and to help maintain the clarity of each
/// control statement. Unlike other programming languages, integers or strings
/// cannot be used where a Boolean value is expected.
///
/// For example, the following code sample will not compile, because it
/// attempts to use the integer `i` in a logical context:
///
/// var i = 5
/// while i {
/// print(i)
/// i -= 1
/// }
///
/// The correct approach in Swift is to compare the `i` value with zero in the
/// `while` statement.
///
/// while i != 0 {
/// print(i)
/// i -= 1
/// }
///
/// Conforming to the Boolean Protocol
/// ==================================
///
/// To add `Boolean` conformance to your custom type, implement a `boolValue`
/// property that represents your type as an instance of `Bool`, the default
/// concrete type for the `Boolean` protocol.
public protocol Boolean {
/// This value expressed as a `Bool` instance.
var boolValue: Bool { get }
}

/// A type that can be converted to and from an associated raw value.
///
/// With a `RawRepresentable` type, you can switch back and forth between a
Expand Down Expand Up @@ -701,8 +661,8 @@ public protocol _ExpressibleByFileReferenceLiteral {
public protocol _DestructorSafeContainer {
}

@available(*, unavailable, renamed: "Boolean")
public typealias BooleanType = Boolean
@available(*, unavailable, renamed: "Bool")

This comment has been minimized.

Copy link
@jtbandes

jtbandes Jul 18, 2016

Contributor

Perhaps this would be better as message: "BooleanType protocol is no longer available, use Bool instead" ? Although, I guess the fix-it hints from renamed are pretty nice. On that note, maybe boolValue should be defined and @available(*, unavailable) too?

public typealias BooleanType = Bool

// Deprecated by SE-0115.

Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"UnavailableStringAPIs.swift"
],
"Bool": [
"Bool.swift",
"Boolean.swift"
"Bool.swift"
],
"Collection": [
"Collection.swift",
Expand Down
2 changes: 1 addition & 1 deletion test/1_stdlib/Renames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func _CollectionOfOne<T>(i: IteratorOverOne<T>) {
}

func _CompilerProtocols() {
func fn(_: BooleanType) {} // expected-error {{'BooleanType' has been renamed to 'Boolean'}} {{14-25=Boolean}} {{none}}
func fn(_: BooleanType) {} // expected-error {{'BooleanType' has been renamed to 'Bool'}} {{14-25=Bool}} {{none}}
}

func _EmptyCollection<T>(i: EmptyIterator<T>) {
Expand Down
18 changes: 13 additions & 5 deletions test/1_stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,29 @@ 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 = Boolean
typealias P1 = MyBoolean
typealias P2 = CustomStringConvertible
protocol Q1 {}

// A small struct that can be stored inline in an opaque buffer.
struct StructConformsToP1 : Boolean, Q1 {
struct StructConformsToP1 : MyBoolean, Q1 {
var boolValue: Bool {
return true
}
}

// A small struct that can be stored inline in an opaque buffer.
struct Struct2ConformsToP1<T : Boolean> : Boolean, Q1 {
struct Struct2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
init(_ value: T) {
self.value = value
}
Expand Down Expand Up @@ -135,13 +143,13 @@ struct Struct4ConformsToP2<T : CustomStringConvertible> : CustomStringConvertibl

struct StructDoesNotConformToP1 : Q1 {}

class ClassConformsToP1 : Boolean, Q1 {
class ClassConformsToP1 : MyBoolean, Q1 {
var boolValue: Bool {
return true
}
}

class Class2ConformsToP1<T : Boolean> : Boolean, Q1 {
class Class2ConformsToP1<T : MyBoolean> : MyBoolean, Q1 {
init(_ value: T) {
self.value = [value]
}
Expand Down
4 changes: 0 additions & 4 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ func goo() {

func id<T>(_ t: T) -> T { return t }

func doGetLogicValue<T : Boolean>(_ t: T) {
t.boolValue // expected-warning {{expression of type 'Bool' is unused}}
}

protocol P {
init()
func bar(_ x: Int)
Expand Down
8 changes: 4 additions & 4 deletions test/DebugInfo/autoclosure.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %target-swift-frontend %s -emit-ir -g -o - | FileCheck %s

// CHECK: define{{.*}}@_TFF11autoclosure7call_meFVs5Int64T_u_KT_Ps7Boolean_
// CHECK: define{{.*}}@_TF11autoclosure7call_meFVs5Int64T_
// CHECK-NOT: ret void
// CHECK: call void @llvm.dbg.declare{{.*}}, !dbg
// CHECK-NOT: ret void
Expand All @@ -17,14 +17,14 @@ infix operator &&&&& {
precedence 120
}

func &&&&&(lhs: Boolean, rhs: @autoclosure () -> Boolean) -> Bool {
return lhs.boolValue ? rhs().boolValue : false
func &&&&&(lhs: Bool, rhs: @autoclosure () -> Bool) -> Bool {
return lhs ? rhs() : false
}

func call_me(_ input: Int64) -> Void {
// rdar://problem/14627460
// An autoclosure should have a line number in the debug info and a scope line of 0.
// CHECK-DAG: !DISubprogram({{.*}}linkageName: "_TFF11autoclosure7call_meFVs5Int64T_u_KT_Ps7Boolean_",{{.*}} line: [[@LINE+3]],{{.*}} isLocal: false, isDefinition: true
// CHECK-DAG: !DISubprogram({{.*}}linkageName: "_TFF11autoclosure7call_meFVs5Int64T_u_KT_Sb",{{.*}} line: [[@LINE+3]],{{.*}} isLocal: false, isDefinition: true
// But not in the line table.
// CHECK-DAG: ![[DBG]] = !DILocation(line: [[@LINE+1]],
if input != 0 &&&&& ( get_truth (input * 2 + 1) > 0 ) {
Expand Down
2 changes: 1 addition & 1 deletion test/DebugInfo/local-vars.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class C {
let member : Int
init(_ i : Int) { member = i }
func isZero() -> Boolean { return member == 0 }
func isZero() -> Bool { return member == 0 }
}

public struct S {
Expand Down
1 change: 0 additions & 1 deletion test/IDE/complete_literal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
true.#^LITERAL3^#
}
// LITERAL3: Begin completions
// LITERAL3-DAG: Decl[InstanceVar]/CurrNominal: boolValue[#Bool#]; name=boolValue{{$}}
// LITERAL3-DAG: Decl[InstanceVar]/CurrNominal: description[#String#]; name=description{{$}}
// LITERAL3-DAG: Decl[InstanceVar]/CurrNominal: hashValue[#Int#]; name=hashValue{{$}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// The iOS/arm64 target uses _Bool for Objective-C's BOOL. We include
// x86_64 here as well because the iOS simulator also uses _Bool.
#if ((os(iOS) || os(tvOS)) && (arch(arm64) || arch(x86_64))) || os(watchOS)
public struct ObjCBool : Boolean {
public struct ObjCBool {
private var value : Bool

public init(_ value: Bool) {
Expand All @@ -18,7 +18,7 @@ public struct ObjCBool : Boolean {

#else

public struct ObjCBool : Boolean {
public struct ObjCBool {
private var value : UInt8

public init(_ value: Bool) {
Expand Down Expand Up @@ -68,7 +68,7 @@ internal func _convertBoolToObjCBool(_ x: Bool) -> ObjCBool {
}

internal func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
return Bool(x)
return x.boolValue
}

public func ~=(x: NSObject, y: NSObject) -> Bool {
Expand Down
Loading

0 comments on commit af30ae3

Please sign in to comment.