https://gcc.gnu.org/g:a790828ccb3b06a7771f871651e7b54d13c3a168
commit r15-2695-ga790828ccb3b06a7771f871651e7b54d13c3a168 Author: Marek Polacek <pola...@redhat.com> Date: Thu Aug 1 11:32:26 2024 -0400 c++: DR882, main cannot be deleted [PR116169] 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. Diff: --- gcc/cp/decl.cc | 26 ++++++++++++++++++-------- gcc/testsuite/g++.dg/DRs/dr882.C | 5 +++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 279af21eed03..687ae6937f53 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -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 index 000000000000..c41cf7416a29 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr882.C @@ -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" }