https://gcc.gnu.org/g:5a696df0da45397886341fee4618720384798259

commit r17-5596-g5a696df0da45397886341fee4618720384798259
Author: Gaius Mulley <[email protected]>
Date:   Mon Oct 26 20:46:41 2026 +0000

    PR modula2/127991: Fix tree-dump crash on anonymous constants
    
    m2decl_DeclareKnownConstant creates an IDENTIFIER_NODE without setting
    its string, even though the constant is intended to be anonymous.  Tree
    dumps treat the nonnull DECL_NAME as printable and dereference its null
    string, causing a crash for anonymous set constants.
    
    Build the CONST_DECL with a NULL_TREE name instead.  The dumper then
    uses the declaration's generated name.  Add a dump regression test.
    
    The test case was found by a non-AI automated testing tool.
    The investigation was assisted by OpenAI Codex 5.6 Sol and Claude.
    
    gcc/m2/ChangeLog:
    
            PR modula2/127991
            * gm2-gcc/m2decl.cc (m2decl_DeclareKnownConstant): Build the
            anonymous constant declaration without a name.
    
    gcc/testsuite/ChangeLog:
    
            PR modula2/127991
            * gm2.dg/dump/pim/pass/anonymousconst.mod: New test.
            * gm2.dg/dump/pim/pass/dg-dump-pim-pass.exp: New test.
    
            Authored-by: Yongqiang Tian <[email protected]>
    Signed-off-by: Gaius Mulley <[email protected]>

Diff:
---
 gcc/m2/gm2-gcc/m2decl.cc                                |  3 +--
 gcc/testsuite/gm2.dg/dump/pim/pass/anonymousconst.mod   | 13 +++++++++++++
 gcc/testsuite/gm2.dg/dump/pim/pass/dg-dump-pim-pass.exp | 15 +++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/m2/gm2-gcc/m2decl.cc b/gcc/m2/gm2-gcc/m2decl.cc
index 966b22cf776a..095fba0d4a72 100644
--- a/gcc/m2/gm2-gcc/m2decl.cc
+++ b/gcc/m2/gm2-gcc/m2decl.cc
@@ -142,7 +142,6 @@ m2decl_DeclareKnownVariable (location_t location, const 
char *name, tree type,
 tree
 m2decl_DeclareKnownConstant (location_t location, tree type, tree value)
 {
-  tree id = make_node (IDENTIFIER_NODE); /* Ignore the name of the constant. */
   tree decl;
 
   m2assert_AssertLocation (location);
@@ -150,7 +149,7 @@ m2decl_DeclareKnownConstant (location_t location, tree 
type, tree value)
   type = m2tree_skip_type_decl (type);
   layout_type (type);
 
-  decl = build_decl (location, CONST_DECL, id, type);
+  decl = build_decl (location, CONST_DECL, NULL_TREE, type);
 
   value = copy_node (value);
   TREE_TYPE (value) = type;
diff --git a/gcc/testsuite/gm2.dg/dump/pim/pass/anonymousconst.mod 
b/gcc/testsuite/gm2.dg/dump/pim/pass/anonymousconst.mod
new file mode 100644
index 000000000000..f19449fa14d7
--- /dev/null
+++ b/gcc/testsuite/gm2.dg/dump/pim/pass/anonymousconst.mod
@@ -0,0 +1,13 @@
+(* { dg-do compile } *)
+(* { dg-options "-O0 -fdump-tree-optimized" } *)
+
+MODULE anonymousconst ;
+TYPE
+   large = SET OF [0..1023] ;
+PROCEDURE Check (a: large) ;
+END Check ;
+BEGIN
+   Check(large{2, 3, 4})
+END anonymousconst.
+
+(* { dg-final { scan-tree-dump "Check \\(C\\." "optimized" } } *)
diff --git a/gcc/testsuite/gm2.dg/dump/pim/pass/dg-dump-pim-pass.exp 
b/gcc/testsuite/gm2.dg/dump/pim/pass/dg-dump-pim-pass.exp
new file mode 100644
index 000000000000..086b175bd69a
--- /dev/null
+++ b/gcc/testsuite/gm2.dg/dump/pim/pass/dg-dump-pim-pass.exp
@@ -0,0 +1,15 @@
+# Compile tests, no torture testing.
+
+# Load support procs.
+load_lib gm2-dg.exp
+
+gm2_init_pim4 $srcdir/$subdir
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.mod]] "" ""
+
+# All done.
+dg-finish

Reply via email to