]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: DR882, main cannot be deleted [PR116169]
authorMarek Polacek <polacek@redhat.com>
Thu, 1 Aug 2024 15:32:26 +0000 (11:32 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 2 Aug 2024 14:13:46 +0000 (10:13 -0400)
This DR clarifies that "int main() = delete;" is ill-formed.

PR c++/116169

gcc/cp/ChangeLog:

* decl.cc (cp_finish_decl): Disallow deleting ::main.

gcc/testsuite/ChangeLog:

* g++.dg/DRs/dr882.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/DRs/dr882.C [new file with mode: 0644]

index 279af21eed036e3c9a85936950c9b443dac96d96..687ae6937f53968733f32c59a26bc99de8307f47 100644 (file)
@@ -8649,15 +8649,25 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
              && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
        {
          /* FIXME check this is 1st decl.  */
-         DECL_DELETED_FN (decl) = 1;
-         DECL_DECLARED_INLINE_P (decl) = 1;
-         DECL_INITIAL (decl)
-           = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
-         FOR_EACH_CLONE (clone, decl)
+         if (UNLIKELY (DECL_MAIN_P (decl)))
            {
-             DECL_DELETED_FN (clone) = 1;
-             DECL_DECLARED_INLINE_P (clone) = 1;
-             DECL_INITIAL (clone) = DECL_INITIAL (decl);
+             /* [basic.start.main]/3: A program that defines main as deleted
+                is ill-formed.  */
+             error ("%<::main%> cannot be deleted");
+             DECL_INITIAL (decl) = NULL_TREE;
+           }
+         else
+           {
+             DECL_DELETED_FN (decl) = 1;
+             DECL_DECLARED_INLINE_P (decl) = 1;
+             DECL_INITIAL (decl)
+               = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
+             FOR_EACH_CLONE (clone, decl)
+               {
+                 DECL_DELETED_FN (clone) = 1;
+                 DECL_DECLARED_INLINE_P (clone) = 1;
+                 DECL_INITIAL (clone) = DECL_INITIAL (decl);
+               }
            }
          init = NULL_TREE;
        }
diff --git a/gcc/testsuite/g++.dg/DRs/dr882.C b/gcc/testsuite/g++.dg/DRs/dr882.C
new file mode 100644 (file)
index 0000000..c41cf74
--- /dev/null
@@ -0,0 +1,5 @@
+// DR882 - Defining main as deleted
+// PR c++/116169
+// { dg-do compile { target c++11 } }
+
+int main() = delete; // { dg-error "cannot be deleted" }