From patchwork Tue Aug 13 02:01:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1971719 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=IG5MqIxg; 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 4WjZPt33Qvz1yfP for ; Tue, 13 Aug 2024 12:01:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 755713858D39 for ; Tue, 13 Aug 2024 02:01:49 +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.129.124]) by sourceware.org (Postfix) with ESMTP id 936593858D20 for ; Tue, 13 Aug 2024 02:01:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 936593858D20 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 936593858D20 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=1723514484; cv=none; b=aYsGnjzTN8+1CEhTZ16JHEWAnl7a/NGF2TbyIB7egfbZT0KT2k8j1wedNA2eRKX/ZdPM02TyxJfMgXCjptDOTJEbG3rsOCF10jGTHgrR64z59Kj6dUp4UqYTiaAQboZR8nWIaKcDNiI60UdmFVZfzjQYj6NNrbDJzrGA+m0/LvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723514484; c=relaxed/simple; bh=JQ89pIRBxy9sAbD3vcfEiZ/qk1cbn5h1SASfUy6YU6w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kTe5perQSJ+I4ixRxYU5uGcHBxaHrViFDv0jAruVE0v1COgRcxAi9VPDrJ658BH5ij5xV/ZF7EKfsxYrG1RokKE14jVvg7afltJudD8JK8a+pobPYA9HwT6ZQDUOKLV5jW/k1OKksKu6pyBp65wDwrptl4SO0cptkrFAXKUiTlM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723514482; 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=boVpszyBXbGsIux273FmDt6Rib+c6y2il60Rw5xRHhA=; b=IG5MqIxgLjrds5M0whxrCbf+bol9cI/eUfQ7w4xNN6kv8Ymbf7V1nebnZ9tPfGv4yFjSCo iLDI/nRvvv4DAMd77LNU9QorWTGLY2mWN7fNSUfMJjUSB8DubW9W/BYJtYJb7lnyRjx5AG LKyD/IqJf9Dx/hlJYGLvRUgy5l9EmUw= Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-489-vTFokCabPh-zMAHlkI4O9A-1; Mon, 12 Aug 2024 22:01:20 -0400 X-MC-Unique: vTFokCabPh-zMAHlkI4O9A-1 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-66628e9ec89so106454657b3.1 for ; Mon, 12 Aug 2024 19:01:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723514479; x=1724119279; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=boVpszyBXbGsIux273FmDt6Rib+c6y2il60Rw5xRHhA=; b=gXh2BZdPb5oz0RwbBR72oty26X4+4mTmPXXAuJvLAp0NCGyitGxpaS4zLTMSvBdR6D lloisWpeLm/SwE6Cp/zWpnuk50nPIB7UrH/WJGxidE1SCycYTpzZnVrD24KHPGQ/IBlx rYkilj5SYa4MYCA/2JMZLpnXvrWSTDexTv+Xd22JF/tKIwVzg++Ms+IT8rgfWn8VGYis RnEFs7nm7xC4tmKszmHn2lKEggai1dsIlmc55AZxFWVtPZS8Kdx+aNdcAbLscaNEeWTN 0/u5/ZdjYu55Yhh5j9V1YWxW/PMbDLinb28nio4OczOA6Jx0xf3P0uhFU5KZU6lP1a7O 4YSg== X-Gm-Message-State: AOJu0YzfLPLg11wi5rUAl+LLpXRaxH3aeyTh4sgOmhqnztYR8p5P670W z++zhGltbT+QYfdtyIGm3Er/FKfj1e6c/DVuyFYz0bGBv50LnTIlx7IN9Sh4XJXzuP1XEvSDAvF iNs0MBdEeHvSj9yme6/ZebTYK3KpsQGNxJV8/n9844gop1YEnl0CCoehLJKZMb/E08aHoVl0Izw xnPy3A4uUPitVcKqgMC5DkUfNhpk2TfFsdjEf6 X-Received: by 2002:a05:6902:2b02:b0:e0b:efa8:18b6 with SMTP id 3f1490d57ef6-e113d281fefmr3184291276.45.1723514479550; Mon, 12 Aug 2024 19:01:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEQfBKX5ywd+tnbiYRqChCrxGiS24GSUkoTD9v5Y+868wzEXT4I8I6P7ru9Ewb2UbZ++0oAAA== X-Received: by 2002:a05:6902:2b02:b0:e0b:efa8:18b6 with SMTP id 3f1490d57ef6-e113d281fefmr3184268276.45.1723514479125; Mon, 12 Aug 2024 19:01:19 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bd82c7d679sm29927726d6.49.2024.08.12.19.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Aug 2024 19:01:18 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, iain@sandoe.co.uk, Patrick Palka Subject: [PATCH] c++/coroutines: fix passing *this to promise type, again [PR116327] Date: Mon, 12 Aug 2024 22:01:16 -0400 Message-ID: <20240813020116.100242-1-ppalka@redhat.com> X-Mailer: git-send-email 2.46.0.77.g25673b1c47 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 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_H3, 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 Tested on x86_64-pc-linux-gnu, does this look OK for trunk/14? -- >8 -- In r15-2210 we got rid of the unnecessary cast to lvalue reference when passing *this to the promise type ctor, and as a drive-by change we also simplified the code to use cp_build_fold_indirect_ref. But cp_build_fold_indirect_ref apparently does too much here, namely it has a shortcut for returning current_class_ref if the operand is current_class_ptr. The problem with that shortcut is current_class_ref might have gotten clobbered earlier if it appeared in the function body, since rewrite_param_uses walks and rewrites in-place all local variable uses to their corresponding frame copy. So later this cp_build_fold_indirect_ref for *__closure will instead return the mutated current_class_ref i.e. *frame_ptr->__closure, which doesn't make sense here since we're in the ramp function and not the actor function where frame_ptr is in scope. This patch fixes this by building INDIRECT_REF directly instead of using cp_build_fold_indirect_ref. (Another approach might be to restore an unshare_expr'd current_class_ref after doing coro_rewrite_function_body to avoid it remaining clobbered after the rewriting process. Yet another more ambitious approach might be to avoid this tree sharing in the first place by returning unshared versions of current_class_ref from maybe_dummy_object etc.) PR c++/116327 PR c++/104981 PR c++/115550 gcc/cp/ChangeLog: * coroutines.cc (morph_fn_to_coro): Build INDIRECT_REF directly instead of using cp_build_fold_indirect_ref when doing *this. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr116327-preview-this.C: New test. --- gcc/cp/coroutines.cc | 6 +++-- .../g++.dg/coroutines/pr116327-preview-this.C | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 145ec4b1d16..58be456ce00 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -4850,7 +4850,8 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) if (parm_i->this_ptr || parm_i->lambda_cobj) { /* We pass a reference to *this to the allocator lookup. */ - tree this_ref = cp_build_fold_indirect_ref (arg); + tree tt = TREE_TYPE (TREE_TYPE (arg)); + tree this_ref = build1 (INDIRECT_REF, tt, arg); vec_safe_push (args, this_ref); } else @@ -5070,7 +5071,8 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) if (parm.this_ptr || parm.lambda_cobj) { /* We pass a reference to *this to the param preview. */ - tree this_ref = cp_build_fold_indirect_ref (arg); + tree tt = TREE_TYPE (TREE_TYPE (arg)); + tree this_ref = build1 (INDIRECT_REF, tt, arg); vec_safe_push (promise_args, this_ref); } else if (parm.rv_ref) diff --git a/gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C b/gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C new file mode 100644 index 00000000000..27b69a41392 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C @@ -0,0 +1,22 @@ +// PR c++/116327 - ICE in coroutine with parameter preview on lambda with captures + +#include + +struct coroutine{ + struct promise_type{ + promise_type(const auto &...){} + std::suspend_never initial_suspend(){ return {}; } + std::suspend_always final_suspend()noexcept{ return {}; } + void unhandled_exception(){} + coroutine get_return_object(){ return {}; } + void return_value(int)noexcept{} + }; +}; + +int main(){ + auto f = [a=0](auto) -> coroutine { + co_return 2; + }; + f(0); + return 0; +}