Repository navigation
Conversation
| // The branches of a conditional operand are each converted to the context | ||
| // type, so a conditional between enumerators of the target type is fine in | ||
| // C++ and must not be diagnosed here either. | ||
| enum E1 comma5(int c) { | ||
| return ((void)0, c ? E1_One : E1_Zero); | ||
| } |
There was a problem hiding this comment.
Can you also add a C++ test?
There was a problem hiding this comment.
the comma path only runs under !S.getLangOpts().CPlusPlus, so this change doesn't reach C++ at all. The third RUN line already compiles this file as C++ and comma5 passes clean there (comma6 keeps its cxx-error), do we still need a separate test then ?
317a581 to
4509c5d
Compare
|
@llvm/pr-subscribers-clang Author: Aditya Medhane (flash1729) ChangesIn C, a conditional between two enumerators has type int, so returning Give Fixes #185400 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b11e1d28a8b8f..6ba63470ecd1b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -474,6 +474,11 @@ features cannot lower the translation-unit ABI level;
dimension that is a zero integer constant, as in `struct Empty vla[n]` or
`int vla[n][0]`. (#GH28328)
+- `-Wimplicit-int-enum-cast` no longer warns about a conditional operator used
+ as the right operand of a comma operator when each branch of the conditional
+ is already of the target enumeration type. The branches are now checked
+ individually, as they are outside of a comma operator. (#GH185400)
+
### Improvements to Clang's time-trace
### Improvements to Coverage Mapping
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f0a1a529841b2..f6f29c1bfaae3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14042,6 +14042,16 @@ static void CheckCommaOperand(
bool ExtraCheckForImplicitConversion,
llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) {
E = E->IgnoreParenImpCasts();
+
+ // A conditional operand is fed into the context type branch by branch, the
+ // same way CheckConditionalOperand does, so that a conditional whose
+ // branches each convert cleanly is not reported through the type of the
+ // conditional as a whole. CheckConditionalOperator analyzes the
+ // subexpressions itself, so do not add this one to the work list.
+ if (ExtraCheckForImplicitConversion && E->getType() != T)
+ if (auto *CO = dyn_cast<AbstractConditionalOperator>(E))
+ return CheckConditionalOperator(S, CO, CC, T);
+
WorkList.push_back({E, CC, false});
if (ExtraCheckForImplicitConversion && E->getType() != T)
diff --git a/clang/test/Sema/implicit-int-enum-conversion.c b/clang/test/Sema/implicit-int-enum-conversion.c
index 36717f36dd083..eb5948e74d3b8 100644
--- a/clang/test/Sema/implicit-int-enum-conversion.c
+++ b/clang/test/Sema/implicit-int-enum-conversion.c
@@ -72,3 +72,21 @@ 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'}}
}
+
+// The branches of a conditional operand are each converted to the context
+// type, so a conditional between enumerators of the target type is fine in
+// C++ and must not be diagnosed here either.
+enum E1 comma5(int c) {
+ return ((void)0, c ? E1_One : E1_Zero); // Okay, no conversion in C++
+}
+
+#ifdef __cplusplus
+// In C++ the enumerators already have the enumeration type, so the conditional
+// has type E1 and there is nothing to convert.
+static_assert(__is_same(decltype(true ? E1_One : E1_Zero), E1), "");
+#endif
+
+enum E1 comma6(int c) {
+ return ((void)0, c ? E1_One : 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'}}
+}
|
A conditional operator appearing as the right operand of a comma operator was converted to the context type as a whole. In C the type of a conditional between two enumerators is int, so returning '(void)0, c ? E1_One : E1_Zero' from a function returning that enumeration was reported as an int to enum conversion, even though the same code is valid in C++. Recurse into the conditional so each branch is checked against the context type, matching what CheckConditionalOperand already does outside of a comma operator. A branch that genuinely needs the conversion is still diagnosed. Fixes llvm#185400
4d557d9 to
55ba74c
Compare
|
@flash1729 Please disclose any use of AI |
Apologies for that, I should have mentioned this earlier. I used Claude Code to walk through the issue and work out where the fix belonged. I wrote it up, built it, and ran the tests myself, and I'm happy to answer questions about any part of it. I'll add an Assisted-by note to the PR description. little heads up -- my university exams have started, so I'll get to the remaining reviews once they're done, in about two weeks. |
In C, a conditional between two enumerators has type int, so returning
(side(), c ? A : B)from a function returning that enum made-Wimplicit-int-enum-castclaim the code is invalid in C++, even thoughcompiling it as C++ accepts it. The comma path converted the whole
conditional to the context type, while
CheckConditionalOperandalreadychecks conditionals branch by branch.
Give
CheckCommaOperandthe same behavior: recurse into a conditionalright operand so each branch is checked individually. A branch that
genuinely needs the conversion still warns.
Fixes #185400