From patchwork Wed Aug 28 15:21:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1977949 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=TwqKmnv7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Wv7S40km3z1yfy for ; Thu, 29 Aug 2024 01:21:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5CEB93860C3E for ; Wed, 28 Aug 2024 15:21:54 +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 ESMTP id B19C93858C31 for ; Wed, 28 Aug 2024 15:21:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B19C93858C31 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 B19C93858C31 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=1724858494; cv=none; b=aTRE2ehD18hE0L4QQ7MjHnKGpQu7Sjb2B81fuWNp6Le5P6NoMJ3/BoKVamNkLghGMY3pOeoMIZn8I5A7ZLAuArUHZms/WUaDFExfEXF2r4WUNmUHNGyR3g6Rq9+51Q/wJzhpa4esxVY8ebT0VlY1rOVu7bGIC2zx1WRK9CKMHKs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724858494; c=relaxed/simple; bh=ZOtrjKWPv7GV8ssnuBTzBm/Oppo+pv5eyoCV59DbXaI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DrZcVNtiKhmo/KyfMZMCTxHnPdBNJBM6QsgiCbFHthglrDcHMx3/xKUWR2BCgxu3IEla+5sz4OFeEaIZ35h+gkxVGOXppyz7OpHn9HuYBHw3PIVshRG8DYuiRwLnRoFG7fERFvY65TIsUwirO154WTUz2ztOeSSA8AEnKx86s9w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724858492; 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=vuLDUrNgTCIipAkZKkpp8izIOfUr+pLk1Hfn1zdx2wI=; b=TwqKmnv7XGhEAw73SVNQWShMe+gNDRhSXFLV0u1zBVBPkiwH00OrSGhYkRyuXQc7bCrEHT FZOrRiqysteYYUuoG86/3Nei066Z9VeXeRgCuxaoNyESfjN9RwDxkNVbMpMYg3WBSjG2Hq 3kS7D3apbHcHBQPlcVtOuSWIfq0Suv0= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-WQDRkHeuNjmcXI5z2M4ORQ-1; Wed, 28 Aug 2024 11:21:31 -0400 X-MC-Unique: WQDRkHeuNjmcXI5z2M4ORQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 673221955BED for ; Wed, 28 Aug 2024 15:21:30 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.32.209]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 322481955D56; Wed, 28 Aug 2024 15:21:29 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: ICE with ()-init and TARGET_EXPR eliding [PR116424] Date: Wed, 28 Aug 2024 11:21:19 -0400 Message-ID: <20240828152119.18115-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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? -- >8 -- Here we crash on a cp_gimplify_expr/TARGET_EXPR assert: gcc_checking_assert (!TARGET_EXPR_ELIDING_P (*expr_p) || !TREE_ADDRESSABLE (TREE_TYPE (*expr_p))); We cannot elide the TARGET_EXPR because we're taking its address. It is set as eliding in massage_init_elt. I've tried to not set TARGET_EXPR_ELIDING_P when the context is not direct-initialization. That didn't work: even when it's not direct-initialization now, it can become one later, for instance, after split_nonconstant_init. One problem is that replace_placeholders_for_class_temp_r will replace placeholders in non-eliding TARGET_EXPRs with the slot, but if we then elide the TARGET_EXPR, we end up with a "stray" VAR_DECL and crash. (Only some TARGET_EXPRs are handled by replace_decl.) I thought I'd have to go back to but then I realized that this problem occurrs only with ()-init but not {}-init. With {}-init, there is no problem, because we are clearing TARGET_EXPR_ELIDING_P in process_init_constructor_record: /* We can't actually elide the temporary when initializing a potentially-overlapping field from a function that returns by value. */ if (ce->index && TREE_CODE (next) == TARGET_EXPR && unsafe_copy_elision_p (ce->index, next)) TARGET_EXPR_ELIDING_P (next) = false; But that does not happen for ()-init because we have no ce->index. ()-init doesn't allow brace elision so we don't really reshape them. But I can just move the clearing a few lines down and then it handles both ()-init and {}-init. PR c++/116424 gcc/cp/ChangeLog: * typeck2.cc (process_init_constructor_record): Move the clearing of TARGET_EXPR_ELIDING_P down. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/paren-init38.C: New test. --- gcc/cp/typeck2.cc | 14 +++++++------- gcc/testsuite/g++.dg/cpp2a/paren-init38.C | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/paren-init38.C base-commit: bdcd30e4711943cae70a1b47f8a63e96a94c02a0 diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 30a6fbe95c9..a0c8f833ac1 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -1774,13 +1774,6 @@ process_init_constructor_record (tree type, tree init, int nested, int flags, { gcc_assert (ce->value); next = massage_init_elt (fldtype, next, nested, flags, complain); - /* We can't actually elide the temporary when initializing a - potentially-overlapping field from a function that returns by - value. */ - if (ce->index - && TREE_CODE (next) == TARGET_EXPR - && unsafe_copy_elision_p (ce->index, next)) - TARGET_EXPR_ELIDING_P (next) = false; ++idx; } } @@ -1873,6 +1866,13 @@ process_init_constructor_record (tree type, tree init, int nested, int flags, } } + /* We can't actually elide the temporary when initializing a + potentially-overlapping field from a function that returns by + value. */ + if (TREE_CODE (next) == TARGET_EXPR + && unsafe_copy_elision_p (field, next)) + TARGET_EXPR_ELIDING_P (next) = false; + if (is_empty_field (field) && !TREE_SIDE_EFFECTS (next)) /* Don't add trivial initialization of an empty base/field to the diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init38.C b/gcc/testsuite/g++.dg/cpp2a/paren-init38.C new file mode 100644 index 00000000000..58743e051da --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init38.C @@ -0,0 +1,20 @@ +// PR c++/116424 +// { dg-do compile { target c++20 } } + +struct dd { + char *ptr; + dd(); + dd(dd &&__str); +}; +struct v { + dd n{}; + int f = -1; + v operator|(const v &other) const; +}; +struct cc : v {}; +static const cc a; +static const cc b; +static const cc c1(a | b); +static const cc c2{a | b}; +static const cc c3 = cc(a | b); +static const cc c4 = cc{a | b};