public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5381] c++: error recovery during C++20 template-id ADL failure
Date: Thu, 18 Nov 2021 18:11:41 +0000 (GMT)	[thread overview]
Message-ID: <20211118181141.636AD385AC36@sourceware.org> (raw)

https://gcc.gnu.org/g:53c964ad996a1bb22566b987eafb333b5899deab

commit r12-5381-g53c964ad996a1bb22566b987eafb333b5899deab
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Nov 18 13:10:28 2021 -0500

    c++: error recovery during C++20 template-id ADL failure
    
    When diagnosing ADL failure we try to perform a second unqualified
    lookup for backwards compatibility with legacy code (via -fpermissive),
    and for better diagnostics.
    
    But for C++20 template-id ADL, the backwards compatibility code
    sometimes causes confusing subsequent diagnostics such as in the
    testcase below where we end up diagnosing deduction failure after
    emitting the helpful "no declarations were found by ADL".  This happens
    because the code just discards the arguments of the template-id callee
    when replacing it with the later-declared template, which leads to
    overload resolution failure:
    
      <stdin>: In instantiation of ‘void f() [with T = int]’:
      <stdin>:12:22:   required from here
      <stdin>:5:9: error: ‘g’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
      <stdin>:10:6: note: ‘template<class T> void g(int)’ declared here, later in the translation unit
      <stdin>:5:9: error: no matching function for call to ‘g(int)’
      <stdin>:10:6: note: candidate: ‘template<class T> void g(int)’
      <stdin>:10:6: note:   template argument deduction/substitution failed:
      <stdin>:5:9: note:   couldn’t deduce template parameter ‘T’
    
    So for C++20 template-id ADL, this patch disables the backwards
    compatibility code while keeping the helpful "no declarations were
    found by ADL" diagnostic.
    
    gcc/cp/ChangeLog:
    
            * pt.c (tsubst_copy_and_build) <case CALL_EXPR>: Disable the
            -fpermissive fallback for C++20 template-id ADL, but keep the
            diagnostic.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/fn-template25.C: New test.

Diff:
---
 gcc/cp/pt.c                                | 20 +++++++++++---------
 gcc/testsuite/g++.dg/cpp2a/fn-template25.C | 12 ++++++++++++
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index ad51c07347b..71a771fd0f2 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -20439,7 +20439,8 @@ tsubst_copy_and_build (tree t,
 							    (function, 1))))
 	    && !any_type_dependent_arguments_p (call_args))
 	  {
-	    if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
+	    bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
+	    if (template_id_p)
 	      function = TREE_OPERAND (function, 0);
 	    if (koenig_p && (complain & tf_warning_or_error))
 	      {
@@ -20454,20 +20455,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 +20505,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..5da409c2a1b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/fn-template25.C
@@ -0,0 +1,12 @@
+// { dg-do compile { target c++20 } }
+
+template<class T>
+void f() {
+  g<int>(T{}); // { dg-error "argument-dependent lookup" }
+	       // { dg-bogus "no match" "" { target *-*-* } .-1 }
+}
+
+template<class T>
+void g(int);   // { dg-message "declared here, later" }
+
+template void f<int>();


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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211118181141.636AD385AC36@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).