Skip to content

Commit cb3cff5

Browse files
committed
[test] Test new deprecation warning for hashValue implementations
1 parent 1485404 commit cb3cff5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/Sema/struct_equatable_hashable.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,60 @@ protocol ImplierMain: Equatable {}
261261
struct ImpliedMain: ImplierMain {}
262262
extension ImpliedOther: ImplierMain {}
263263

264+
265+
// Hashable conformances that rely on a manual implementation of `hashValue`
266+
// should produce a deprecation warning.
267+
struct OldSchoolStruct: Hashable {
268+
static func ==(left: OldSchoolStruct, right: OldSchoolStruct) -> Bool {
269+
return true
270+
}
271+
var hashValue: Int { return 42 }
272+
// expected-warning@-1{{'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'OldSchoolStruct' to 'Hashable' by implementing 'hash(into:)' instead}}
273+
}
274+
enum OldSchoolEnum: Hashable {
275+
case foo
276+
case bar
277+
278+
static func ==(left: OldSchoolEnum, right: OldSchoolEnum) -> Bool {
279+
return true
280+
}
281+
var hashValue: Int { return 23 }
282+
// expected-warning@-1{{'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'OldSchoolEnum' to 'Hashable' by implementing 'hash(into:)' instead}}
283+
}
284+
class OldSchoolClass: Hashable {
285+
static func ==(left: OldSchoolClass, right: OldSchoolClass) -> Bool {
286+
return true
287+
}
288+
var hashValue: Int { return -9000 }
289+
// expected-warning@-1{{'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'OldSchoolClass' to 'Hashable' by implementing 'hash(into:)' instead}}
290+
}
291+
292+
// However, it's okay to implement `hashValue` as long as `hash(into:)` is also
293+
// provided.
294+
struct MixedStruct: Hashable {
295+
static func ==(left: MixedStruct, right: MixedStruct) -> Bool {
296+
return true
297+
}
298+
func hash(into hasher: inout Hasher) {}
299+
var hashValue: Int { return 42 }
300+
}
301+
enum MixedEnum: Hashable {
302+
case foo
303+
case bar
304+
static func ==(left: MixedEnum, right: MixedEnum) -> Bool {
305+
return true
306+
}
307+
func hash(into hasher: inout Hasher) {}
308+
var hashValue: Int { return 23 }
309+
}
310+
class MixedClass: Hashable {
311+
static func ==(left: MixedClass, right: MixedClass) -> Bool {
312+
return true
313+
}
314+
func hash(into hasher: inout Hasher) {}
315+
var hashValue: Int { return -9000 }
316+
}
317+
264318
// FIXME: Remove -verify-ignore-unknown.
265319
// <unknown>:0: error: unexpected error produced: invalid redeclaration of 'hashValue'
266320
// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(Foo, Foo) -> Bool'

0 commit comments

Comments
 (0)