From patchwork Thu Apr 14 00:24: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: 1616999 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=qzUunqKn; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kf0dn30kkz9sFx for ; Thu, 14 Apr 2022 10:27:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BD9A3858016 for ; Thu, 14 Apr 2022 00:27:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BD9A3858016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649896027; bh=LzcEYC7sEc429yt74cHnhm56/cm9I4T3+jiv4NMX/Vs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qzUunqKnCz4JLkXCKLla5us2eeLN3EYqjNf6oeKwAlfgPFoHNWwW7gOQh59jRnTwM 7qtn0qVykoSWP/KJs6uy0MYOXeTgx1wTtj5S6Fcs5nF9GunfRpKRa/7CL3UUCw7Zvn CtWjmpOLhekQRMJHz+8dlRB0LMaCJ029hgScE2KA= 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 3B0893857822 for ; Thu, 14 Apr 2022 00:25:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3B0893857822 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-628-tcyCRqOiMISuXXnvTLkytA-1; Wed, 13 Apr 2022 20:25:02 -0400 X-MC-Unique: tcyCRqOiMISuXXnvTLkytA-1 Received: by mail-qk1-f197.google.com with SMTP id bp31-20020a05620a459f00b00699fabcc554so2234142qkb.12 for ; Wed, 13 Apr 2022 17:25:02 -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=LzcEYC7sEc429yt74cHnhm56/cm9I4T3+jiv4NMX/Vs=; b=Rmp91QhsSzgjgTXDHFpKis2/0SVP7q6v3PSxwqr0ZPV9UHLtNQt9syB5F6Vc7pVdy9 0sTjzOTMkjbSfT9FYMODJuPmjcJZfd+ZnsBfBp9Sz6wV3akaGUR5MH6vkXan6Zc1Tv8s y12j74MwFcN1IqaEL0znSBvh774nHeeHGeNv2mUD6BWNWZ4USxnFiL7vNnX/6+86swkM OQtoYfzYxCroogO0kVxCxCHix9DF9vSH0yUOlhtQnq5sdWOJc3giKNP0BBCA5T2UZFeR Pe0xL7IK2iArqXnvGHxjfGAId9EFiknx1a7uvY8T5/BI4udC2pU2g8ALniFpIBIZUMus BslA== X-Gm-Message-State: AOAM530sFhg12MMo+PwokchbnkX1sr9TXYDl6y6TjRlJRSb0H8yhS8gf 2oNSuHYbKXUPbgeCbj4NKi7GYyXhpsndewtPJtiTvEL7iIa4t0t4i/3Wg7VvFciusSc8BkTlhC7 nvIKxbjy+PjDU+d4e9UB7aBoEyhp9yg9rUHSJqZ0urkuRMDe4FKdUb/6+iTbntD9cjQ== X-Received: by 2002:a37:b3c5:0:b0:69c:52ee:3a93 with SMTP id c188-20020a37b3c5000000b0069c52ee3a93mr138260qkf.640.1649895902025; Wed, 13 Apr 2022 17:25:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyk2vMSAre2CPWStQnU5ZJSLcacrZj9hdfq+I5hYmO+UTKlDjJQwnGkIzuPZB/PSBmvuXg+0A== X-Received: by 2002:a37:b3c5:0:b0:69c:52ee:3a93 with SMTP id c188-20020a37b3c5000000b0069c52ee3a93mr138242qkf.640.1649895901566; Wed, 13 Apr 2022 17:25: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 i62-20020a37b841000000b0069c10d27571sm204067qkf.70.2022.04.13.17.25.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 17:25:00 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: local fn and generic lambda [PR97219] Date: Wed, 13 Apr 2022 20:24:57 -0400 Message-Id: <20220414002457.3271572-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 instantiating the op() for a generic lambda, we can no longer do name lookup inside function scopes enclosing the lambda, so we need to remember the lookup result from processing the definition of the lambda. So the code in finish_call_expr to throw away the lookup result and instead look it up again at instantiation time needs to be adjusted. The approach I take is to only discard the result if the local extern comes from dependent scope; once the enclosing function template is instantiated and we're regenerating the lambda, then we can remember the result of lookup. We also need any default arguments to be instantiated at that point. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/97219 gcc/cp/ChangeLog: * name-lookup.cc (dependent_local_decl_p): New. * cp-tree.h (dependent_local_decl_p): Declare. * semantics.cc (finish_call_expr): Use it. * pt.cc (tsubst_arg_types): Also substitute default args for local externs. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/lambda-generic-local-fn1.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/name-lookup.cc | 18 ++++++++++++++++++ gcc/cp/pt.cc | 4 +++- gcc/cp/semantics.cc | 6 +++--- .../g++.dg/cpp1y/lambda-generic-local-fn1.C | 17 +++++++++++++++++ 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-generic-local-fn1.C base-commit: d4e00ccef6c706a4a4a6446bffaf4111f98d5771 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2f718852ac1..e9a3d09ac4c 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8243,6 +8243,7 @@ extern tree fold_builtin_source_location (location_t); /* in name-lookup.cc */ extern tree strip_using_decl (tree); extern void diagnose_name_conflict (tree, tree); +extern bool dependent_local_decl_p (tree); /* Tell the binding oracle what kind of binding we are looking for. */ diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 118da0950af..7b0638d3166 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -8976,4 +8976,22 @@ cp_emit_debug_info_for_using (tree t, tree context) } } +/* True if D is a local declaration in dependent scope. Assumes that it is + (part of) the current lookup result for its name. */ + +bool +dependent_local_decl_p (tree d) +{ + if (!DECL_LOCAL_DECL_P (d)) + return false; + + cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d)); + cp_binding_level *l = b->scope; + while (!l->this_entity) + l = l->level_chain; + return uses_template_parms (l->this_entity); +} + + + #include "gt-cp-name-lookup.h" diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 63c08706377..adc863de702 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -15182,7 +15182,9 @@ tsubst_arg_types (tree arg_types, /* Except that we do substitute default arguments under tsubst_lambda_expr, since the new op() won't have any associated template arguments for us to refer to later. */ - if (lambda_fn_in_template_p (in_decl)) + if (lambda_fn_in_template_p (in_decl) + || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL + && DECL_LOCAL_DECL_P (in_decl))) default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl, false/*fn*/, false/*constexpr*/); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index f5ec808b1bc..f08c0b6281f 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -2690,13 +2690,13 @@ finish_call_expr (tree fn, vec **args, bool disallow_virtual, if (processing_template_decl) { - /* If FN is a local extern declaration or set thereof, look them up - again at instantiation time. */ + /* If FN is a local extern declaration (or set thereof) in a template, + look it up again at instantiation time. */ if (is_overloaded_fn (fn)) { tree ifn = get_first_fn (fn); if (TREE_CODE (ifn) == FUNCTION_DECL - && DECL_LOCAL_DECL_P (ifn)) + && dependent_local_decl_p (ifn)) orig_fn = DECL_NAME (ifn); } diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-local-fn1.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-local-fn1.C new file mode 100644 index 00000000000..06919c535d8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-local-fn1.C @@ -0,0 +1,17 @@ +// PR c++/97219 +// { dg-do compile { target c++14 } } + +struct B; + +template +auto f(T *) { + void q(B *, void * = static_cast(0)); + return [](auto *p) { q(p); }; +} + +void q(void *) = delete; + +int main(void) { + B *bp = 0; + f(bp)(bp); +}