]> gcc.gnu.org Git - gcc.git/commitdiff
c: Fix up error-recovery on functions initialized as variables [PR109412]
authorJakub Jelinek <jakub@redhat.com>
Thu, 27 Apr 2023 09:35:55 +0000 (11:35 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 27 Apr 2023 09:35:55 +0000 (11:35 +0200)
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.

2023-04-27  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
gcc/testsuite/gcc.dg/pr109412.c [new file with mode: 0644]

index 7079d4ee145cf7c6b80c64c765973cb24c8a027d..a17879698ece833d328183dbc88856e83e4018f8 100644 (file)
@@ -9374,6 +9374,11 @@ pop_init_level (location_t loc, int implicit,
        {
          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 {}.  */
diff --git a/gcc/testsuite/gcc.dg/pr109412.c b/gcc/testsuite/gcc.dg/pr109412.c
new file mode 100644 (file)
index 0000000..11adce5
--- /dev/null
@@ -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 } */
This page took 0.086932 seconds and 5 git commands to generate.