From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1D3AD3858C5E for ; Fri, 14 Apr 2023 19:15:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D3AD3858C5E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681499751; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=LVeZvsXnuKSJBJFhZbbX8puDEldTNg0MYVp0xrGjyR8=; b=BN3rK49/eXkIGbfJ9SNdUOCCb88GNcvAQRiiiYmsUWy1OHcJuveMKsXHUSWo78b2fRzhpv njrg1o9GAiZdasSQOpJLQVVd9Tr3oFmg28aHLRSf14WfVfmuhgb41Y9QgnKTCuEtTnSc7z YE6DMVJKY98jj+IpgnIvLV7W/RVcVAA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-562-xcVPiA7xPtyO6Glx-nq8Cw-1; Fri, 14 Apr 2023 15:15:49 -0400 X-MC-Unique: xcVPiA7xPtyO6Glx-nq8Cw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 87B5E8996E2; Fri, 14 Apr 2023 19:15:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F81DC16028; Fri, 14 Apr 2023 19:15:48 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 33EJFfLr1939094 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 21:15:41 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 33EJFeSZ1939092; Fri, 14 Apr 2023 21:15:40 +0200 Date: Fri, 14 Apr 2023 21:15:40 +0200 From: Jakub Jelinek To: "Joseph S. Myers" , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Fix up error-recovery on functions initialized as variables [PR109412] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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