Skip to content

Commit 9348d44

Browse files
authored
Merge pull request #61639 from mikepinkerton/enum-test
[cxx-interop] Test typed and untyped enums cannot be assigned to each other
2 parents 24ec0ed + 441ebd3 commit 9348d44

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

test/Interop/Cxx/enum/Inputs/module.modulemap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ module CenumsWithOptionsOmit {
2121
module CenumsNSOptions {
2222
header "c-enums-NS_OPTIONS.h"
2323
requires cplusplus
24-
}
24+
}
25+
26+
module TypedUntypedEnums {
27+
header "typed-untyped-enums.h"
28+
requires cplusplus
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H
2+
#define TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H
3+
4+
5+
// Typed enum.
6+
enum Color { kRed = 0, kBlue, kGreen, kYellow = 10 };
7+
8+
// Untyped enum.
9+
enum { kOne = 1, kTwo, kThree, kFour };
10+
11+
// enum class.
12+
enum class Pet { goat = 5, cat = 15, dogcow, rabbit };
13+
14+
15+
#endif // TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test that typed and untyped enums cannot be assigned to each other.
2+
3+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
4+
5+
import TypedUntypedEnums
6+
7+
// Implicit type. |x| is an |Int|.
8+
let x = kThree
9+
10+
// Implicit type, |myColor| is a |Color|.
11+
var myColor = kYellow
12+
13+
// These correctly fail. Cannot convert |Int| <-> |Color| (an enum).
14+
myColor = kTwo // expected-error {{cannot assign value of type 'Int' to type 'Color'}}
15+
myColor = x // expected-error {{cannot assign value of type 'Int' to type 'Color'}}
16+
let integer : Int = kBlue // expected-error {{cannot convert value of type 'Color' to specified type 'Int'}}
17+
18+
// These correctly fail. Cannot convert |Int| <-> |Pet| (an enum class).
19+
let animal : Pet = 7 // expected-error {{cannot convert value of type 'Int' to specified type 'Pet'}}
20+
let number : Int = Pet.goat // expected-error {{cannot convert value of type 'Pet' to specified type 'Int'}}

0 commit comments

Comments
 (0)