https://gcc.gnu.org/g:2b37c9973967230b01dc5e38770fb3ec84610555

commit r13-9423-g2b37c9973967230b01dc5e38770fb3ec84610555
Author: Iain Buclaw <ibuc...@gdcproject.org>
Date:   Tue Mar 11 17:56:18 2025 +0100

    d: Fix regression returning from function with invariants [PR119139]
    
    An optimization was added in GDC-12 which sets the TREE_READONLY flag on
    all local variables with the storage class `const' assigned.  For some
    reason, const is also being added by the front-end to `__result'
    variables in non-virtual functions, which ends up getting wrong code by
    the gimplify pass promoting the local to static storage.
    
    A bug has been raised upstream, as this looks like an error in the AST.
    For now, turn off setting TREE_READONLY on all result variables.
    
            PR d/119139
    
    gcc/d/ChangeLog:
    
            * decl.cc (get_symbol_decl): Don't set TREE_READONLY for __result
            declarations.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/pr119139.d: New test.
    
    (cherry picked from commit 81582ca6cb692098c1bda7995aec46c6cbfbfcb3)

Diff:
---
 gcc/d/decl.cc                   |  2 +-
 gcc/testsuite/gdc.dg/pr119139.d | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc
index 84274b3f3c31..deba3335a6a8 100644
--- a/gcc/d/decl.cc
+++ b/gcc/d/decl.cc
@@ -1313,7 +1313,7 @@ get_symbol_decl (Declaration *decl)
       /* `const` applies to data that cannot be changed by the const reference
         to that data. It may, however, be changed by another reference to that
         same data.  */
-      if (vd->isConst () && !vd->isDataseg ())
+      if (vd->isConst () && !vd->isResult () && !vd->isDataseg ())
        TREE_READONLY (decl->csym) = 1;
     }
 
diff --git a/gcc/testsuite/gdc.dg/pr119139.d b/gcc/testsuite/gdc.dg/pr119139.d
new file mode 100644
index 000000000000..dc42c411e390
--- /dev/null
+++ b/gcc/testsuite/gdc.dg/pr119139.d
@@ -0,0 +1,24 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119139
+// { dg-do compile }
+// { dg-options "-fdump-tree-gimple" }
+string toString()
+{
+    return "1";
+}
+
+struct B
+{
+    ulong n;
+
+    invariant{}
+
+    string str()
+    {
+        if (n == 0)
+        {
+            return "0";
+        }
+        return toString();
+    }
+}
+// { dg-final { scan-tree-dump-not "static const struct  __result =" "gimple" 
} }

Reply via email to