llvm-project/clang/test/Sema/implicit-int-enum-conversion.c
Aaron Ballman b59ab701e9
[C] Handle comma operator for implicit int->enum conversions (#138752)
In C++, the type of an enumerator is the type of the enumeration,
whereas in C, the type of the enumerator is 'int'. The type of a comma
operator is the type of the right-hand operand, which means you can get
an implicit conversion with this code in C but not in C++:
```
  enum E { Zero };
  enum E foo() {
    return ((void)0, Zero);
  }
```
We were previously incorrectly diagnosing this code as being
incompatible with C++ because the type of the paren expression would be
'int' there, whereas in C++ the type is 'E'.

So now we handle the comma operator with special logic when analyzing
implicit conversions in C. When analyzing the left-hand operand of a
comma operator, we do not need to check for that operand causing an
implicit conversion for the entire comma expression. So we only check
for that case with the right-hand operand.

This addresses a concern brought up post-commit:
https://github.com/llvm/llvm-project/pull/137658#issuecomment-2854525259
2025-05-07 10:05:00 -04:00

75 lines
3.1 KiB
C

// RUN: %clang_cc1 -fsyntax-only -verify -Wimplicit-int-enum-cast %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-compat %s
// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ %s
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-implicit-enum-enum-cast %s
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wc++-compat -Wno-implicit-enum-enum-cast -Wno-implicit-int-enum-cast %s
// good-no-diagnostics
enum E1 {
E1_Zero,
E1_One
};
enum E2 {
E2_Zero
};
struct S {
enum E1 e;
} s = { 12 }; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{cannot initialize a member subobject of type 'enum E1' with an rvalue of type 'int'}}
enum E1 foo(void) {
int x;
enum E1 e = 12; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{cannot initialize a variable of type 'enum E1' with an rvalue of type 'int'}}
// Enum to integer is fine.
x = e;
// Integer to enum is not fine.
e = x; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{assigning to 'enum E1' from incompatible type 'int'}}
return x; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{cannot initialize return object of type 'enum E1' with an lvalue of type 'int'}}
}
// Returning with the correct types is fine.
enum E1 bar(void) {
return E1_Zero;
}
// Enum to different-enum conversion is also a C++ incompatibility, but is
// handled via a more general diagnostic, -Wimplicit-enum-enum-cast, which is
// on by default.
enum E1 quux(void) {
enum E1 e1 = E2_Zero; // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \
cxx-error {{cannot initialize a variable of type 'enum E1' with an rvalue of type 'E2'}}
e1 = E2_Zero; // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \
cxx-error {{assigning to 'enum E1' from incompatible type 'E2'}}
return E2_Zero; // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \
cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'E2'}}
}
enum E1 comma1(void) {
return ((void)0, E1_One);
}
enum E1 comma2(void) {
enum E1 x;
return
(x = 12, // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{assigning to 'enum E1' from incompatible type 'int'}}
E1_One);
}
enum E1 comma3(void) {
enum E1 x;
return ((void)0, foo()); // Okay, no conversion in C++
}
enum E1 comma4(void) {
return ((void)1, 2); // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'int'}}
}