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 3031E385840C for ; Tue, 12 Mar 2024 15:56:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3031E385840C 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 3031E385840C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710259003; cv=none; b=Xkep1UNBVtOXXyzFjFNyqTyDbCu18FgWvwNxSl3Cg6HblJCCuLqOFyp/gTXyJ9ffjjekx7LKm8u0hKNwPuR0W9xa+aCIxWkxxYdRcBoUiJT5RSBoYWbThtxIctoglGlMddJb0NOaK7kPr+tXzvUN0JaS4oHoTMpe4YHwNFrziHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710259003; c=relaxed/simple; bh=p6Kc2vdqpJZJHSsn+Eq2aSRC2pHVSr5yEl6l7FndGTk=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=FGgt3zYS4DsOgYERIZZlUj75Noreu5TGJCpQwPJyy4RekyV7fc9p/ptv4vhIXX9rKv95oN5jiDU9zJwp7VOSOeSEeaUDOVq72oKYm1dsw7YxPYj54tLvAzbaKKsxIVXkRvVWi7eBxsO7038IpTdi81JgFvdzwRljuyS3ngKA2R8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710259000; 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: in-reply-to:in-reply-to:references:references; bh=KCDQGanwi4MdhYFVE3PplHUd5wIilGK/Z05YFrj3LMI=; b=Q49S2TMJxpmju3zIfsBa6zxGq/8vK9KEInOzMJ843SdA6orN4z2CupvVj9KfzYYT3OP+rU nwCZmOYNWPp3IK9XAsmR5yA2h26BgBVZfdiKJq54NS/3El1qVCU4Rfu+aWfCfq65At5js7 7CAnJLCB1oUuWSbTAxvEEjkeXiXJwoQ= Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-353-L9p9GMhSN2-skmOgVGOY8w-1; Tue, 12 Mar 2024 11:56:39 -0400 X-MC-Unique: L9p9GMhSN2-skmOgVGOY8w-1 Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-60a61b31993so8070657b3.1 for ; Tue, 12 Mar 2024 08:56:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710258998; x=1710863798; 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=KCDQGanwi4MdhYFVE3PplHUd5wIilGK/Z05YFrj3LMI=; b=PvoxmyP2DWhG3IXl14qJoBPZegmgCs9w1ewGDtL3v3JZVrQTXCt0qx0oKVWGyuehLH 49tOCO5kz9PX2C5SWe4djF3rxkct3BWVZd7Rk36+GxaGvxhMhBWrJo+aotnjP4qHOsSu /DurqCy3mra4U2Kr8DeapjbX0co/v3u6fHlhHtVVmhFH1Tr36a36FPzB9kyOdJiI3ndQ AWKKT256IzZsBwkFH5p1TR4Zr03nmFR+oPHHnRSu2xEQW3mH0w1NJHZ8sla3mbejIGJ3 GksHdZRcXgo/UwlDYqHXbvk24AgRR23WqEyDL+P2wvozG9LqMM3KrbrzD4ZuzMl3AwjT clvg== X-Gm-Message-State: AOJu0YzU1oi7y2o6S61csq1wtgPFsKXRs2FHlw5lr4zePQJ61IpZE3R2 lgaDzbn0QTTUceV0ZZbueVz63q7uIF8TetdmPa2b5Q5M0qzbdS9MTTJFXM61m6JChMbfVq/31zX d3Ur93GG9daHustHfYvkETE3s8jMqd3AUqxpnqb2FLKlSUBp8QyVNHx+84wOx0zI= X-Received: by 2002:a81:69d5:0:b0:60a:124a:f794 with SMTP id e204-20020a8169d5000000b0060a124af794mr9437112ywc.28.1710258998565; Tue, 12 Mar 2024 08:56:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFbb5PVBYZVFdpLRUgMrzMPvKFjbpaAnvAOvTA05rf4E4CRkbqx8JXwiuE2Tzp/+yaxSxM5/Q== X-Received: by 2002:a81:69d5:0:b0:60a:124a:f794 with SMTP id e204-20020a8169d5000000b0060a124af794mr9437087ywc.28.1710258998064; Tue, 12 Mar 2024 08:56:38 -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 f1-20020ac87f01000000b0042f37ebfbf2sm3645405qtk.5.2024.03.12.08.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 08:56:37 -0700 (PDT) Date: Tue, 12 Mar 2024 11:56:36 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: ICE with temporary of class type in array DMI [PR109966] Message-ID: References: <20240311232758.458729-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) 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=-12.6 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,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: On Tue, Mar 12, 2024 at 09:57:14AM -0400, Jason Merrill wrote: > On 3/11/24 19:27, Marek Polacek wrote: > > 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 > > > > 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; > > Why clear walk_subtrees? Won't that mean we fail to replace any > placeholders nested within an array element initializer? Right. I couldn't find a testcase where that would cause a problem but I think I just wasn't inventive enough. Originally, I was checking same_type_ignoring_top_level_qualifiers_p but that's not going to work for code like struct N { N(M); }; N arr[2] = { M{""}, M{""} }; or with operator M(). But I suppose I could just use can_convert like below. What do you think about that? dg.exp passed, full regtest running. -- >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): Handle initializing an array from a brace-enclosed-initializer. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/nsdmi-aggr20.C: New test. * g++.dg/cpp1y/nsdmi-aggr21.C: New test. --- gcc/cp/typeck2.cc | 18 +++++-- gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C | 17 +++++++ gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C | 59 +++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr20.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr21.C diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 31198b2f9f5..21d4f42ae20 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -1406,6 +1406,7 @@ 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 @@ -1414,24 +1415,33 @@ digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) static bool potential_prvalue_result_of (tree subob, tree full_expr) { +#define RECUR(t) potential_prvalue_result_of (subob, t) 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 + && can_convert (strip_array_types (TREE_TYPE (full_expr)), + TREE_TYPE (subob), tf_none)) + return true; + return false; +#undef RECUR } /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used 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; base-commit: ef79c64cb5762c86ee04ddfcedb7fe31eaa3bac8 -- 2.44.0