c++: DR882, main cannot be deleted [PR116169]

Message ID 20240801202019.3945236-1-polacek@redhat.com
State Committed
Commit a790828ccb3b06a7771f871651e7b54d13c3a168
Headers
Series c++: DR882, main cannot be deleted [PR116169] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed

Commit Message

Marek Polacek Aug. 1, 2024, 8:20 p.m. UTC
  Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
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                   | 26 ++++++++++++++++++--------
 gcc/testsuite/g++.dg/DRs/dr882.C |  5 +++++
 2 files changed, 23 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/DRs/dr882.C


base-commit: c5ccdfdcab0b24afba2a661af861bec1d63f0595
  

Comments

Jason Merrill Aug. 1, 2024, 9:19 p.m. UTC | #1
On 8/1/24 4:20 PM, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> -- >8 --
> 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                   | 26 ++++++++++++++++++--------
>   gcc/testsuite/g++.dg/DRs/dr882.C |  5 +++++
>   2 files changed, 23 insertions(+), 8 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/DRs/dr882.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 279af21eed0..687ae6937f5 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 00000000000..c41cf7416a2
> --- /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" }
> 
> base-commit: c5ccdfdcab0b24afba2a661af861bec1d63f0595
  

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 279af21eed0..687ae6937f5 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 00000000000..c41cf7416a2
--- /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" }