|
| 1 | +// RUN: rm -rf %t && mkdir -p %t |
| 2 | +// RUN: cp %s %t/main.swift |
| 3 | +// RUN: %target-swift-frontend -typecheck -verify -primary-file %t/main.swift %S/Inputs/struct_equatable_hashable_other.swift -verify-ignore-unknown |
| 4 | + |
| 5 | +struct Point: Hashable { |
| 6 | + let x: Int |
| 7 | + let y: Int |
| 8 | +} |
| 9 | + |
| 10 | +if Point(x: 1, y: 2) == Point(x: 2, y: 1) { } |
| 11 | +var pointHash: Int = Point(x: 3, y: 5).hashValue |
| 12 | + |
| 13 | +struct Pair<T: Hashable>: Hashable { |
| 14 | + let first: T |
| 15 | + let second: T |
| 16 | + |
| 17 | + func same() -> Bool { |
| 18 | + return first == second |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +let p1 = Pair(first: "a", second: "b") |
| 23 | +let p2 = Pair(first: "a", second: "c") |
| 24 | +if p1 == p2 { } |
| 25 | +var pairHash: Int = p1.hashValue |
| 26 | + |
| 27 | +func localStruct() -> Bool { |
| 28 | + struct Local: Equatable { |
| 29 | + let v: Int |
| 30 | + } |
| 31 | + |
| 32 | + return Local(v: 5) == Local(v: 4) |
| 33 | +} |
| 34 | + |
| 35 | +struct CustomHashable: Hashable { |
| 36 | + let x: Int |
| 37 | + let y: Int |
| 38 | + |
| 39 | + var hashValue: Int { return 0 } |
| 40 | + |
| 41 | + static func ==(x: CustomHashable, y: CustomHashable) -> Bool { return true } |
| 42 | +} |
| 43 | + |
| 44 | +if CustomHashable(x: 1, y: 2) == CustomHashable(x: 2, y: 3) { } |
| 45 | +var custHash: Int = CustomHashable(x: 1, y: 2).hashValue |
| 46 | + |
| 47 | +// Check use of an struct's synthesized members before the struct is actually declared. |
| 48 | +struct UseStructBeforeDeclaration { |
| 49 | + let eqValue = StructToUseBeforeDeclaration(v: 4) == StructToUseBeforeDeclaration(v: 5) |
| 50 | + let hashValue = StructToUseBeforeDeclaration(v: 1).hashValue |
| 51 | +} |
| 52 | +struct StructToUseBeforeDeclaration: Hashable { |
| 53 | + let v: Int |
| 54 | +} |
| 55 | + |
| 56 | +// Check structs from another file in the same module. |
| 57 | +if FromOtherFile(v: "a") == FromOtherFile(v: "b") {} |
| 58 | +let _: Int = FromOtherFile(v: "c").hashValue |
| 59 | + |
| 60 | +func getFromOtherFile() -> AlsoFromOtherFile { return AlsoFromOtherFile(v: 4) } |
| 61 | +if AlsoFromOtherFile(v: 3) == getFromOtherFile() {} |
| 62 | + |
| 63 | +// FIXME: This should work. |
| 64 | +func overloadFromOtherFile() -> YetAnotherFromOtherFile { return YetAnotherFromOtherFile(v: 1.2) } |
| 65 | +func overloadFromOtherFile() -> Bool { return false } |
| 66 | +if YetAnotherFromOtherFile(v: 1.9) == overloadFromOtherFile() {} |
| 67 | + |
| 68 | + |
| 69 | +// Even if the struct has only equatable/hashable members, it's not synthesized |
| 70 | +// implicitly. |
| 71 | +struct StructWithoutExplicitConformance { |
| 72 | + let a: Int |
| 73 | + let b: String |
| 74 | +} |
| 75 | + |
| 76 | +if StructWithoutExplicitConformance(a: 1, b: "b") == StructWithoutExplicitConformance(a: 2, b: "a") { } // expected-error{{binary operator '==' cannot be applied to two 'StructWithoutExplicitConformance' operands}} |
| 77 | +// expected-note @-1 {{overloads for '==' exist with these partially matching parameter lists: }} |
| 78 | + |
| 79 | + |
| 80 | +// Structs with non-hashable/equatable stored properties don't derive conformance. |
| 81 | +struct NotHashable {} |
| 82 | +struct StructWithNonHashablePayload: Hashable { // expected-error 2 {{does not conform}} |
| 83 | + let a: NotHashable |
| 84 | +} |
| 85 | + |
| 86 | +// ...but computed properties and static properties are not considered. |
| 87 | +struct StructIgnoresComputedProperties: Hashable { |
| 88 | + var a: Int |
| 89 | + var b: String |
| 90 | + static var staticComputed = NotHashable() |
| 91 | + var computed: NotHashable { return NotHashable() } |
| 92 | +} |
| 93 | +if StructIgnoresComputedProperties(a: 1, b: "a") == StructIgnoresComputedProperties(a: 2, b: "c") {} |
| 94 | +let _: Int = StructIgnoresComputedProperties(a: 3, b: "p").hashValue |
| 95 | + |
| 96 | + |
| 97 | +// Structs should be able to derive conformances based on the conformances of |
| 98 | +// their generic arguments. |
| 99 | +struct GenericHashable<T: Hashable>: Hashable { |
| 100 | + let value: T |
| 101 | +} |
| 102 | +if GenericHashable<String>(value: "a") == GenericHashable<String>(value: "b") { } |
| 103 | +var genericHashableHash: Int = GenericHashable<String>(value: "a").hashValue |
| 104 | + |
| 105 | +// But it should be an error if the generic argument doesn't have the necessary |
| 106 | +// constrants to satisfy the conditions for derivation. |
| 107 | +struct GenericNotHashable<T: Equatable>: Hashable { // expected-error 2 {{does not conform}} |
| 108 | + let value: T |
| 109 | +} |
| 110 | +if GenericNotHashable<String>(value: "a") == GenericNotHashable<String>(value: "b") { } |
| 111 | +var genericNotHashableHash: Int = GenericNotHashable<String>(value: "a").hashValue // expected-error {{value of type 'GenericNotHashable<String>' has no member 'hashValue'}} |
| 112 | + |
| 113 | + |
| 114 | +// Conformance can be synthesized in an extension. |
| 115 | +struct StructConformsInExtension { |
| 116 | + let v: Int |
| 117 | +} |
| 118 | +extension StructConformsInExtension : Equatable {} |
| 119 | + |
| 120 | +// Explicit conformance in an extension should work too. |
| 121 | +public struct StructConformsAndImplementsInExtension { |
| 122 | + let v: Int |
| 123 | +} |
| 124 | +extension StructConformsAndImplementsInExtension : Equatable { |
| 125 | + public static func ==(lhs: StructConformsAndImplementsInExtension, rhs: StructConformsAndImplementsInExtension) -> Bool { |
| 126 | + return true |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// No explicit conformance and it cannot be derived. |
| 131 | +struct NotExplicitlyHashableAndCannotDerive { // expected-error 2 {{does not conform}} |
| 132 | + let v: NotHashable |
| 133 | +} |
| 134 | +extension NotExplicitlyHashableAndCannotDerive : Hashable {} |
| 135 | + |
| 136 | +// A struct with no stored properties trivially derives conformance. |
| 137 | +struct NoStoredProperties: Hashable {} |
| 138 | + |
| 139 | +// FIXME: Remove -verify-ignore-unknown. |
| 140 | +// <unknown>:0: error: unexpected error produced: invalid redeclaration of 'hashValue' |
| 141 | +// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(Foo, Foo) -> Bool' |
| 142 | +// <unknown>:0: error: unexpected note produced: candidate has non-matching type '<T> (Generic<T>, Generic<T>) -> Bool' |
| 143 | +// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(InvalidCustomHashable, InvalidCustomHashable) -> Bool' |
| 144 | +// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(EnumToUseBeforeDeclaration, EnumToUseBeforeDeclaration) -> Bool' |
0 commit comments