public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: unqual lookup performed twice w/ template-id ADL [PR102670]
Date: Wed, 17 Nov 2021 23:17:50 -0500	[thread overview]
Message-ID: <aa3aaeca-2441-8aaa-eed3-7faba7ee5ff9@redhat.com> (raw)
In-Reply-To: <20211103160419.1472321-1-ppalka@redhat.com>

On 11/3/21 12:04, Patrick Palka wrote:
> Here we're incorrectly performing unqualified lookup of 'adl' again at
> substitution time for the call adl<I>(t) (for which name lookup at parse
> time found nothing) which causes us to reject the testcase because the
> second unqualified lookup finds the later-declared variable template
> 'adl', leading to confusion.  Fixed thusly.
> 
> The testcase concepts-recursive-sat1.C needed to be adjusted use ADL
> proper instead of relying on this incorrect behavior.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk and perhaps 11 given it's a C++20 bugfix?

OK for trunk.  Not for 11, I think, as it also affects non-C++20 code.

> 	PR c++/102670
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.c (tsubst_copy_and_build) <case CALL_EXPR>: When looking
> 	for an identifier callee in the koenig_p case, also look through
> 	TEMPLATE_ID_EXPR.  Use tsubst_copy to substitute through the
> 	template arguments of the template-id.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-recursive-sat1.C:
> 	* g++.dg/cpp2a/fn-template23.C: New test.
> ---
>   gcc/cp/pt.c                                   | 11 +++++-
>   .../g++.dg/cpp2a/concepts-recursive-sat1.C    | 15 +++++---
>   gcc/testsuite/g++.dg/cpp2a/fn-template23.C    | 36 +++++++++++++++++++
>   3 files changed, 56 insertions(+), 6 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/fn-template23.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 66040035b2f..40f84648ed2 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -20256,7 +20256,10 @@ tsubst_copy_and_build (tree t,
>   					    /*done=*/false,
>   					    /*address_p=*/false);
>   	  }
> -	else if (koenig_p && identifier_p (function))
> +	else if (koenig_p
> +		 && (identifier_p (function)
> +		     || (TREE_CODE (function) == TEMPLATE_ID_EXPR
> +			 && identifier_p (TREE_OPERAND (function, 0)))))
>   	  {
>   	    /* Do nothing; calling tsubst_copy_and_build on an identifier
>   	       would incorrectly perform unqualified lookup again.
> @@ -20269,6 +20272,12 @@ tsubst_copy_and_build (tree t,
>   	       FIXME but doing that causes c++/15272, so we need to stop
>   	       using IDENTIFIER_NODE in that situation.  */
>   	    qualified_p = false;
> +
> +	    if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
> +	      /* Use tsubst_copy to substitute through the template arguments
> +		 of the template-id without performing unqualified lookup on
> +		 the template name.  */
> +	      function = tsubst_copy (function, args, complain, in_decl);
>   	  }
>   	else
>   	  {
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C
> index 22696c30d81..4c178b77946 100644
> --- a/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C
> @@ -3,16 +3,21 @@
>   template<int N, typename T>
>   concept Foo = requires(T t) { foo<N + 1>(t); }; // { dg-error "template instantiation depth" }
>   
> -template<int N = 1, typename T = int>
> -  requires Foo<N, T>
> -int foo(T t)
> +namespace ns
>   {
> -  return foo<N + 1>(t);
> +  struct S { };
> +
> +  template<int N, typename T>
> +    requires Foo<N, T>
> +  int foo(T t)
> +  {
> +    return foo<N + 1>(t);
> +  }
>   }
>   
>   int main(int, char**)
>   {
> -  return foo<1>(1);
> +  return ns::foo<1>(ns::S{});
>   }
>   
>   // { dg-prune-output "compilation terminated" }
> diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template23.C b/gcc/testsuite/g++.dg/cpp2a/fn-template23.C
> new file mode 100644
> index 00000000000..b85d4c96dab
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/fn-template23.C
> @@ -0,0 +1,36 @@
> +// PR c++/102670
> +// { dg-do compile { target c++20 } }
> +
> +namespace ns {
> +  struct S { };
> +
> +  template<int I>
> +  constexpr int adl(const S &) {
> +    return I;
> +  }
> +}
> +
> +namespace redirect {
> +  template<class T, int I>
> +  concept can_call_adl = requires(T t) {
> +    adl<I>(t);
> +  };
> +
> +  template<int I>
> +  struct adl_fn {
> +    template<can_call_adl<I> T>
> +    constexpr decltype(auto) operator()(T t) const {
> +      return adl<I>(t);
> +    }
> +  };
> +
> +  namespace {
> +    template<int I>
> +    constexpr inline adl_fn<I> adl{};
> +  }
> +}
> +
> +int main() {
> +  static_assert(redirect::can_call_adl<ns::S, 3>);
> +  redirect::adl<3>(ns::S{});
> +}
> 


      parent reply	other threads:[~2021-11-18  4:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 16:04 Patrick Palka
2021-11-10 16:53 ` [PATCH] c++: template-id ADL and partial instantiation [PR99911] Patrick Palka
2021-11-18  5:15   ` Jason Merrill
2021-11-18 14:45     ` Patrick Palka
2021-11-18 15:31       ` Patrick Palka
2021-11-18 16:02         ` Jason Merrill
2021-11-18  4:17 ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aa3aaeca-2441-8aaa-eed3-7faba7ee5ff9@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).