@@ -91,7 +91,12 @@ func TestSwiftObjectNSObjectAssertNoErrors()
91
91
// Verify that Obj-C isEqual: provides same answer as Swift ==
92
92
func TestEquatableEquals<T: Equatable & AnyObject>(_ e1: T, _ e2: T) {
93
93
if e1 == e2 {
94
+ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
95
+ // Legacy behavior: Equatable Swift does not imply == in ObjC
96
+ TestSwiftObjectNSObjectNotEquals(e1, e2)
97
+ #else
94
98
TestSwiftObjectNSObjectEquals(e1, e2)
99
+ #endif
95
100
} else {
96
101
TestSwiftObjectNSObjectNotEquals(e1, e2)
97
102
}
@@ -104,14 +109,26 @@ func TestNonEquatableEquals(_ e1: AnyObject, _ e2: AnyObject) {
104
109
// Verify that Obj-C hashValue matches Swift hashValue for Hashable types
105
110
func TestHashable(_ h: H)
106
111
{
112
+ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
113
+ // Legacy behavior: Hash value is identity in ObjC
114
+ TestSwiftObjectNSObjectDefaultHashValue(h)
115
+ #else
116
+ // New behavior: Hashable in Swift, same hash value in ObjC
107
117
TestSwiftObjectNSObjectHashValue(h, h.hashValue)
118
+ #endif
108
119
}
109
120
110
121
// Test Obj-C hashValue for Swift types that are Equatable but not Hashable
111
122
func TestEquatableHash(_ e: AnyObject)
112
123
{
113
- // These should have a constant hash value
124
+ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
125
+ // Legacy behavior: Equatable in Swift => ObjC hashes with identity
126
+ TestSwiftObjectNSObjectDefaultHashValue(e)
127
+ fakeEquatableWarning(e)
128
+ #else
129
+ // New behavior: These should have a constant hash value
114
130
TestSwiftObjectNSObjectHashValue(e, 1)
131
+ #endif
115
132
}
116
133
117
134
func TestNonEquatableHash(_ e: AnyObject)
@@ -125,11 +142,19 @@ func TestNonEquatableHash(_ e: AnyObject)
125
142
// CHECK-NEXT: d ##SwiftObjectNSObject.D##
126
143
// CHECK-NEXT: S ##{{.*}}SwiftObject##
127
144
128
- // Full message is longer, but this is the essential part ...
145
+ // Verify that the runtime emits the warning that we expected ...
129
146
// CHECK-NEXT: Obj-C `-hash` {{.*}} type `SwiftObjectNSObject.E` {{.*}} Equatable but not Hashable
130
147
// CHECK-NEXT: Obj-C `-hash` {{.*}} type `SwiftObjectNSObject.E1` {{.*}} Equatable but not Hashable
131
148
// CHECK-NEXT: Obj-C `-hash` {{.*}} type `SwiftObjectNSObject.E2` {{.*}} Equatable but not Hashable
132
149
150
+ // If we're checking legacy behavior or unsupported platform, then
151
+ // the warning above won't be emitted. This function emits a fake
152
+ // message that will satisfy the checks above in such cases.
153
+ func fakeEquatableWarning(_ e: AnyObject) {
154
+ let msg = " Obj- C `-hash` ... type `SwiftObjectNSObject.\(type(of: e) ) ` ... Equatable but not Hashable\n"
155
+ fputs(msg, stderr)
156
+ }
157
+
133
158
// Temporarily disable this test on older OSes until we have time to
134
159
// look into why it's failing there. rdar://problem/47870743
135
160
if #available(OSX 10.12, iOS 10.0, *) {
@@ -195,7 +220,7 @@ if #available(OSX 10.12, iOS 10.0, *) {
195
220
fputs( " c ##SwiftObjectNSObject. C##\n" , stderr)
196
221
fputs ( " d ##SwiftObjectNSObject.D## \n " , stderr)
197
222
fputs ( " S ##Swift._SwiftObject## \n " , stderr)
198
- fputs ( " Obj-C `-hash` ... type `SwiftObjectNSObject.E` ... Equatable but not Hashable " , stderr )
199
- fputs ( " Obj-C `-hash` ... type `SwiftObjectNSObject.E1` ... Equatable but not Hashable " , stderr )
200
- fputs ( " Obj-C `-hash` ... type `SwiftObjectNSObject.E2` ... Equatable but not Hashable " , stderr )
223
+ fakeEquatableWarning ( E ( i : 1 ) )
224
+ fakeEquatableWarning ( E1 ( i : 1 ) )
225
+ fakeEquatableWarning ( E2 ( i : 1 ) )
201
226
}
0 commit comments