From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 0BD9E385B83B for ; Thu, 18 Nov 2021 16:03:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0BD9E385B83B Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-366-nBHSK7OvNHqwNKlSYH1aNA-1; Thu, 18 Nov 2021 11:02:34 -0500 X-MC-Unique: nBHSK7OvNHqwNKlSYH1aNA-1 Received: by mail-qk1-f199.google.com with SMTP id o19-20020a05620a22d300b0046754380e8aso5186520qki.13 for ; Thu, 18 Nov 2021 08:02:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=lEgwwPCsw+0In5lW6L0dC3ZWDNlyiLxq2mvImHQEcoU=; b=ig10u24qhMh2ErEYkVVIL1L28InCceqE6EUaxEvhVi9/hy2m5yDbsTqWI25HrG30Vo AmKlHT6xEkR7NC/8fU5qrppchXIPU6qqCON1XnoYb17xWmikazetiB9tN7i//3gvmd6Q mE6lgaW8S82CKvV2k20MOiM+BpKqgmy3IK/4y5CJt+TGP86kGDMNhN3RBFfY23YupGN1 tb7w5d6AT4XUeGFnqHpVZWPvbxNgtTjZaClT7ysYW7XcSTblGxZUE87Dwa1w7ONwMmaw EDq5sNW+Tp+DZLBpjceK/IMBst0g4IOTLdfgFx+Oi75YWEE6yZNiV4u5s13OdRv5J/tp zaBw== X-Gm-Message-State: AOAM530H7oo2xzQzrH5mNPyDqvWdOHjdEUruM1EpPZAPaIYnoNryYz14 k/TseEWv4m6ULQRuB/hxB7zj75OdzROAPB2nxQQh+ppnmjuiAwrxGn1SUcx1oWASPPYmtX/UDGi h5s7XWV3DFlTOMPWGNg== X-Received: by 2002:a05:620a:3714:: with SMTP id de20mr21332826qkb.255.1637251352902; Thu, 18 Nov 2021 08:02:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJxbH0O8vfufhXY4xJGoeY1hUbVWVOEgLClAPGmvjaHoL8BRS8w+VTbzK1GEhK4fOoLNyOXh7Q== X-Received: by 2002:a05:620a:3714:: with SMTP id de20mr21332797qkb.255.1637251352569; Thu, 18 Nov 2021 08:02:32 -0800 (PST) Received: from [192.168.1.149] (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 l21sm126811qkk.1.2021.11.18.08.02.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 18 Nov 2021 08:02:31 -0800 (PST) Message-ID: <3399f858-a67e-d0ba-286e-1ba4ed3f08c3@redhat.com> Date: Thu, 18 Nov 2021 11:02:22 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.3.1 Subject: Re: [PATCH] c++: template-id ADL and partial instantiation [PR99911] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20211103160419.1472321-1-ppalka@redhat.com> <20211110165307.4079746-1-ppalka@redhat.com> <62b9802-6ed1-5c8b-9da7-bca4a4822be@idea> From: Jason Merrill In-Reply-To: <62b9802-6ed1-5c8b-9da7-bca4a4822be@idea> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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-List-Received-Date: Thu, 18 Nov 2021 16:03:31 -0000 On 11/18/21 10:31, Patrick Palka wrote: > On Thu, 18 Nov 2021, Patrick Palka wrote: > >> On Thu, 18 Nov 2021, Jason Merrill wrote: >> >>> On 11/10/21 11:53, Patrick Palka wrote: >>>> Here when partially instantiating the call get(T{}) with T=N::A >>>> (for which earlier unqualified name lookup for 'get' found nothing) >>>> the arguments after substitution are no longer dependent but the callee >>>> still is, so perform_koenig_lookup postpones ADL. But then we go on to >>>> diagnose the unresolved template name anyway, as if ADL was already >>>> performed and failed. >>>> >>>> This patch fixes this by avoiding the error path in question when the >>>> template arguments of an unresolved template-id are dependent, which >>>> mirrors the dependence check in perform_koenig_lookup. >>> >>> This change is OK. >>> >>>> In passing, this >>>> patch also disables the -fpermissive fallback that performs a second >>>> unqualified lookup in the template-id ADL case; this fallback seems to be >>>> intended for legacy code and shouldn't be used for C++20 template-id ADL. >>> >>> Why wouldn't we want the more helpful diagnostic? >> >> The "no declarations were found by ADL" diagnostic is helpful, but the >> backwards compatibility logic doesn't correctly handle the template-id >> case. E.g. for >> >> template >> void f() { >> g(T{}); >> } >> >> template >> void g(int); // #1 >> >> int main() { >> f(); >> } >> >> we get the helpful diagnostic followed by a confusing one because we >> didn't incorporate the template-id's template arguments when replacing >> the callee with the later-declared template #1: >> >> : In instantiation of ‘void f() [with T = int]’: >> :10:9: required from here >> :3:6: error: ‘g’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] >> :7:6: note: ‘template void g(int)’ declared here, later in the translation unit >> :3:6: error: no matching function for call to ‘g(int)’ >> :7:6: note: candidate: ‘template void g(int)’ >> :7:6: note: template argument deduction/substitution failed: >> :3:6: note: couldn’t deduce template parameter ‘T’ >> >> >> We also ignores template-ness of the name being looked up, so e.g. for: >> >> template >> void f() { >> g<>(T{}); >> } >> >> void g(int); // #1 >> >> int main() { >> f(); >> } >> >> the secondary unqualified lookup finds the later-declared non-template #1: >> >> : In instantiation of ‘void f() [with T = int]’: >> :9:9: required from here >> :3:6: error: ‘g’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] >> :6:6: note: ‘void g(int)’ declared here, later in the translation unit >> >> which doesn't seem right. >> >> To fix the first issue, rather than disabling the diagnostic perhaps we >> should just disable the backwards compatibility logic in the template-id >> case, as in the below? > > Now in patch form: > > -- >8 -- > > Subject: [PATCH] c++: error recovery during C++20 template-id ADL failure > > When diagnosing ADL failure we perform a second unqualified lookup for > backwards compatibility with legacy code, and for better diagnostics. > > For C++20 template-id ADL however, the backwards compatibility logic > causes confusing subsequent diagnostics, such as in the testcase below > where we report deduction failure following the useful "no declarations > were found by ADL" diagnostic because we've discarded the arguments of > the template-id when replacing it with the later-declared template. > > So for C++20 template-id ADL, this patch just disables the backwards > compatibility code while keeping the useful diagnostic. > > gcc/cp/ChangeLog: > > * pt.c (tsubst_copy_and_build) : Disable the > -fpermissive fallback for template-id ADL, but keep the > diagnostic. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/fn-template25.C: New test. > --- > gcc/cp/pt.c | 23 +++++++++++++--------- > gcc/testsuite/g++.dg/cpp2a/fn-template25.C | 14 +++++++++++++ > 2 files changed, 28 insertions(+), 9 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/fn-template25.C > > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index ad51c07347b..3f1550a17ad 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -20439,8 +20439,12 @@ tsubst_copy_and_build (tree t, > (function, 1)))) > && !any_type_dependent_arguments_p (call_args)) > { > + bool template_id_p = false; > if (TREE_CODE (function) == TEMPLATE_ID_EXPR) > - function = TREE_OPERAND (function, 0); > + { > + function = TREE_OPERAND (function, 0); > + template_id_p = true; > + } I think bool template_id_p = TREE_CODE (function) == TEMPLATE_ID_EXPR; would simplify this. OK either way. > if (koenig_p && (complain & tf_warning_or_error)) > { > /* For backwards compatibility and good diagnostics, try > @@ -20454,20 +20458,21 @@ tsubst_copy_and_build (tree t, > > if (unq != function) > { > - /* In a lambda fn, we have to be careful to not > - introduce new this captures. Legacy code can't > - be using lambdas anyway, so it's ok to be > - stricter. */ > - bool in_lambda = (current_class_type > - && LAMBDA_TYPE_P (current_class_type)); > char const *const msg > = G_("%qD was not declared in this scope, " > "and no declarations were found by " > "argument-dependent lookup at the point " > "of instantiation"); > > + bool in_lambda = (current_class_type > + && LAMBDA_TYPE_P (current_class_type)); > + /* In a lambda fn, we have to be careful to not > + introduce new this captures. Legacy code can't > + be using lambdas anyway, so it's ok to be > + stricter. Be strict with C++20 template-id ADL too. */ > + bool strict = in_lambda || template_id_p; > bool diag = true; > - if (in_lambda) > + if (strict) > error_at (cp_expr_loc_or_input_loc (t), > msg, function); > else > @@ -20503,7 +20508,7 @@ tsubst_copy_and_build (tree t, > inform (DECL_SOURCE_LOCATION (fn), > "%qD declared here, later in the " > "translation unit", fn); > - if (in_lambda) > + if (strict) > RETURN (error_mark_node); > } > > diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template25.C b/gcc/testsuite/g++.dg/cpp2a/fn-template25.C > new file mode 100644 > index 00000000000..a8888af2023 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/fn-template25.C > @@ -0,0 +1,14 @@ > +// { dg-do compile { target c++20 } } > + > +template > +void f() { > + g(T{}); // { dg-error "argument-dependent lookup" } > + // { dg-bogus "no match" "" { target *-*-* } .-1 } > +} > + > +template > +void g(int); // { dg-message "declared here, later" } > + > +int main() { > + f(); > +} >