[clang] Don't wrap immediate invocations in ConstantExprs within constexpr initializers (PR #89565)

Daniel M. Katz via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 23 07:00:13 PDT 2024


================
@@ -244,8 +244,8 @@ namespace UndefinedBehavior {
     constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
     constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
     constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
-    constexpr signed char c1 = 100 * 2; // ok expected-warning{{changes value}}
-    constexpr signed char c2 = '\x64' * '\2'; // also ok  expected-warning{{changes value}}
+    constexpr signed char c1 = 100 * 2; // ok - no error from changing value because initializer is constexpr.
----------------
katzdm wrote:

@erichkeane I think that it isn't, because `100`, `2`, and `100 * 2` are all expressions of type `int`. The only sideways thing that happens is the implicit conversion from `int` to `signed char` that reinterprets the value as `56`.

Both this diagnostic and the other that you commented on can be recovered by changing [this](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaChecking.cpp#L16543-L16547) `Sema::DiagRuntimeBehavior` to `Sema::Diag` instead.

Running this change with `check-clang`, we recover the following diagnostics that are currently removed by this PR:
- clang/test/AST/Interp/intap.cpp
- clang/test/CXX/expr/expr.const/p2-0x.cpp
- clang/test/SemaCXX/enum-scoped.cpp
- clang/test/SemaCXX/ext-int.cpp

with no other tests newly failing.

Let me know if you think this change makes sense; I'll push it to the PR and revert the changes to the tests mentioned above. Also let me know if it's desirable for any of the other diagnostics removed by this PR to be kept; happy to dig.

https://github.com/llvm/llvm-project/pull/89565


More information about the cfe-commits mailing list