Skip to content

Commit a75b083

Browse files
committed
Automatically infer Equatable conformance for plain C enums
This provides the == operator as well as ~=, which allows for less encumbered pattern matching for these imported types. rdar://problem/17287720 Swift SVN r31469
1 parent 3ec9325 commit a75b083

File tree

8 files changed

+27
-16
lines changed

8 files changed

+27
-16
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,8 @@ namespace {
19321932
structDecl->computeType();
19331933

19341934
ProtocolDecl *protocols[]
1935-
= {cxt.getProtocol(KnownProtocolKind::RawRepresentable)};
1935+
= {cxt.getProtocol(KnownProtocolKind::RawRepresentable),
1936+
cxt.getProtocol(KnownProtocolKind::Equatable)};
19361937
populateInheritedTypes(structDecl, protocols);
19371938

19381939
// Note that this is a raw representable type.

test/ClangModules/Inputs/SwiftPrivateAttr.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ struct __PrivS2 {
5454
init(value: Int32)
5555
}
5656
var __PrivAnonymousA: Int { get }
57-
struct E0 : RawRepresentable {
57+
struct E0 : RawRepresentable, Equatable {
5858
init(_ rawValue: UInt32)
5959
init(rawValue: UInt32)
6060
var rawValue: UInt32
6161
}
6262
var __E0PrivA: E0 { get }
63-
struct __PrivE1 : RawRepresentable {
63+
struct __PrivE1 : RawRepresentable, Equatable {
6464
init(_ rawValue: UInt32)
6565
init(rawValue: UInt32)
6666
var rawValue: UInt32
6767
}
6868
var __PrivE1A: __PrivE1 { get }
69-
struct __PrivE2 : RawRepresentable {
69+
struct __PrivE2 : RawRepresentable, Equatable {
7070
init(_ rawValue: UInt32)
7171
init(rawValue: UInt32)
7272
var rawValue: UInt32

test/ClangModules/ctypes_parse.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ import CoreFoundation
1111
var cgPointVar: CGPoint
1212

1313
func checkRawRepresentable<T: RawRepresentable>(_: T) {}
14+
func checkEquatable<T: Equatable>(_: T) -> Bool {}
15+
func checkEquatablePattern(c: Color) {
16+
switch c {
17+
case red: return
18+
case green: return
19+
case blue: return
20+
}
21+
}
1422

1523
func testColor() {
1624
var c: Color = red
1725
c = blue
1826
_ = c.rawValue
1927
checkRawRepresentable(c)
28+
checkEquatable(c)
29+
checkEquatablePattern(c)
2030
}
2131

2232
func testTribool() {

test/IDE/Inputs/mock-sdk/Foo.FooSub.printed.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@exported import FooHelper
22

33
func fooSubFunc1(a: Int32) -> Int32
4-
struct FooSubEnum1 : RawRepresentable {
4+
struct FooSubEnum1 : RawRepresentable, Equatable {
55
init(_ rawValue: UInt32)
66
init(rawValue: UInt32)
77
var rawValue: UInt32

test/IDE/Inputs/mock-sdk/Foo.annotated.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44

55
<decl:Struct>/// Aaa. FooEnum1. Bbb.
6-
struct <loc>FooEnum1</loc> : <ref:Protocol>RawRepresentable</ref> {
6+
struct <loc>FooEnum1</loc> : <ref:Protocol>RawRepresentable</ref>, <ref:Protocol>Equatable</ref> {
77
<decl:Constructor><loc>init(_ rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
88
<decl:Constructor><loc>init(rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
99
<decl:Var>var <loc>rawValue</loc>: <ref:Struct>UInt32</ref></decl>
1010
}</decl>
1111

1212
<decl:Var>/// Aaa. FooEnum1X. Bbb.
1313
var <loc>FooEnum1X</loc>: <ref:Struct>FooEnum1</ref> { get }</decl>
14-
<decl:Struct>struct <loc>FooEnum2</loc> : <ref:Protocol>RawRepresentable</ref> {
14+
<decl:Struct>struct <loc>FooEnum2</loc> : <ref:Protocol>RawRepresentable</ref>, <ref:Protocol>Equatable</ref> {
1515
<decl:Constructor><loc>init(_ rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
1616
<decl:Constructor><loc>init(rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
1717
<decl:Var>var <loc>rawValue</loc>: <ref:Struct>UInt32</ref></decl>
1818
}</decl>
1919
<decl:Var>var <loc>FooEnum2X</loc>: <ref:Struct>FooEnum2</ref> { get }</decl>
2020
<decl:Var>var <loc>FooEnum2Y</loc>: <ref:Struct>FooEnum2</ref> { get }</decl>
21-
<decl:Struct>struct <loc>FooEnum3</loc> : <ref:Protocol>RawRepresentable</ref> {
21+
<decl:Struct>struct <loc>FooEnum3</loc> : <ref:Protocol>RawRepresentable</ref>, <ref:Protocol>Equatable</ref> {
2222
<decl:Constructor><loc>init(_ rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
2323
<decl:Constructor><loc>init(rawValue: <ref:Struct>UInt32</ref>)</loc></decl>
2424
<decl:Var>var <loc>rawValue</loc>: <ref:Struct>UInt32</ref></decl>

test/IDE/Inputs/mock-sdk/Foo.printed.recursive.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44

55
/// Aaa. FooEnum1. Bbb.
6-
struct FooEnum1 : RawRepresentable {
6+
struct FooEnum1 : RawRepresentable, Equatable {
77
init(_ rawValue: UInt32)
88
init(rawValue: UInt32)
99
var rawValue: UInt32
1010
}
1111

1212
/// Aaa. FooEnum1X. Bbb.
1313
var FooEnum1X: FooEnum1 { get }
14-
struct FooEnum2 : RawRepresentable {
14+
struct FooEnum2 : RawRepresentable, Equatable {
1515
init(_ rawValue: UInt32)
1616
init(rawValue: UInt32)
1717
var rawValue: UInt32
1818
}
1919
var FooEnum2X: FooEnum2 { get }
2020
var FooEnum2Y: FooEnum2 { get }
21-
struct FooEnum3 : RawRepresentable {
21+
struct FooEnum3 : RawRepresentable, Equatable {
2222
init(_ rawValue: UInt32)
2323
init(rawValue: UInt32)
2424
var rawValue: UInt32
@@ -269,7 +269,7 @@ extension FooRepeatedMembers {
269269
extension FooRepeatedMembers {
270270
}
271271
func fooSubFunc1(a: Int32) -> Int32
272-
struct FooSubEnum1 : RawRepresentable {
272+
struct FooSubEnum1 : RawRepresentable, Equatable {
273273
init(_ rawValue: UInt32)
274274
init(rawValue: UInt32)
275275
var rawValue: UInt32

test/IDE/Inputs/mock-sdk/Foo.printed.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Yo.
1212

1313
/// Aaa. FooEnum1. Bbb.
14-
struct FooEnum1 : RawRepresentable {
14+
struct FooEnum1 : RawRepresentable, Equatable {
1515
init(_ rawValue: UInt32)
1616
init(rawValue: UInt32)
1717
var rawValue: UInt32
@@ -20,14 +20,14 @@ struct FooEnum1 : RawRepresentable {
2020
/// Aaa. FooEnum1X. Bbb.
2121
var FooEnum1X: FooEnum1 { get }
2222

23-
struct FooEnum2 : RawRepresentable {
23+
struct FooEnum2 : RawRepresentable, Equatable {
2424
init(_ rawValue: UInt32)
2525
init(rawValue: UInt32)
2626
var rawValue: UInt32
2727
}
2828
var FooEnum2X: FooEnum2 { get }
2929
var FooEnum2Y: FooEnum2 { get }
30-
struct FooEnum3 : RawRepresentable {
30+
struct FooEnum3 : RawRepresentable, Equatable {
3131
init(_ rawValue: UInt32)
3232
init(rawValue: UInt32)
3333
var rawValue: UInt32

test/IDE/Inputs/mock-sdk/FooHelper.FooHelperSub.printed.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
func fooHelperSubFunc1(a: Int32) -> Int32
3-
struct FooHelperSubEnum1 : RawRepresentable {
3+
struct FooHelperSubEnum1 : RawRepresentable, Equatable {
44
init(_ rawValue: UInt32)
55
init(rawValue: UInt32)
66
var rawValue: UInt32

0 commit comments

Comments
 (0)