From: Marek Polacek Date: Thu, 10 Sep 2020 21:27:43 +0000 (-0400) Subject: c++: Fix P0846 (ADL and function templates) in template [PR97010] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=635072248a426c933c74ef4431e82401249b6218;p=gcc.git c++: Fix P0846 (ADL and function templates) in template [PR97010] To quickly recap, P0846 says that a name is also considered to refer to a template if it is an unqualified-id followed by a < and name lookup finds either one or more functions or finds nothing. In a template, when parsing a function call that has type-dependent arguments, we can't perform ADL right away so we set KOENIG_LOOKUP_P in the call to remember to do it when instantiating the call (tsubst_copy_and_build/CALL_EXPR). When the called function is a function template, we represent the call with a TEMPLATE_ID_EXPR; usually the operand is an OVERLOAD. In the P0846 case though, the operand can be an IDENTIFIER_NODE, when name lookup found nothing when parsing the template name. But we weren't handling this correctly in tsubst_copy_and_build. First we need to pass the FUNCTION_P argument from to , otherwise we give a bogus error. And then in we need to perform ADL. The rest of the changes is to give better errors when ADL didn't find anything. gcc/cp/ChangeLog: PR c++/97010 * pt.c (tsubst_copy_and_build) : Call tsubst_copy_and_build explicitly instead of using the RECUR macro. Handle a TEMPLATE_ID_EXPR with an IDENTIFIER_NODE as its operand. : Perform ADL for a TEMPLATE_ID_EXPR with an IDENTIFIER_NODE as its operand. gcc/testsuite/ChangeLog: PR c++/97010 * g++.dg/cpp2a/fn-template21.C: New test. * g++.dg/cpp2a/fn-template22.C: New test. --- diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 3755aab092d..d9cc7766a59 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -19278,7 +19278,8 @@ out: } /* Like tsubst but deals with expressions and performs semantic - analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ + analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or + "F (ARGS)". */ tree tsubst_copy_and_build (tree t, @@ -19360,7 +19361,10 @@ tsubst_copy_and_build (tree t, case TEMPLATE_ID_EXPR: { tree object; - tree templ = RECUR (TREE_OPERAND (t, 0)); + tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, + complain, in_decl, + function_p, + integral_constant_expression_p); tree targs = TREE_OPERAND (t, 1); if (targs) @@ -19407,13 +19411,21 @@ tsubst_copy_and_build (tree t, } else object = NULL_TREE; - templ = lookup_template_function (templ, targs); + + tree tid = lookup_template_function (templ, targs); if (object) - RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ), - object, templ, NULL_TREE)); + RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid), + object, tid, NULL_TREE)); + else if (identifier_p (templ)) + { + /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when + name lookup found nothing when parsing the template name. */ + gcc_assert (cxx_dialect >= cxx20 || seen_error ()); + RETURN (tid); + } else - RETURN (baselink_for_fns (templ)); + RETURN (baselink_for_fns (tid)); } case INDIRECT_REF: @@ -20004,14 +20016,17 @@ tsubst_copy_and_build (tree t, /* We do not perform argument-dependent lookup if normal lookup finds a non-function, in accordance with the - expected resolution of DR 218. */ + resolution of DR 218. */ if (koenig_p && ((is_overloaded_fn (function) /* If lookup found a member function, the Koenig lookup is not appropriate, even if an unqualified-name was used to denote the function. */ && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) - || identifier_p (function)) + || identifier_p (function) + /* C++20 P0846: Lookup found nothing. */ + || (TREE_CODE (function) == TEMPLATE_ID_EXPR + && identifier_p (TREE_OPERAND (function, 0)))) /* Only do this when substitution turns a dependent call into a non-dependent call. */ && type_dependent_expression_p_push (t) @@ -20019,9 +20034,13 @@ tsubst_copy_and_build (tree t, function = perform_koenig_lookup (function, call_args, tf_none); if (function != NULL_TREE - && identifier_p (function) + && (identifier_p (function) + || (TREE_CODE (function) == TEMPLATE_ID_EXPR + && identifier_p (TREE_OPERAND (function, 0)))) && !any_type_dependent_arguments_p (call_args)) { + if (TREE_CODE (function) == TEMPLATE_ID_EXPR) + function = TREE_OPERAND (function, 0); if (koenig_p && (complain & tf_warning_or_error)) { /* For backwards compatibility and good diagnostics, try diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template21.C b/gcc/testsuite/g++.dg/cpp2a/fn-template21.C new file mode 100644 index 00000000000..7b3f26bbfa0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/fn-template21.C @@ -0,0 +1,24 @@ +// PR c++/97010 +// { dg-do compile { target c++20 } } + +namespace M { + struct S { int x; }; + S foo (); + + template + void get (S); +} + +template +void bar (const T& t) +{ + get(t); +} + +int +main () +{ + auto a = M::foo (); + get(a); + bar (a); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template22.C b/gcc/testsuite/g++.dg/cpp2a/fn-template22.C new file mode 100644 index 00000000000..62cc81db0c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/fn-template22.C @@ -0,0 +1,25 @@ +// PR c++/97010 +// { dg-do compile { target c++20 } } + +namespace M { + struct S { int x; }; + S foo (); + +// Test not-found-by-ADL scenario. +// template +// void get (S); +} + +template +void bar (const T& t) +{ + get(t); // { dg-error ".get. was not declared in this scope" } +} + +int +main () +{ + auto a = M::foo (); + get(a); // { dg-error ".get. was not declared in this scope" } + bar (a); +}