From patchwork Fri Jul 21 22:38:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 124138 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp502826vqg; Fri, 21 Jul 2023 15:40:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlFsS4oQYgK4UCzmt4GrycJfw1eWPeoPKmswACzVCXry633RLBqywJ+0E2gTjDRcFp9gbcg4 X-Received: by 2002:a17:906:311a:b0:966:17b2:5b0b with SMTP id 26-20020a170906311a00b0096617b25b0bmr2893476ejx.49.1689979237732; Fri, 21 Jul 2023 15:40:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689979237; cv=none; d=google.com; s=arc-20160816; b=Pi8t9apTyn3eAyiw/rBoFP+vf2OgnqTaSkLvAFEiECgq4MeGOO7rbQ2vGwqFS1ucK8 4xLVR05xaqACbIPT+rgD/fcjlAaqgzKmpVdF/dzgoMWfRIVgNTK0jDva6LWxysm4YrWp pMPwt9Aq9pMBE8FKQN5mvFkaO2KaNun8HQB699bojxvIZOkvFBd7h65Vl/iioywqFD/c rXOVOk2NBUelSwO/A9U6xHPNrpXWnM95vzNgwuXDrfasn9ixqAKWsDs2sYxBAw7K7dpk /iITrr8M8v/lXfRMovB8SjaLeIr3rblgPNBxJaCddoKmuzq5XejZQks74oes/KxeuyRV mnsw== 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-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=LVVGT0qEQvD/TjTAaSVZit0N2YqA+k8IjD/7GiL5RRY=; fh=7pRf3suwruT4MOcKZ5rZBLmKaAxmc6IyTlSbYU/lmno=; b=ABsPQFndRdx+V4VthvdI/10EJT6ePBN9pkVM/5HzXnzwToo7z38s20GSKY0CIkvhn+ 3pQ/DQalLypsfIhuV7BhY9M+yDDdXAmpymUmbgqD4CqAPuYbufOSrWI7e+48nAJXUvZ+ Je02HO/VM9vOY7fOq0tCZd01n2Q7mSbkbRy9PHIRzqVFFzRYRy01NoG1E3HOR+lVFyye Ild6FRgmcaN6KLYz4QcDspvGfYVnvGziQ6s8BOjMPkYRERBFyh3SKTuyRjlSFovJl4Gw haAEDjBEoXmPbR3uIi4zvOB/LT9FvjJWxuh8fuWJHkZucZMZI8ecTp58x/LWFV8D8U6a Miuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=edGlZU6u; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id kk27-20020a170907767b00b00988839e5435si2541799ejc.522.2023.07.21.15.40.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jul 2023 15:40:37 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=edGlZU6u; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 9B31B3870C23 for ; Fri, 21 Jul 2023 22:39:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B31B3870C23 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689979186; bh=LVVGT0qEQvD/TjTAaSVZit0N2YqA+k8IjD/7GiL5RRY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=edGlZU6u4riD93us4LRNtkhNKkk+OTVXtv9s1INobuSjr2YINoz3RHiZ8ELzygVMI qdurnYBTyO52k1MzKsrk2axzTru6CMaQNHWeTIGOYYkNIvlF0UyDT5nKJ23VOcYc2U twKSoD6pO8+cpWuD6bshUe/tvyjjG/Zx4e6vVzFI= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id B29A1386C5A9 for ; Fri, 21 Jul 2023 22:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B29A1386C5A9 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-161-kPNZb-AIOq-OuY6UNh3ROw-1; Fri, 21 Jul 2023 18:38:39 -0400 X-MC-Unique: kPNZb-AIOq-OuY6UNh3ROw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C10B8037BA for ; Fri, 21 Jul 2023 22:38:39 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.32.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 78F7F1121314; Fri, 21 Jul 2023 22:38:39 +0000 (UTC) To: Jason Merrill , GCC Patches Cc: Patrick Palka Subject: [PATCH] c++: fix ICE with constexpr ARRAY_REF [PR110382] Date: Fri, 21 Jul 2023 18:38:35 -0400 Message-ID: <20230721223835.630543-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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: 1772071669135362781 X-GMAIL-MSGID: 1772071669135362781 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/13? -- >8 -- This code in cxx_eval_array_reference has been hard to get right. In r12-2304 I added some code; in r13-5693 I removed some of it. Here the problematic line is "S s = arr[0];" which causes a crash on the assert in verify_ctor_sanity: gcc_assert (!ctx->object || !DECL_P (ctx->object) || ctx->global->get_value (ctx->object) == ctx->ctor); ctx->object is the VAR_DECL 's', which is correct here. The second line points to the problem: we replaced ctx->ctor in cxx_eval_array_reference: new_ctx.ctor = build_constructor (elem_type, NULL); // #1 which I think we shouldn't have; the CONSTRUCTOR we created in cxx_eval_constant_expression/DECL_EXPR new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); had the right type. We still need #1 though. E.g., in constexpr-96241.C, we never set ctx.ctor/object before calling cxx_eval_array_reference, so we have to build a CONSTRUCTOR there. And in constexpr-101371-2.C we have a ctx.ctor, but it has the wrong type, so we need a new one. PR c++/110382 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_array_reference): Create a new constructor only when we don't already have a matching one. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-110382.C: New test. --- gcc/cp/constexpr.cc | 5 ++++- gcc/testsuite/g++.dg/cpp1y/constexpr-110382.C | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-110382.C base-commit: 87516efcbe28884c39a8c68e600d11cc91ed96c7 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index fb94f3cefcb..518b7c7a2d5 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4291,7 +4291,10 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, else val = build_value_init (elem_type, tf_warning_or_error); - if (!SCALAR_TYPE_P (elem_type)) + if (!SCALAR_TYPE_P (elem_type) + /* Create a new constructor only if we don't already have one that + is suitable. */ + && !(ctx->ctor && same_type_p (elem_type, TREE_TYPE (ctx->ctor)))) { new_ctx = *ctx; new_ctx.ctor = build_constructor (elem_type, NULL); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-110382.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-110382.C new file mode 100644 index 00000000000..317c5ecfcd5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-110382.C @@ -0,0 +1,17 @@ +// PR c++/110382 +// { dg-do compile { target c++14 } } + +struct S { + double a = 0; +}; + +constexpr double +g () +{ + S arr[1]; + S s = arr[0]; + (void) arr[0]; + return s.a; +} + +int main() { return g (); }