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.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 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=1689979129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LVVGT0qEQvD/TjTAaSVZit0N2YqA+k8IjD/7GiL5RRY=; b=eO5jiTzuLnUddorl/ikBp7dfGn9Sk0HnK3TltJf+mY33jxYl6Vi9YM/fcu09uzGlpWcJg0 QlWUplegvdtSDkXP200i1rbpu1mJFD1xBAYzIGuM+Ic9wiA6EYbue7wt4ydE+jCdFxXyL8 ukxR9AexPJ2bf2AbVFuuGHDHrQ/N6XI= 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) From: Marek Polacek 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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 (); } base-commit: 87516efcbe28884c39a8c68e600d11cc91ed96c7 -- 2.41.0