Skip to content

[clang] Add regression tests for narrowing with is_constant_evaluated. #142885

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

Conversation

efriedma-quic
Copy link
Collaborator

As discussed in #142707, in the context of determining whether a conversion is a narrowing conversion, is_constant_evaluation should be false, even it's a subexpression of a manifestly constant-evaluated expression.

As discussed in llvm#142707, in the context of determining whether a
conversion is a narrowing conversion, is_constant_evaluation should be
false, even it's a subexpression of a manifestly constant-evaluated
expression.
@efriedma-quic efriedma-quic requested a review from zygoloid June 5, 2025 01:39
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jun 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-clang

Author: Eli Friedman (efriedma-quic)

Changes

As discussed in #142707, in the context of determining whether a conversion is a narrowing conversion, is_constant_evaluation should be false, even it's a subexpression of a manifestly constant-evaluated expression.


Full diff: https://github.com/llvm/llvm-project/pull/142885.diff

1 Files Affected:

  • (modified) clang/test/SemaCXX/builtin-is-constant-evaluated.cpp (+11)
diff --git a/clang/test/SemaCXX/builtin-is-constant-evaluated.cpp b/clang/test/SemaCXX/builtin-is-constant-evaluated.cpp
index a1c003c85f732..c775fe71069df 100644
--- a/clang/test/SemaCXX/builtin-is-constant-evaluated.cpp
+++ b/clang/test/SemaCXX/builtin-is-constant-evaluated.cpp
@@ -143,3 +143,14 @@ namespace fold_initializer {
   const float A::f = __builtin_is_constant_evaluated();
   static_assert(fold(A::f == 1.0f));
 }
+
+namespace narrowing {
+  struct X { unsigned u; };
+  constexpr int f(X x) {return x.u;}
+  void g() {
+    static_assert(f({0xFFFFFFFFLL + __builtin_is_constant_evaluated()}) == 0);
+    f({0x100000000LL - __builtin_is_constant_evaluated()}); // expected-error {{constant expression evaluates to 4294967296}} \
+    // expected-warning {{implicit conversion}} \
+    // expected-note {{insert an explicit cast to silence this issue}}
+  }
+}

@efriedma-quic efriedma-quic merged commit f7a3a5c into llvm:main Jun 5, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants