From patchwork Mon Mar 11 23:27:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1910767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DstPNNvN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TttJ64KQ3z1yWn for ; Tue, 12 Mar 2024 10:28:36 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C6E37385828D for ; Mon, 11 Mar 2024 23:28:34 +0000 (GMT) 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 E7DA9385841E for ; Mon, 11 Mar 2024 23:28:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7DA9385841E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E7DA9385841E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710199692; cv=none; b=I16z9Qos4jJSyFOQsAJintpA3D+Dh2QY5aGEne86FvsUpUT+Rla5uxDOvlKqoGDoXKQdtqmSDUQSHvsuSvkVfu2yM+/gZr9mw7ahz4bVqMbe9EBbwT1qzYxiUk2i/3OLsc5wOxsHlsZ7jUY4/d39zC1MZC9RDsV+fT0miUDh5SY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710199692; c=relaxed/simple; bh=mcxfL1Doo3hbi4tOIz4T5+yzkOrMd15maU2wc/sT3YE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AHl4/21JNyoWGcG8BqPSiMpeZpmkWUQLVlHOSaJBsjfEmiXKYBNRXEt3kD2gsm7sCdwAsRepSIV58tiRwdGk5hCeWWhxibcO3xZFebQLWNKZ81hfMUVrMhaONWcbdud+fAjYnlsv/j3ZlujPeZsOAZ96v4k5KYvlycQ8DCl6/I4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710199687; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=p+DfaTeHGEw5ilbHoL+VWvGiZ+SLa/xPrG+GVEs50aA=; b=DstPNNvNBQy33j4alX9BEp4LVWBT7nFYZLxMuuxMsxXFQzkOOPt5FRWmxy5u3v2nAtD63G kYn9fPbIGCc7W4gfhpuibh3csZRZ3tVAw3q9hBejiZselTnQunEEbOxkSeIl5/DTjXSf3X NVcjChRehRsGq42hXQFAEYomnBGHHl4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-674-E3eVhIAAOXejiH62Wfa5BA-1; Mon, 11 Mar 2024 19:28:05 -0400 X-MC-Unique: E3eVhIAAOXejiH62Wfa5BA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 28CBB28EC105 for ; Mon, 11 Mar 2024 23:28:05 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.33.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0AFE540C6CB7; Mon, 11 Mar 2024 23:28:05 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: ICE with temporary of class type in array DMI [PR109966] Date: Mon, 11 Mar 2024 19:27:58 -0400 Message-ID: <20240311232758.458729-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/13? -- >8 -- This ICE started with the fairly complicated r13-765. We crash in gimplify_var_or_parm_decl because a stray VAR_DECL leaked there. The problem is ultimately that potential_prvalue_result_of wasn't correctly handling arrays and replace_placeholders_for_class_temp_r replaced a PLACEHOLDER_EXPR in a TARGET_EXPR which is used in the context of copy elision. If I have M m[2] = { M{""}, M{""} }; then we don't invoke the M(const M&) copy-ctor. I think the fix is to detect such a case in potential_prvalue_result_of. PR c++/109966 gcc/cp/ChangeLog: * typeck2.cc (potential_prvalue_result_of): Add walk_subtrees parameter. Handle initializing an array from a brace-enclosed-initializer. (replace_placeholders_for_class_temp_r): Pass walk_subtrees down to potential_prvalue_result_of. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/nsdmi-aggr20.C: New test. * g++.dg/cpp1y/nsdmi-aggr21.C: New test. --- gcc/cp/typeck2.cc | 27 ++++++++--- gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C | 17 +++++++ gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C | 59 +++++++++++++++++++++++ 3 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C base-commit: 0c179654c3170749f3fb3232f2442fcbc99bffbb diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 31198b2f9f5..8b99ce78e9a 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -1406,46 +1406,59 @@ digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) A a = (A{}); // initializer A a = (1, A{}); // initializer A a = true ? A{} : A{}; // initializer + A arr[1] = { A{} }; // initializer auto x = A{}.x; // temporary materialization auto x = foo(A{}); // temporary materialization FULL_EXPR is the whole expression, SUBOB is its TARGET_EXPR subobject. */ static bool -potential_prvalue_result_of (tree subob, tree full_expr) +potential_prvalue_result_of (tree subob, tree full_expr, int *walk_subtrees) { +#define RECUR(t) potential_prvalue_result_of (subob, t, walk_subtrees) if (subob == full_expr) return true; else if (TREE_CODE (full_expr) == TARGET_EXPR) { tree init = TARGET_EXPR_INITIAL (full_expr); if (TREE_CODE (init) == COND_EXPR) - return (potential_prvalue_result_of (subob, TREE_OPERAND (init, 1)) - || potential_prvalue_result_of (subob, TREE_OPERAND (init, 2))); + return (RECUR (TREE_OPERAND (init, 1)) + || RECUR (TREE_OPERAND (init, 2))); else if (TREE_CODE (init) == COMPOUND_EXPR) - return potential_prvalue_result_of (subob, TREE_OPERAND (init, 1)); + return RECUR (TREE_OPERAND (init, 1)); /* ??? I don't know if this can be hit. */ else if (TREE_CODE (init) == PAREN_EXPR) { gcc_checking_assert (false); - return potential_prvalue_result_of (subob, TREE_OPERAND (init, 0)); + return RECUR (TREE_OPERAND (init, 0)); } } + /* The array case listed above. */ + else if (TREE_CODE (full_expr) == CONSTRUCTOR + && TREE_CODE (TREE_TYPE (full_expr)) == ARRAY_TYPE) + for (constructor_elt &e: CONSTRUCTOR_ELTS (full_expr)) + if (e.value == subob) + { + *walk_subtrees = 0; + return true; + } + return false; +#undef RECUR } /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used in the context of guaranteed copy elision). */ static tree -replace_placeholders_for_class_temp_r (tree *tp, int *, void *data) +replace_placeholders_for_class_temp_r (tree *tp, int *walk_subtrees, void *data) { tree t = *tp; tree full_expr = *static_cast(data); /* We're looking for a TARGET_EXPR nested in the whole expression. */ if (TREE_CODE (t) == TARGET_EXPR - && !potential_prvalue_result_of (t, full_expr)) + && !potential_prvalue_result_of (t, full_expr, walk_subtrees)) { tree init = TARGET_EXPR_INITIAL (t); while (TREE_CODE (init) == COMPOUND_EXPR) diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C new file mode 100644 index 00000000000..4796d861e83 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C @@ -0,0 +1,17 @@ +// PR c++/109966 +// { dg-do compile { target c++14 } } + +#define SA(X) static_assert ((X),#X) + +struct A { + int a; + int b = a; +}; + +struct B { + int x = 0; + int y[1]{A{x}.b}; +}; + +constexpr B b = { }; +SA(b.y[0] == 0); diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C new file mode 100644 index 00000000000..efec45bc1a8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C @@ -0,0 +1,59 @@ +// PR c++/109966 +// { dg-do compile { target c++14 } } + +struct k { + k(const char *); +}; +struct M { + k name; + int j = 42; + int i = j; +}; + +M m = M{""}; + +struct S { + M arr1[1]{M{""}}; + M a1[1] = { (M{""}) }; + M a2[1] = { (true, M{""}) }; + M a3[1] = { true ? M{""} : M{""} }; + M arr2[2]{M{""}, M{""}}; + M arr3[3]{M{""}, M{""}, M{""}}; + + M arr1e[1] = {M{""}}; + M arr2e[2] = {M{""}, M{""}}; + M arr3e[3] = {M{""}, M{""}, M{""}}; + + M arr1l[1] = { m }; + M arr2l[2] = { m, m }; + M arr3l[3] = { m, m, m }; + + M m1 = M{""}; + M m2 = m; + M m3{M{""}}; + M m4 = {M{""}}; +} o; + +struct N { + N(M); +}; + +struct Z { + N arr1[1]{ M{""} }; + N arr2[2]{ M{""}, M{""} }; + N arr1e[1] = { M{""} }; + N arr2e[2] = { M{""}, M{""} }; +} z; + +struct Y { + k name; + int j = 42; + int i = j; + operator M(); +}; + +struct W { + M arr1[1]{ Y{""} }; + M arr2[2]{ Y{""}, Y{""} }; + M arr3[3]{ Y{""}, Y{""}, Y{""} }; +} w;