From patchwork Thu Jul 20 14:08:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 123309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp3145107vqt; Thu, 20 Jul 2023 07:09:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlEQvnd03IUOrUSm3qAwLmKEO7oeJz9dLSE1m4E3BSfV6HdvNFM8cULlvAbgQ/aiumGAwJ8a X-Received: by 2002:a17:906:7810:b0:982:2586:f85 with SMTP id u16-20020a170906781000b0098225860f85mr2357581ejm.65.1689862147795; Thu, 20 Jul 2023 07:09:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689862147; cv=none; d=google.com; s=arc-20160816; b=HrSs3kL5sMWqNp8Gz+V6Qmyn/fc1ORrw1nPU8zoH8PqPTRRrRnBmsUjpi4f8kMK6+y unpGHmwAjP4jouNXDACgLMS/VvZm/eIaFjkFC2PnucgOWgVIUrjoMLV57SSbSVr3FIGM D5i+Ccab+jj8oYNVaCi3JVl5GYdpsw4jF0nqoyaOpcpKi3B0du7fY0mwEUfF+24cB4He M3TA6SdMkJ/1qckUycr7HRY1YKyOYb1xkmFm8mFixEd9GPfXZY3fQ63lWF0YK4agdgP2 BG9E2WchRdrRGnGS14WFNNPxWAjqFDPuplJj+NV2LcH4tSNLe2N/jV82gN/RWyNCqRKn AF4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=mk3SIdJaqxyLa9it5CXPwS5jU4ksZWDUatgDXXeYtwU=; fh=QRO9BFZjo+7Uu83oh+aqWPLEBuPqskWEssO1Fg9xdSI=; b=yS/16efwlFrnd3KYSz61HVG23axgwZPj+bF9c34Qazm6PLfF73A5mLdQLhjcNbcsZq XZXViywf7/ESbEvrm9sDDWbDbQ5U6CX+QGUvJUFAXH9u1ikDYVePZXYkW2KMbFmTeqCQ t1j7I6DSUqjscS958aBXotRKSy0yUmBsDRDhUDTD4fWjVVj5fxNXIHeJ7tS70to7vJKZ 2Z7RcKtdlGTmDQ7+jC1k6WSNUV5S8UIRb1FynTGfnWISmcAZVJ2Dxx0FYw0r8/Th/2bq UtCpEQW9ycQZ8lMb9jICyxTyYByWZqgFiQ+WHgqoAoplyeecYkGDoeUWKgExMRDlqIU5 4XgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=GhuKfy9q; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w21-20020a170906131500b00997ba1c6f3dsi727376ejb.403.2023.07.20.07.09.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:09:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=GhuKfy9q; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B5173856948 for ; Thu, 20 Jul 2023 14:09:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B5173856948 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689862146; bh=mk3SIdJaqxyLa9it5CXPwS5jU4ksZWDUatgDXXeYtwU=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=GhuKfy9q7O3K0LD6YbvUJbZAPMjeDjk2dxq0b8MHs5UzF9ZFDJYcdRI6ns8/nZcaI dzod5mDDixbhnyBFskXHEqK6n+iJ+JttVOgLtzCY5ZlE7oj6kPX5m7gdOo7cqAey7x uc0wwmGRVbxDy/yt7aIZR3skckVo2IYTnyVE8U0o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 6339D3858C2B for ; Thu, 20 Jul 2023 14:08:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6339D3858C2B Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-494-EWBiP_vMM_yZ2Wj4KFYzuw-1; Thu, 20 Jul 2023 10:08:19 -0400 X-MC-Unique: EWBiP_vMM_yZ2Wj4KFYzuw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-635eeb952b8so11003186d6.3 for ; Thu, 20 Jul 2023 07:08:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689862098; x=1690466898; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mk3SIdJaqxyLa9it5CXPwS5jU4ksZWDUatgDXXeYtwU=; b=COu4P2b4KJjPxGRpGlJbzM7qxrfTZu0/Dpn2rHvFOAKKy8mZBqpTr90w7eQdroFYV+ JRVFGqxZIG9ksG6oPJhvunNxtttYUAGRs9S2I37r5mjod6oLwM1NgCVrAbHkf141qNzG xFQKwau7u3lmvN23+sERsdX8m3DHvSGAfBHhZ59z9oDOPBI3PXQwMUHxfnZEU57JlkWS o8OQkEM//68t3PODa3FqwzjhDZExkMDtsPfwkdtjK1AmV7goHI7b0TXQHEfQMBbp7VZ/ 2/IdF40SMO38+/R5Cuw232KhU1mi2e3TfnHFqoHzPUqTnvEP7CYMi/ucoHxxlSjMQ+kQ ysKQ== X-Gm-Message-State: ABy/qLbeR2xlyZ6fmxinxpBYAgNLY9Nm8wHavRmVEEq+pExvQ7U8FCfE xROxu+4k/N9YrliRIwKUwkVGrOrmSMkoZTvL+zQF7byL9lLIt2DAI9M/1qNzDMlBpm+Q0+1PHrn jK1GMgFlOy83clwUfZA== X-Received: by 2002:a0c:eb49:0:b0:630:198c:3c60 with SMTP id c9-20020a0ceb49000000b00630198c3c60mr21940315qvq.23.1689862097835; Thu, 20 Jul 2023 07:08:17 -0700 (PDT) X-Received: by 2002:a0c:eb49:0:b0:630:198c:3c60 with SMTP id c9-20020a0ceb49000000b00630198c3c60mr21940296qvq.23.1689862097578; Thu, 20 Jul 2023 07:08:17 -0700 (PDT) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id x20-20020a0cb214000000b00626362f1bf1sm370392qvd.63.2023.07.20.07.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 07:08:17 -0700 (PDT) Date: Thu, 20 Jul 2023 10:08:15 -0400 To: Jason Merrill Cc: Patrick Palka , GCC Patches Subject: [PATCH v2] c++: fix ICE with designated initializer [PR110114] Message-ID: References: <20230719180053.46101-1-polacek@redhat.com> <3efd2e0c-1c18-de91-4708-dfd1892ecb11@idea> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771872933789515169 X-GMAIL-MSGID: 1771948891325732928 On Wed, Jul 19, 2023 at 03:24:10PM -0400, Jason Merrill wrote: > On 7/19/23 14:38, Marek Polacek wrote: > > On Wed, Jul 19, 2023 at 02:32:15PM -0400, Patrick Palka wrote: > > > On Wed, 19 Jul 2023, Marek Polacek wrote: > > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller > > > instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an > > > incomplete class type, and so that the caller doesn't "commit" to > > > building an aggregate conversion. > > > > Perhaps. I wanted to avoid the call to build_user_type_conversion_1. > > I could add an early return to implicit_conversion_1 but I'd have to > > move some code around not to check COMPLETE_TYPE_P before complete_type. > > Maybe return NULL for the incomplete case here, rather than just skipping > reshape_init? > > /* Call reshape_init early to remove redundant braces. */ > if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) > && CLASS_TYPE_P (to) > && COMPLETE_TYPE_P (complete_type (to)) > && !CLASSTYPE_NON_AGGREGATE (to)) > { > expr = reshape_init (to, expr, complain); > if (expr == error_mark_node) > return NULL; > from = TREE_TYPE (expr); > } > > If that doesn't work, the patch is fine as-is. It does work, with one test tweak (which I don't think is a regression): Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- r13-1227 added an assert checking that the index in a CONSTRUCTOR is a FIELD_DECL. That's a reasonable assumption but in this case we never called reshape_init due to the type being incomplete, and so the index remained an identifier node: get_class_binding never got around to looking up the FIELD_DECL. We can avoid the crash by returning early in implicit_conversion_1; we'd return NULL anyway due to: if (i < CONSTRUCTOR_NELTS (ctor)) return NULL; in build_aggr_conv. PR c++/110114 gcc/cp/ChangeLog: * call.cc (implicit_conversion_1): Return early if the type isn't complete. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist100.C: Adjust expected diagnostic. * g++.dg/cpp2a/desig28.C: New test. * g++.dg/cpp2a/desig29.C: New test. --- gcc/cp/call.cc | 19 +++++++++++-------- gcc/testsuite/g++.dg/cpp0x/initlist100.C | 4 ++-- gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C base-commit: b86c0fe327a5196a316bd698d12765b08de5dce7 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index b55230d98aa..673ec91d60e 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -2059,15 +2059,18 @@ implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p, complain &= ~tf_error; /* Call reshape_init early to remove redundant braces. */ - if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) - && CLASS_TYPE_P (to) - && COMPLETE_TYPE_P (complete_type (to)) - && !CLASSTYPE_NON_AGGREGATE (to)) + if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to)) { - expr = reshape_init (to, expr, complain); - if (expr == error_mark_node) - return NULL; - from = TREE_TYPE (expr); + to = complete_type (to); + if (!COMPLETE_TYPE_P (to)) + return nullptr; + if (!CLASSTYPE_NON_AGGREGATE (to)) + { + expr = reshape_init (to, expr, complain); + if (expr == error_mark_node) + return nullptr; + from = TREE_TYPE (expr); + } } if (TYPE_REF_P (to)) diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist100.C b/gcc/testsuite/g++.dg/cpp0x/initlist100.C index 9d80a004c17..6865d34a6f9 100644 --- a/gcc/testsuite/g++.dg/cpp0x/initlist100.C +++ b/gcc/testsuite/g++.dg/cpp0x/initlist100.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++11 } } namespace std { -template class initializer_list; // { dg-message "declaration" } +template class initializer_list; } template struct B { B (std::initializer_list); }; struct C { virtual int foo (); }; -struct D : C {} d { B { D {} } }; // { dg-error "incomplete|no matching" } +struct D : C {} d { B { D {} } }; // { dg-error "no matching" } diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C new file mode 100644 index 00000000000..b63265fea51 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C @@ -0,0 +1,17 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct A { + int a,b; +}; + +struct B; + +void foo(const A &) {} +void foo(const B &) {} + +int +main () +{ + foo({.a=0}); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C new file mode 100644 index 00000000000..bd1a82b041d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C @@ -0,0 +1,10 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct B; + +void foo(const B &) {} + +int main() { + foo({.a=0}); // { dg-error "invalid" } +}