Skip to content

[cxx-interop] Test typed and untyped enums cannot be assigned to each other #61639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/Interop/Cxx/enum/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ module CenumsWithOptionsOmit {
module CenumsNSOptions {
header "c-enums-NS_OPTIONS.h"
requires cplusplus
}
}

module TypedUntypedEnums {
header "typed-untyped-enums.h"
requires cplusplus
}
15 changes: 15 additions & 0 deletions test/Interop/Cxx/enum/Inputs/typed-untyped-enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H
#define TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H


// Typed enum.
enum Color { kRed = 0, kBlue, kGreen, kYellow = 10 };

// Untyped enum.
enum { kOne = 1, kTwo, kThree, kFour };

// enum class.
enum class Pet { goat = 5, cat = 15, dogcow, rabbit };


#endif // TEST_INTEROP_CXX_ENUM_INPUTS_TYPED_UNTYPED_ENUMS_H
20 changes: 20 additions & 0 deletions test/Interop/Cxx/enum/typed-untyped-enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Test that typed and untyped enums cannot be assigned to each other.

// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop

import TypedUntypedEnums

// Implicit type. |x| is an |Int|.
let x = kThree

// Implicit type, |myColor| is a |Color|.
var myColor = kYellow

// These correctly fail. Cannot convert |Int| <-> |Color| (an enum).
myColor = kTwo // expected-error {{cannot assign value of type 'Int' to type 'Color'}}
myColor = x // expected-error {{cannot assign value of type 'Int' to type 'Color'}}
let integer : Int = kBlue // expected-error {{cannot convert value of type 'Color' to specified type 'Int'}}

// These correctly fail. Cannot convert |Int| <-> |Pet| (an enum class).
let animal : Pet = 7 // expected-error {{cannot convert value of type 'Int' to specified type 'Pet'}}
let number : Int = Pet.goat // expected-error {{cannot convert value of type 'Pet' to specified type 'Int'}}