From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id D8BD6385842C; Thu, 27 Apr 2023 09:41:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D8BD6385842C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682588516; bh=p3IH6cJB5wbJpQhNKhbbjIkSr71V6EmvzhkKt7LKywM=; h=From:To:Subject:Date:From; b=DzaLzdnGX18aYaH4tVq9xqsr6I04rrqzysTUE/KdngU3WwZMuYqSJ2nKhm151oOGj oy25OWRUuC/ULMDaLrgm4scQx7EGjME1ascYJNkr3VSqkd2h0j8q3sakXG6YTUxquk L6gMhjK6PbcjqXnCf5b9XQe1DZ4cecE43ffCof84= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-7257] c: Fix up error-recovery on functions initialized as variables [PR109412] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 910735c5d7ce7607384fc1eec4189e90c8ae5c84 X-Git-Newrev: 7e312adcb70ca7d67f0c2cf238cddec9b3243ff9 Message-Id: <20230427094156.D8BD6385842C@sourceware.org> Date: Thu, 27 Apr 2023 09:41:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7e312adcb70ca7d67f0c2cf238cddec9b3243ff9 commit r13-7257-g7e312adcb70ca7d67f0c2cf238cddec9b3243ff9 Author: Jakub Jelinek Date: Thu Apr 27 11:35:55 2023 +0200 c: Fix up error-recovery on functions initialized as variables [PR109412] 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 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. (cherry picked from commit a1030fbf70eef5b635e4fbb904ec7209ebd137ca) Diff: --- gcc/c/c-typeck.cc | 5 +++++ gcc/testsuite/gcc.dg/pr109412.c | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 7079d4ee145..a17879698ec 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -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 index 00000000000..11adce52509 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr109412.c @@ -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 } */