c: Diagnose compound literals with function type [PR108043]
authorJakub Jelinek <jakub@redhat.com>
Mon, 19 Dec 2022 19:55:56 +0000 (20:55 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 19 Dec 2022 19:55:56 +0000 (20:55 +0100)
Both C99 and latest C2X say that compound literal shall have an object type
(complete object type in the latter case) or array of unknown bound,
so complit with function type is invalid.  When the initializer had to be
non-empty for such case, we used to diagnose it as incorrect initializer,
but with (fntype){} now allowed we just ICE on it.

The following patch diagnoses that.

2022-12-19  Jakub Jelinek  <jakub@redhat.com>

PR c/108043
* c-parser.cc (c_parser_postfix_expression_after_paren_type): Diagnose
compound literals with function type.

* gcc.dg/pr108043.c: New test.
* gcc.dg/c99-complit-2.c (foo): Adjust expected diagnostics for
complit with function type.

gcc/c/c-parser.cc
gcc/testsuite/gcc.dg/c99-complit-2.c
gcc/testsuite/gcc.dg/pr108043.c [new file with mode: 0644]

index 1bbb39f9b08749a48fc8cf88facb67d4aeb70763..7d6960fffbb6775b5620f44c4c7002c41454ab4f 100644 (file)
@@ -10924,6 +10924,11 @@ c_parser_postfix_expression_after_paren_type (c_parser *parser,
       error_at (type_loc, "compound literal has variable size");
       type = error_mark_node;
     }
+  else if (TREE_CODE (type) == FUNCTION_TYPE)
+    {
+      error_at (type_loc, "compound literal has function type");
+      type = error_mark_node;
+    }
   if (constexpr_p && type != error_mark_node)
     {
       tree type_no_array = strip_array_types (type);
index 8120ce8d988ecf6c53db59ca3358c4c93994943c..5e1b5be8e282bcc697786c2c4336c7c9a4c56dcb 100644 (file)
@@ -23,7 +23,7 @@ foo (int a)
   /* { dg-error "init" "incomplete union type" { target *-*-* } .-1 } */
   /* { dg-error "invalid use of undefined type" "" { target *-*-* } .-2 } */
   (void (void)) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
-  /* { dg-error "init" "function type" { target *-*-* } .-1 } */
+  /* { dg-error "compound literal has function type" "function type" { target *-*-* } .-1 } */
   (int [a]) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
   /* { dg-error "init|variable" "VLA type" { target *-*-* } .-1 } */
   /* Initializers must not attempt to initialize outside the object
diff --git a/gcc/testsuite/gcc.dg/pr108043.c b/gcc/testsuite/gcc.dg/pr108043.c
new file mode 100644 (file)
index 0000000..0cc0700
--- /dev/null
@@ -0,0 +1,12 @@
+/* PR c/108043 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+typedef void F (void);
+
+void
+foo (void)
+{
+  (F) {};              /* { dg-error "compound literal has function type" } */
+  (F) { foo };         /* { dg-error "compound literal has function type" } */
+}