From patchwork Thu Apr 14 18:51:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1617400 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=GAJVClN5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KfT9Z3T3Sz9s3q for ; Fri, 15 Apr 2022 04:52:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE7233858016 for ; Thu, 14 Apr 2022 18:52:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE7233858016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649962366; bh=4+p1vzg359pFE1c7Xf8+FXbhpPMDeM0GkCEhMK98SDc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=GAJVClN5dANBv+y4M9yf75zfrF4nZ0zNn089Gh2/F9NmA+E1nMAA1yheUfDlVc00U KllC23liU1v0xpMYHSw8xxdv5pYhH3bCdYE+DQhl0FsDL1A8eqRm5XQP4Nh2DIlq8G T6tV03VaydyL5pyAKkxWgBhD24HqFeg71IahoCgQ= 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 ESMTPS id 9A0C13858C53 for ; Thu, 14 Apr 2022 18:52:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9A0C13858C53 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-536-VJJhNesDPySbL9P087AYoA-1; Thu, 14 Apr 2022 14:52:03 -0400 X-MC-Unique: VJJhNesDPySbL9P087AYoA-1 Received: by mail-qt1-f197.google.com with SMTP id s17-20020a05622a1a9100b002ed3cb8acb3so3795450qtc.10 for ; Thu, 14 Apr 2022 11:52:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=4+p1vzg359pFE1c7Xf8+FXbhpPMDeM0GkCEhMK98SDc=; b=4Is6Y10xPoRNhDehN8U6IDed7AUGgk10AviVnTcVGK5+utft56zvSEFJIHaCc1LIqG X3Q8J6swosWETcjYVTu83Gasm1kQ3flKoaEK+7atDTFY/4GdW2RV1Ofsjzdm/SmPMj0k Ig51E5GW8h1IvkPXbVieQhhIlQPuK9ruTeNHW9laqBq34EXGzNch+BpEPaMATy28BGQj 6/YIF2/8AhIzgwN1OqChKQgEr1F5ChwKuoXwthriBYTRnBdIidgRkDqVNZYIZiD7pVUB Z5y410LHr1cuWKvctaL12stUqw1NarbhOPTu/85b8nJYOp0fTacGEqLlMAchmd7s+3lP GJTg== X-Gm-Message-State: AOAM532YxxTWoIad9kMFlwYyGK/bG8w9znvMUK7ooYMQsSYcQnfMxl8g 6BqM2+Bw1yC73vdcSgN00PNy4VmfblP56cvP+YakTG2CdcPs03m7zZm5KIdCE301xfs/BnV1YbQ usTAc8XFPBuQdNlLUTAF3r3chVrpMPGA96C0wPvyTnG6SPoM3daSHm+6TnCYwIaz/bA== X-Received: by 2002:a05:620a:4083:b0:67d:a6e3:523c with SMTP id f3-20020a05620a408300b0067da6e3523cmr2920541qko.601.1649962321702; Thu, 14 Apr 2022 11:52:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDt9gClf6uxLEF5FM0Zjs4fTK/5xcsSrPEU2dxJ+wtuKS9UGzIkkpSGYqNmqBM9+6h/ctNEw== X-Received: by 2002:a05:620a:4083:b0:67d:a6e3:523c with SMTP id f3-20020a05620a408300b0067da6e3523cmr2920523qko.601.1649962321244; Thu, 14 Apr 2022 11:52:01 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id x4-20020a05620a258400b0067d47fb5aa4sm1300784qko.63.2022.04.14.11.51.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 11:52:00 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: lambda and the current instantiation [PR82980] Date: Thu, 14 Apr 2022 14:51:57 -0400 Message-Id: <20220414185157.3470106-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When a captured variable is type-dependent, we've expressed the type of the capture field and proxy with a decltype variant. But if the type is "the current instantiation", we need to be able to see that so that we can do lookup inside it just like we could with the captured variable itself. I also tried looking through lambda capture in cp_parser_postfix_dot_deref_expression, but this way seems cleaner. I plan to treat more types as deducible in stage 1. I considered also using this in do_auto_deduction, but think that would be wrong: [temp.dep.expr] says an id-expression is type-dependent if it is "associated by name lookup with a variable declared with a type that contains a placeholder type where the initializer is type-dependent". That doesn't clearly exclude deducing a dependent type from the initializer, but it seems like a barrier, and other implementations agree. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/82980 gcc/cp/ChangeLog: * lambda.cc (type_deducible_expression_p): New. (lambda_capture_field_type): Check it. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-current-inst1.C: New test. --- gcc/cp/lambda.cc | 20 ++++++++++++++++++- .../cpp0x/lambda/lambda-current-inst1.C | 18 +++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C base-commit: d634c5d7c78c6ec0fa39d96984460475564519c8 diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index f22798d51e8..65579edc316 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -183,6 +183,24 @@ lambda_function (tree lambda) return lambda; } +/* True if EXPR is an expression whose type can be used directly in lambda + capture. Not to be used for 'auto'. */ + +static bool +type_deducible_expression_p (tree expr) +{ + if (!type_dependent_expression_p (expr)) + return true; + if (BRACE_ENCLOSED_INITIALIZER_P (expr) + || TREE_CODE (expr) == EXPR_PACK_EXPANSION) + return false; + tree t = non_reference (TREE_TYPE (expr)); + if (!t) return false; + while (TREE_CODE (t) == POINTER_TYPE) + t = TREE_TYPE (t); + return currently_open_class (t); +} + /* Returns the type to use for the FIELD_DECL corresponding to the capture of EXPR. EXPLICIT_INIT_P indicates whether this is a C++14 init capture, and BY_REFERENCE_P indicates whether we're @@ -211,7 +229,7 @@ lambda_capture_field_type (tree expr, bool explicit_init_p, else type = do_auto_deduction (type, expr, auto_node); } - else if (type_dependent_expression_p (expr)) + else if (!type_deducible_expression_p (expr)) { type = cxx_make_type (DECLTYPE_TYPE); DECLTYPE_TYPE_EXPR (type) = expr; diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C new file mode 100644 index 00000000000..a6631c5ca99 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C @@ -0,0 +1,18 @@ +// PR c++/82980 +// { dg-do compile { target c++11 } } + +template +struct Outer +{ + template + void f(); + + void bar(Outer outer) { + [outer](){ outer.f(); }; + } + void baz(Outer *p) { + [&](){ p->f(); }; + } +}; + +int main() { }