]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with deleted main [PR120338]
authorMarek Polacek <polacek@redhat.com>
Wed, 15 Apr 2026 18:15:06 +0000 (14:15 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 15 Apr 2026 22:31:38 +0000 (18:31 -0400)
Here we ICE with deleted main which is later redefined at

  gcc_assert (DECL_SAVED_TREE (fn));

because for some reason when deleting main I chose NULL_TREE instead of
error_mark_node as its DECL_INITIAL.  For other deleted functions we
also use error_mark_node so that

  void foo() = delete;
  void foo() {}

results in the "redefinition of" error.  Let's do the same for main, then.

PR c++/120338

gcc/cp/ChangeLog:

* decl.cc (cp_finish_decl): Set DECL_INITIAL to error_mark_node
instead of NULL_TREE for deleted main.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/deleted19.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp0x/deleted19.C [new file with mode: 0644]

index 7e40226d739186fe48d59f49371c6bfc395d3041..679a8007ca6f02fc84a8c4df31e60dd9b310bda6 100644 (file)
@@ -9649,7 +9649,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
              /* [basic.start.main]/3: A program that defines main as deleted
                 is ill-formed.  */
              error ("%<::main%> cannot be deleted");
-             DECL_INITIAL (decl) = NULL_TREE;
+             DECL_INITIAL (decl) = error_mark_node;
            }
          else
            {
diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted19.C b/gcc/testsuite/g++.dg/cpp0x/deleted19.C
new file mode 100644 (file)
index 0000000..c3f8f06
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/120338
+// { dg-do compile { target c++11 } }
+
+int main() = delete;  // { dg-error "cannot be deleted" }
+int main() {}        // { dg-error "redefinition" }