Skip to content

Commit de969c6

Browse files
authored
[ClangImporter] Don't infer 'Comparable' for swift_wrapper. (#9120)
The underlying type's ordering may not be appropriate for the wrapped type (think an ordered list whose underlying type is NSString). Frameworks can always add a Comparable conformance explicitly. We squeak out of this being a source-breaking change by virtue of never having released a working version of it. Rintaro fixed the ambiguity problems back in f11b741, but that was after the last rebranch for Swift 3.1. rdar://problem/30166538
1 parent 534db2c commit de969c6

File tree

4 files changed

+9
-23
lines changed

4 files changed

+9
-23
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4922,7 +4922,6 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
49224922
// implementation in the standard library.
49234923
transferKnown(KnownProtocolKind::Equatable);
49244924
transferKnown(KnownProtocolKind::Hashable);
4925-
transferKnown(KnownProtocolKind::Comparable);
49264925
bool transferredObjCBridgeable =
49274926
transferKnown(KnownProtocolKind::ObjectiveCBridgeable);
49284927

stdlib/public/core/NewtypeWrapper.swift.gyb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ extension _SwiftNewtypeWrapper where Self.RawValue : Hashable {
2121
}
2222
}
2323

24-
% for op in ['<', '>', '<=', '>=']:
25-
public func ${op} <T: _SwiftNewtypeWrapper>(lhs: T, rhs: T) -> Bool
26-
where T : Comparable, T.RawValue : Comparable {
27-
return lhs.rawValue ${op} rhs.rawValue
28-
}
29-
% end
30-
3124
#if _runtime(_ObjC)
3225
extension _SwiftNewtypeWrapper where Self.RawValue : _ObjectiveCBridgeable {
3326
public func _bridgeToObjectiveC() -> Self.RawValue._ObjectiveCType {

test/ClangImporter/newtype_conformance.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ func acceptEquatable<T: Equatable>(_: T) {}
1515
func acceptHashable<T: Hashable>(_: T) {}
1616
func acceptComparable<T: Comparable>(_: T) {}
1717

18-
func testNewTypeWrapperComparable(x: NSNotification.Name, y: NSNotification.Name) {
18+
func testNewTypeWrapper(x: NSNotification.Name, y: NSNotification.Name) {
1919
acceptEquatable(x)
2020
acceptHashable(x)
21-
acceptComparable(x)
21+
acceptComparable(x) // expected-error {{does not conform to expected type 'Comparable'}}
2222

2323
_ = x == y
2424
_ = x != y
2525
_ = x.hashValue
26-
_ = x < y
27-
_ = x > y
28-
_ = x <= y
29-
_ = x >= y
3026
_ = x as NSString
3127
}
3228

test/IDE/newtype.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// RUN: %target-typecheck-verify-swift -sdk %clang-importer-sdk -I %S/Inputs/custom-modules -I %t
77
// REQUIRES: objc_interop
88

9-
// PRINT-LABEL: struct ErrorDomain : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
9+
// PRINT-LABEL: struct ErrorDomain : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, _ObjectiveCBridgeable {
1010
// PRINT-NEXT: init(_ rawValue: String)
1111
// PRINT-NEXT: init(rawValue: String)
1212
// PRINT-NEXT: var _rawValue: NSString
@@ -28,7 +28,7 @@
2828
// PRINT-NEXT: extension Food {
2929
// PRINT-NEXT: static let err: ErrorDomain
3030
// PRINT-NEXT: }
31-
// PRINT-NEXT: struct ClosedEnum : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
31+
// PRINT-NEXT: struct ClosedEnum : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, _ObjectiveCBridgeable {
3232
// PRINT-NEXT: init(rawValue: String)
3333
// PRINT-NEXT: var _rawValue: NSString
3434
// PRINT-NEXT: var rawValue: String { get }
@@ -40,15 +40,15 @@
4040
// PRINT-NEXT: static let secondEntry: ClosedEnum
4141
// PRINT-NEXT: static let thirdEntry: ClosedEnum
4242
// PRINT-NEXT: }
43-
// PRINT-NEXT: struct IUONewtype : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
43+
// PRINT-NEXT: struct IUONewtype : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, _ObjectiveCBridgeable {
4444
// PRINT-NEXT: init(_ rawValue: String)
4545
// PRINT-NEXT: init(rawValue: String)
4646
// PRINT-NEXT: var _rawValue: NSString
4747
// PRINT-NEXT: var rawValue: String { get }
4848
// PRINT-NEXT: typealias RawValue = String
4949
// PRINT-NEXT: typealias _ObjectiveCType = NSString
5050
// PRINT-NEXT: }
51-
// PRINT-NEXT: struct MyFloat : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable {
51+
// PRINT-NEXT: struct MyFloat : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable {
5252
// PRINT-NEXT: init(_ rawValue: Float)
5353
// PRINT-NEXT: init(rawValue: Float)
5454
// PRINT-NEXT: let rawValue: Float
@@ -60,7 +60,7 @@
6060
// PRINT-NEXT: static let version: MyFloat{{$}}
6161
// PRINT-NEXT: }
6262
//
63-
// PRINT-LABEL: struct MyInt : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable {
63+
// PRINT-LABEL: struct MyInt : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable {
6464
// PRINT-NEXT: init(_ rawValue: Int32)
6565
// PRINT-NEXT: init(rawValue: Int32)
6666
// PRINT-NEXT: let rawValue: Int32
@@ -118,7 +118,7 @@
118118
// PRINT-NEXT: func takeMyABIOldType(_: MyABIOldType!)
119119
// PRINT-NEXT: func takeMyABINewTypeNonNull(_: MyABINewType)
120120
// PRINT-NEXT: func takeMyABIOldTypeNonNull(_: MyABIOldType)
121-
// PRINT-NEXT: struct MyABINewTypeNS : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
121+
// PRINT-NEXT: struct MyABINewTypeNS : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, _ObjectiveCBridgeable {
122122
// PRINT-NEXT: init(_ rawValue: String)
123123
// PRINT-NEXT: init(rawValue: String)
124124
// PRINT-NEXT: var _rawValue: NSString
@@ -138,7 +138,7 @@
138138
// PRINT-NEXT: init(i: Int32)
139139
// PRINT-NEXT: }
140140
// PRINT-NEXT: extension NSSomeContext {
141-
// PRINT-NEXT: struct Name : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
141+
// PRINT-NEXT: struct Name : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, _ObjectiveCBridgeable {
142142
// PRINT-NEXT: init(_ rawValue: String)
143143
// PRINT-NEXT: init(rawValue: String)
144144
// PRINT-NEXT: var _rawValue: NSString
@@ -179,14 +179,12 @@ func tests() {
179179
func acceptSwiftNewtypeWrapper<T : _SwiftNewtypeWrapper>(_ t: T) { }
180180
func acceptEquatable<T : Equatable>(_ t: T) { }
181181
func acceptHashable<T : Hashable>(_ t: T) { }
182-
func acceptComparable<T : Hashable>(_ t: T) { }
183182
func acceptObjectiveCBridgeable<T : _ObjectiveCBridgeable>(_ t: T) { }
184183

185184
func testConformances(ed: ErrorDomain) {
186185
acceptSwiftNewtypeWrapper(ed)
187186
acceptEquatable(ed)
188187
acceptHashable(ed)
189-
acceptComparable(ed)
190188
acceptObjectiveCBridgeable(ed)
191189
}
192190

0 commit comments

Comments
 (0)