mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
If an enum has no cases with payloads, make it implicitly Equatable and Hashable, and derive default implementations of '==' and 'hashValue'. Insert the derived '==' into module context wrapped in a new DerivedFileUnit kind, and arrange for it to be codegenned with the deriving EnumDecl by adding a 'DerivedOperatorDecls' array to NominalTypeDecls that gets visited at SILGen time. Swift SVN r14471
65 lines
1.3 KiB
Swift
65 lines
1.3 KiB
Swift
// RUN: %swift -parse -verify %s
|
|
|
|
enum Foo {
|
|
case A, B
|
|
}
|
|
|
|
if Foo.A == .B { }
|
|
var aHash: Int = Foo.A.hashValue()
|
|
|
|
enum Generic<T> {
|
|
case A, B
|
|
|
|
func method() -> Int {
|
|
if A == B { }
|
|
return A.hashValue()
|
|
}
|
|
}
|
|
|
|
if Generic<Foo>.A == .B { }
|
|
var gaHash: Int = Generic<Foo>.A.hashValue()
|
|
|
|
func localEnum() -> Bool {
|
|
enum Local {
|
|
case A, B
|
|
}
|
|
|
|
return Local.A == .B
|
|
}
|
|
|
|
enum CustomHashable {
|
|
case A, B
|
|
|
|
func hashValue() -> Int { return 0 }
|
|
}
|
|
func ==(x: CustomHashable, y: CustomHashable) -> Bool {
|
|
return true
|
|
}
|
|
|
|
if CustomHashable.A == .B { }
|
|
var custHash: Int = CustomHashable.A.hashValue()
|
|
|
|
// We still synthesize conforming overloads of '==' and 'hashValue' if
|
|
// explicit definitions don't satisfy the protocol requirements. Probably
|
|
// not what we actually want.
|
|
enum InvalidCustomHashable {
|
|
case A, B
|
|
|
|
func hashValue() -> String { return "" }
|
|
}
|
|
func ==(x: InvalidCustomHashable, y: InvalidCustomHashable) -> String {
|
|
return ""
|
|
}
|
|
if InvalidCustomHashable.A == .B { }
|
|
var s: String = InvalidCustomHashable.A == .B
|
|
s = InvalidCustomHashable.A.hashValue()
|
|
var i: Int = InvalidCustomHashable.A.hashValue()
|
|
|
|
// Complex enums are not implicitly Equatable or Hashable.
|
|
enum Complex {
|
|
case A(Int)
|
|
case B
|
|
}
|
|
|
|
if Complex.A(1) == .B { } // expected-error{{does not type-check}}
|