https://gcc.gnu.org/g:6e4e6883d412ec5f4440a3385df51c90d4108227

commit r16-3721-g6e4e6883d412ec5f4440a3385df51c90d4108227
Author: Jakub Jelinek <[email protected]>
Date:   Tue Sep 9 16:44:48 2025 +0200

    c, c++: Allow &__real__ static_var in constant expressions [PR121678]
    
    When looking at constexpr references, I've noticed staticp handles
    COMPONENT_REFs and ARRAY_REFs (the latter if the index is INTEGER_CST),
    but not {REAL,IMAG}PART_EXPR.  I think that is incorrect and causes
    rejection of constexpr (for C++) or static const (for C) addresses
    of __real__ or __imag__ parts of static vars.
    
    2025-09-09  Jakub Jelinek  <[email protected]>
    
            PR c++/121678
            * tree.cc (staticp): Handle REALPART_EXPR and IMAGPART_EXPR.
    
            * g++.dg/ext/pr121678.C: New test.
            * gcc.dg/pr121678.c: New test.

Diff:
---
 gcc/testsuite/g++.dg/ext/pr121678.C | 7 +++++++
 gcc/testsuite/gcc.dg/pr121678.c     | 7 +++++++
 gcc/tree.cc                         | 4 ++++
 3 files changed, 18 insertions(+)

diff --git a/gcc/testsuite/g++.dg/ext/pr121678.C 
b/gcc/testsuite/g++.dg/ext/pr121678.C
new file mode 100644
index 000000000000..e283ea1c8fc1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/pr121678.C
@@ -0,0 +1,7 @@
+// PR c++/121678
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+static constexpr _Complex double a = 1.0;
+static constexpr double *r = &__real__ a;
+static constexpr double *i = &__imag__ a;
diff --git a/gcc/testsuite/gcc.dg/pr121678.c b/gcc/testsuite/gcc.dg/pr121678.c
new file mode 100644
index 000000000000..afb6c208892e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr121678.c
@@ -0,0 +1,7 @@
+/* PR c++/121678 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+static const _Complex double a = 1.0;
+static const double *const r = &__real__ a;
+static const double *const i = &__imag__ a;
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 905c2d6657fb..2a3ab23f1dc7 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -3935,6 +3935,10 @@ staticp (tree arg)
       else
        return NULL;
 
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+      return staticp (TREE_OPERAND (arg, 0));
+
     case COMPOUND_LITERAL_EXPR:
       return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;

Reply via email to