public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: "Joseph S. Myers" <joseph@codesourcery.com>,
	Marek Polacek <polacek@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c: Fix up error-recovery on functions initialized as variables [PR109412]
Date: Fri, 14 Apr 2023 21:15:40 +0200	[thread overview]
Message-ID: <ZDmmXMae9glt4ABn@tucnak> (raw)

Hi!

The change to allow empty initializers in C broke error-recovery on the
following testcase.  We are emitting function %qD is initialized like a
variable error early; if the initializer is non-empty, we just emit
another error that the initializer is invalid.  Previously if it was empty,
we'd emit another error that scalar is being initialized by empty
initializer (not really correct), but now we instead just try to
build_zero_cst for the FUNCTION_TYPE and ICE on it.

The following patch just emits the same diagnostics for the empty
initializers as we emit for the non-empty ones.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2023-04-14  Jakub Jelinek  <jakub@redhat.com>

	PR c/107682
	PR c/109412
	* c-typeck.cc (pop_init_level): If constructor_type is FUNCTION_TYPE,
	reject empty initializer as invalid.

	* gcc.dg/pr109412.c: New test.

--- gcc/c/c-typeck.cc.jj	2023-03-29 08:32:35.747720324 +0200
+++ gcc/c/c-typeck.cc	2023-04-13 11:37:30.353762300 +0200
@@ -9374,6 +9374,11 @@ pop_init_level (location_t loc, int impl
 	{
 	  if (constructor_erroneous || constructor_type == error_mark_node)
 	    ret.value = error_mark_node;
+	  else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
+	    {
+	      error_init (loc, "invalid initializer");
+	      ret.value = error_mark_node;
+	    }
 	  else if (TREE_CODE (constructor_type) == POINTER_TYPE)
 	    /* Ensure this is a null pointer constant in the case of a
 	       'constexpr' object initialized with {}.  */
--- gcc/testsuite/gcc.dg/pr109412.c.jj	2023-04-13 12:06:27.820694502 +0200
+++ gcc/testsuite/gcc.dg/pr109412.c	2023-04-13 12:06:07.561986811 +0200
@@ -0,0 +1,20 @@
+/* PR c/107682 */
+/* PR c/109412 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+char bar () = {};	/* { dg-error "function 'bar' is initialized like a variable" } */
+			/* { dg-error "invalid initializer" "" { target *-*-* } .-1 } */
+			/* { dg-message "near initialization for 'bar'" "" { target *-*-* } .-2 } */
+char baz () = { 1 };	/* { dg-error "function 'baz' is initialized like a variable" } */
+			/* { dg-error "invalid initializer" "" { target *-*-* } .-1 } */
+			/* { dg-message "near initialization for 'baz'" "" { target *-*-* } .-2 } */
+void
+foo ()
+{
+  int qux () = {};	/* { dg-error "function 'qux' is initialized like a variable" } */
+			/* { dg-error "invalid initializer" "" { target *-*-* } .-1 } */
+			/* { dg-message "near initialization for 'qux'" "" { target *-*-* } .-2 } */
+  int corge () = { 1 };	/* { dg-error "function 'corge' is initialized like a variable" } */
+			/* { dg-error "invalid initializer" "" { target *-*-* } .-1 } */
+}			/* { dg-message "near initialization for 'corge'" "" { target *-*-* } .-2 } */

	Jakub


             reply	other threads:[~2023-04-14 19:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-14 19:15 Jakub Jelinek [this message]
2023-04-24 20:47 ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZDmmXMae9glt4ABn@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).