[clang] [clang][bytecode] Fix some imag/real corner cases (PR #174764)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 7 04:54:53 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Fix real/imag when taking a primitive parameter _and_ being discarded, and fix the case where their subexpression can't be classified.

Fixes https://github.com/llvm/llvm-project/issues/174668

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+7-3) 
- (modified) clang/test/AST/ByteCode/complex.cpp (+26) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index b4449def1c6f0..a539f31db0ca6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6792,13 +6792,17 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
       return false;
     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
   case UO_Real: // __real x
-    assert(T);
+    if (!T)
+      return false;
     return this->delegate(SubExpr);
   case UO_Imag: { // __imag x
-    assert(T);
+    if (!T)
+      return false;
     if (!this->discard(SubExpr))
       return false;
-    return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
+    return DiscardResult
+               ? true
+               : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
   }
   case UO_Extension:
     return this->delegate(SubExpr);
diff --git a/clang/test/AST/ByteCode/complex.cpp b/clang/test/AST/ByteCode/complex.cpp
index be10b3cfa53da..182162d251ece 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -410,3 +410,29 @@ namespace ComplexConstexpr {
   static_assert(__imag test6 == 6, "");
   static_assert(&__imag test6 == &__real test6 + 1, "");
 }
+
+namespace Discard {
+  constexpr int test1() {
+    __imag(0);
+    __imag(0.0);
+    __real(0);
+    __real(0.0);
+
+    return 10;
+  }
+  static_assert(test1() == 10, "");
+
+  constexpr int test2() {
+    __imag(bar()); // both-error {{use of undeclared identifier}}
+    return 10;
+  }
+  static_assert(test2() == 10, ""); // both-error {{not an integral constant expression}}
+
+  constexpr int test3() {
+    __real(barz()); // both-error {{use of undeclared identifier}}
+    return 10;
+  }
+  static_assert(test3() == 10, ""); // both-error {{not an integral constant expression}}
+
+
+}

``````````

</details>


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


More information about the cfe-commits mailing list