From patchwork Tue Nov 7 19:52:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 162702 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp473121vqo; Tue, 7 Nov 2023 11:53:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IESJidOmAG0EGaJ8onp+5uKuXYMCiO3SnI1qhZEIQUdnxaY59HPn1ilkm0KjDNpNmBrLJN5 X-Received: by 2002:a05:622a:289:b0:417:b53d:a898 with SMTP id z9-20020a05622a028900b00417b53da898mr5192469qtw.9.1699386794419; Tue, 07 Nov 2023 11:53:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699386794; cv=pass; d=google.com; s=arc-20160816; b=Nk8erSBweUFJXcX03H1XzqIcj4dG1s8wloBbzHjaT27xFwqtkDb5rv7YA7oj1SppSB HGtnDH2gi5A/xYt3WB2jYy661PY2Br9aJpWtWFGKo12DD7taAhk/UUYfVJ5LQu9+hXdF WDF7jgdTsdEJBL+YpnKhuKIGxez7xrlQn0OzR8H2ZeSSyzjY3J4NxcRLkeDrYPi7dzIE uvizPDVOGsLmvAhaOasiK1yNYMgId6D2XWkERnEUwb5borUmXDoJWwVSKv114YekeWU+ Xes+ZQo+0sEQRDAaEapmTT+0MgjPEy7afmNVqFiekSBm68iKvlsn8CSFkbQES6C2laQC 1dhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=ZdbP59pYVnbxqEvqmTZIJEvvLmu0OLbT0MiBuidxpww=; fh=1Hi9m88IqcWZGtfSxUrMGG+GBgL26N02X1SANXM6iTs=; b=Pkah3GS7Klf9yztxHW1+tnEqz+dLXiqvXlr/MYTlULQcmBVWNdjt1ULVZnGyQby23l 57IsPYlVFYtVwslV8yKlkFX8cc3u54glwkJEgQomjB1ePGKgseftF5E4A7UKURIwKA/o OcqKYQCGt8xJfFsqsXsf1UiScL+VX+/9oqAzaiKgPFT5j+t0+8ojSHwLW/2r1z4lAJiP 9X9EBeklVb/bgyIbVQpFjBE2LBBecxb1+gZvFzitCLYU9QgDj7De1rrNDC2LN72AdMS4 Z8F1s/RZpIpS0+G7CH8kU0NmF0Pu3oBZ4E8uwVizfFjGj1o+DV7+uQUQV7gNNAKuGsO3 Fkgw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Bz3r7guF; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v19-20020a05622a189300b004197c401385si300368qtc.439.2023.11.07.11.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 11:53:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Bz3r7guF; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DACBB3858C39 for ; Tue, 7 Nov 2023 19:53:13 +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 ESMTPS id 73C123858D33 for ; Tue, 7 Nov 2023 19:52:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73C123858D33 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 73C123858D33 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=1699386770; cv=none; b=kZbBij+nikhd7ob7YZudFHrjfMrGrIwvZcPyzsiBB4O6pTBnNPKAfhvu+GiAIiDln+YU0SofYRm6mZHi8eW0+ZXDi4/NpfbIXkhMOW9cJNzvY4GmHUEwXUscZtkmmjk9nW9WafMr6dlee4/S4Pc6O0TpJ3NunwzWk+1jQiTc/Cs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699386770; c=relaxed/simple; bh=AdSPUdSVojc4fCP9KMPaXYhWIijgCwy65vq+DEDQ+vI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Atwa5K4QUBxw1pW4sDwqAGUjdoMYSntHAW/VRU70OM7KiWJ89NwnJwVfl43UmVghw7ePFdnuSKiDceM3QJ5pnE9Rt/Ip1GXNkDJxUq+g7bvM+cut429o+ol833+DhnbuLGAbmN9FzToMJDxD/ypx5jsXKoKPFxA9U1t4RepXjSo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699386769; 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=ZdbP59pYVnbxqEvqmTZIJEvvLmu0OLbT0MiBuidxpww=; b=Bz3r7guFjyuCHLo2SFUp4w2jAr57sO3D4G2InrAr224wZ53ID97blZ37t5dAEIkmzc6VNN BbWFmHT+PcdeOthZQQwh5AgMKNfH2pBiiTL6MzBFSFszAFteswbkuEeSbK5+aR4ueGxtXR PL4ha/2ljgjIjDHAM10xEpfE3IgJh6I= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-jvwjLZS2M5K5SN2rpgW6NQ-1; Tue, 07 Nov 2023 14:52:48 -0500 X-MC-Unique: jvwjLZS2M5K5SN2rpgW6NQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7789f06778aso8205985a.0 for ; Tue, 07 Nov 2023 11:52:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699386766; x=1699991566; 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=ZdbP59pYVnbxqEvqmTZIJEvvLmu0OLbT0MiBuidxpww=; b=T0fuEsixY82oLFyw8nPI6ickPDDQTFPQaS67AnubyerP7PXzICQ+eENVni/4FW89GK MGIFnKiPZqFnvdleK9aABb5nFX4KXPukCgEx+hugomLShaectiCaES5gX8TFHi3th2AO 2ongwBZKgzRVrxBPapLjYpY6QHSV5e54DPBIzofhrJefJ4zcpFJ3mpFqqxrgri47toXG nv2G2UtPfIbshz2isnMkO0sr4nSBiNgS2128TcDh1wDf38VgGfd2LZCpFvcsA/gdtn+9 KXcVoe+tpueBhbgw4cXMJ3Iv6k23n1oRHy1/3m/ItEV93AvssYLrIyoQlg9Zdod7CPab qnsA== X-Gm-Message-State: AOJu0YzCYPyfOMsH4TO73sV15NVg/4gMNVKJBZuhHYHaAHt3l9frxKLc 1ENPiExlwBCEWELGwt3XZnzkCFrAgzNGZA5XDU2Da3rMPkYyKG+Wu34Bi09qXWe1sceTLz0TBeM RrlklnXS/+zq4ow5RUo9Q5AzsWUvMz5THmTHswpEwHFyiUlcXRQ1QYziG2yzjPD3o54Xqbr9ck4 Q= X-Received: by 2002:a05:620a:4614:b0:777:7214:44d with SMTP id br20-20020a05620a461400b007777214044dmr4069979qkb.23.1699386766543; Tue, 07 Nov 2023 11:52:46 -0800 (PST) X-Received: by 2002:a05:620a:4614:b0:777:7214:44d with SMTP id br20-20020a05620a461400b007777214044dmr4069957qkb.23.1699386766176; Tue, 07 Nov 2023 11:52:46 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id pc37-20020a05620a842500b00765ab6d3e81sm219082qkn.122.2023.11.07.11.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 11:52:45 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: decltype of (by-value captured reference) [PR79620] Date: Tue, 7 Nov 2023 14:52:44 -0500 Message-ID: <20231107195244.1658781-1-ppalka@redhat.com> X-Mailer: git-send-email 2.43.0.rc0.23.g8be77c5de6 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781936207318903553 X-GMAIL-MSGID: 1781936207318903553 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- The capture decltype handling in finish_decltype_type wasn't looking through implicit INDIRECT_REF (added by convert_from_reference), which caused us to incorrectly resolve decltype((x)) to float& below. We still don't fully accept the example ultimately because when processing the decltype inside the first lambda's trailing return type, we're in lambda type scope but not yet in lambda function scope that the check looks for, which seems like an orthogonal bug. PR c++/79620 gcc/cp/ChangeLog: * cp-tree.h (STRIP_REFERENCE_REF): Define. * semantics.cc (finish_decltype_type): Use it to look through implicit INDIRECT_REF when deciding whether to call capture_decltype. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-decltype3.C: New test. --- gcc/cp/cp-tree.h | 4 +++ gcc/cp/semantics.cc | 4 +-- .../g++.dg/cpp0x/lambda/lambda-decltype3.C | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype3.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b2603d4830e..1fa710d7154 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4084,6 +4084,10 @@ struct GTY(()) lang_decl { && TREE_TYPE (TREE_OPERAND (NODE, 0)) \ && TYPE_REF_P (TREE_TYPE (TREE_OPERAND ((NODE), 0)))) +/* Look through an implicit INDIRECT_REF from convert_from_reference. */ +#define STRIP_REFERENCE_REF(NODE) \ + (REFERENCE_REF_P (NODE) ? TREE_OPERAND (NODE, 0) : NODE) + /* True iff this represents an lvalue being treated as an rvalue during return or throw as per [class.copy.elision]. */ #define IMPLICIT_RVALUE_P(NODE) \ diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index f583dedd6cf..8df4521bf7c 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11717,10 +11717,10 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p, transformed into an access to a corresponding data member of the closure type that would have been declared if x were a use of the denoted entity. */ - if (outer_automatic_var_p (expr) + if (outer_automatic_var_p (STRIP_REFERENCE_REF (expr)) && current_function_decl && LAMBDA_FUNCTION_P (current_function_decl)) - type = capture_decltype (expr); + type = capture_decltype (STRIP_REFERENCE_REF (expr)); else if (error_operand_p (expr)) type = error_mark_node; else if (expr == current_class_ptr) diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype3.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype3.C new file mode 100644 index 00000000000..7fc157aefb5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype3.C @@ -0,0 +1,28 @@ +// PR c++/79620 +// [expr.prim.id.unqual] example 1 +// { dg-do compile { target c++11 } } + +void f() { + float x, &r = x; + + [=]() -> decltype((x)) { // lambda returns float const& because this lambda is not mutable and + // x is an lvalue + decltype(x) y1; // y1 has type float + decltype((x)) y2 = y1; // y2 has type float const& + decltype(r) r1 = y1; // r1 has type float& + decltype((r)) r2 = y2; // r2 has type float const& + return y2; // { dg-bogus "'float&' to 'const float'" "" { xfail *-*-* } } + }; + + [=](decltype((x)) y) { + decltype((x)) z = x; // OK, y has type float&, z has type float const& + }; + + [=] { + [](decltype((x)) y) {}; // OK, lambda takes a parameter of type float const& + + [x=1](decltype((x)) y) { + decltype((x)) z = x; // OK, y has type int&, z has type int const& + }; + }; +}