-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[Clang] Do not perform integral promotion if operands of expression x ? : y have the same type
#108837
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-clang Author: Yanzuo Liu (zwuis) ChangesAccording to [[expr.cond]/5](https://eel.is/c++draft/expr.cond#5) and [[expr.cond]/7.1](https://eel.is/c++draft/expr.cond#7.1), no conversion should be performed to compute result type if middle operand and right operand have the same type regardless of their value catagories. Fixes #15998. Full diff: https://github.com/llvm/llvm-project/pull/108837.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 17ec1fe0b946de..db8a7568a12114 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -389,6 +389,8 @@ Bug Fixes to C++ Support
- Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
pack. #GH63819, #GH107560
- Fix a crash when a static assert declaration has an invalid close location. (#GH108687)
+- Fixed a bug in computing result type of conditional operator with omitted middle operand
+ (a GNU extension). (#GH15998)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8f3e15cc9a9bb7..8414a55e4868b9 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8785,11 +8785,9 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
commonExpr = result.get();
}
// We usually want to apply unary conversions *before* saving, except
- // in the special case of a C++ l-value conditional.
+ // in the special case in C++ that operands have the same type.
if (!(getLangOpts().CPlusPlus
&& !commonExpr->isTypeDependent()
- && commonExpr->getValueKind() == RHSExpr->getValueKind()
- && commonExpr->isGLValue()
&& commonExpr->isOrdinaryOrBitFieldObject()
&& RHSExpr->isOrdinaryOrBitFieldObject()
&& Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) {
diff --git a/clang/test/SemaCXX/conditional-gnu-ext.cpp b/clang/test/SemaCXX/conditional-gnu-ext.cpp
new file mode 100644
index 00000000000000..83a6fff8467863
--- /dev/null
+++ b/clang/test/SemaCXX/conditional-gnu-ext.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter
+// expected-no-diagnostics
+
+/*
+FIXME: Support constexpr
+
+constexpr int evaluate_once(int x) {
+ return (++x) ? : 10;
+}
+static_assert(evaluate_once(0) == 1, "");
+*/
+
+namespace GH15998 {
+ enum E { Zero, One };
+ E test(E e) {
+ return e ? : One;
+ }
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
| @@ -0,0 +1,19 @@ | |||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test should go in clang/test/Sema/conditional-expr.c and we should add a section of the GNU extension there.
I am also a bit concerned that we don't have a specific set of sema tests for the GNU form of the conditional expression. So it makes me much left confident that we will catch any breaks with this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I too am very concerned about the lack of tests here, there are some disappearing conditions I cannot help but think are there for a good reason, and would like to see some study as to why those where there and when.
I'm also concerned about the constexpr support being 'FIXME', it seems that this should just 'work' unless something odd is happening.
| if (!(getLangOpts().CPlusPlus | ||
| && !commonExpr->isTypeDependent() | ||
| && commonExpr->getValueKind() == RHSExpr->getValueKind() | ||
| && commonExpr->isGLValue() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I don't see how losing the above two value kind exceptions fix this? Can you explain this better?
| @@ -0,0 +1,19 @@ | |||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s | |||
| // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should run with BOTH constexpr interpreters.
According to [expr.cond]/5 and [expr.cond]/7.1, no conversion should be performed to compute result type of conditional operator if middle operand and right operand have the same type regardless of their value catagories.
Fixes #15998.