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: 1861276 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=Bz3r7guF; 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 4SPzR43TV8z1yRF for ; Wed, 8 Nov 2023 06:53:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A98163858C66 for ; Tue, 7 Nov 2023 19:53:01 +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-Google-Smtp-Source: AGHT+IG9dsFduE2b9oCNtn5gu+UJ2Bk64VOI3MnDQ8A/jm2B7PMigwdJnIwVrIT6ShRW0sPK3+RVWA== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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& + }; + }; +}