diff mbox series

c++: typename in out-of-class member function definitions [PR97297]

Message ID 20201005230712.3830459-1-polacek@redhat.com
State New
Headers show
Series c++: typename in out-of-class member function definitions [PR97297] | expand

Commit Message

Marek Polacek Oct. 5, 2020, 11:07 p.m. UTC
I was notified that our P0634R3 (Down with typename) implementation has
a flaw: when we have an out-of-class member function definition, we
still required 'typename' for its parameters.  For example here:

  template <typename T> struct S {
    int simple(T::type);
  };
  template <typename T>
  int S<T>::simple(/* typename */T::type) { return 0; }

the 'typename' isn't necessary per [temp.res]/5.2.4.  We have a qualified
name here ("S<T>::simple") so we know it's already been declared so we
can look it up to see if it's a function template or a variable
template.

In this case, the P0634R3 code in cp_parser_direct_declarator wasn't
looking into uninstantiated templates and didn't find the member
function 'simple' -- cp_parser_lookup_name returned a SCOPE_REF which
means that the qualifying scope was dependent.  With this fix, we find
the BASELINK for 'simple', don't clear CP_PARSER_FLAGS_TYPENAME_OPTIONAL
from the flags, and the typename is implicitly assumed.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

gcc/cp/ChangeLog:

	PR c++/97297
	* parser.c (cp_parser_direct_declarator): When checking if a
	name is a function template declaration for the P0634R3 case,
	look in uninstantiated templates too.

gcc/testsuite/ChangeLog:

	PR c++/97297
	* g++.dg/cpp2a/typename18.C: New test.
---
 gcc/cp/parser.c                         | 10 ++++++++--
 gcc/testsuite/g++.dg/cpp2a/typename18.C | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename18.C


base-commit: ea6da7f50fe2adc3a09fc10a3f437902c40ebff9

Comments

Jason Merrill Oct. 6, 2020, 9:26 p.m. UTC | #1
On 10/5/20 7:07 PM, Marek Polacek wrote:
> I was notified that our P0634R3 (Down with typename) implementation has
> a flaw: when we have an out-of-class member function definition, we
> still required 'typename' for its parameters.  For example here:
> 
>    template <typename T> struct S {
>      int simple(T::type);
>    };
>    template <typename T>
>    int S<T>::simple(/* typename */T::type) { return 0; }
> 
> the 'typename' isn't necessary per [temp.res]/5.2.4.  We have a qualified
> name here ("S<T>::simple") so we know it's already been declared so we
> can look it up to see if it's a function template or a variable
> template.
> 
> In this case, the P0634R3 code in cp_parser_direct_declarator wasn't
> looking into uninstantiated templates and didn't find the member
> function 'simple' -- cp_parser_lookup_name returned a SCOPE_REF which
> means that the qualifying scope was dependent.  With this fix, we find
> the BASELINK for 'simple', don't clear CP_PARSER_FLAGS_TYPENAME_OPTIONAL
> from the flags, and the typename is implicitly assumed.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> gcc/cp/ChangeLog:
> 
> 	PR c++/97297
> 	* parser.c (cp_parser_direct_declarator): When checking if a
> 	name is a function template declaration for the P0634R3 case,
> 	look in uninstantiated templates too.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/97297
> 	* g++.dg/cpp2a/typename18.C: New test.
> ---
>   gcc/cp/parser.c                         | 10 ++++++++--
>   gcc/testsuite/g++.dg/cpp2a/typename18.C | 21 +++++++++++++++++++++
>   2 files changed, 29 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename18.C
> 
> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
> index cb4422764ed..2002c05fdb5 100644
> --- a/gcc/cp/parser.c
> +++ b/gcc/cp/parser.c
> @@ -21788,8 +21788,14 @@ cp_parser_direct_declarator (cp_parser* parser,
>   		       templates, assume S::p to name a type.  Otherwise,
>   		       don't.  */
>   		    tree decl
> -		      = cp_parser_lookup_name_simple (parser, unqualified_name,
> -						      token->location);
> +		      = cp_parser_lookup_name (parser, unqualified_name,
> +					       none_type,
> +					       /*is_template=*/false,
> +					       /*is_namespace=*/false,
> +					       /*check_dependency=*/false,
> +					       /*ambiguous_decls=*/NULL,
> +					       token->location);
> +
>   		    if (!is_overloaded_fn (decl)
>   			/* Allow
>   			   template<typename T>
> diff --git a/gcc/testsuite/g++.dg/cpp2a/typename18.C b/gcc/testsuite/g++.dg/cpp2a/typename18.C
> new file mode 100644
> index 00000000000..99468661491
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/typename18.C
> @@ -0,0 +1,21 @@
> +// PR c++/97297
> +// { dg-do compile { target c++20 } }
> +
> +template <typename T>
> +struct S {
> +    int simple(T::type);
> +
> +    template <typename U>
> +    int member(U::type);
> +};
> +
> +template <typename T>
> +int S<T>::simple(T::type) {
> +    return 1;
> +}
> +
> +template <typename T>
> +template <typename U>
> +int S<T>::member(U::type) {
> +    return 2;
> +}
> 
> base-commit: ea6da7f50fe2adc3a09fc10a3f437902c40ebff9
>
diff mbox series

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index cb4422764ed..2002c05fdb5 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -21788,8 +21788,14 @@  cp_parser_direct_declarator (cp_parser* parser,
 		       templates, assume S::p to name a type.  Otherwise,
 		       don't.  */
 		    tree decl
-		      = cp_parser_lookup_name_simple (parser, unqualified_name,
-						      token->location);
+		      = cp_parser_lookup_name (parser, unqualified_name,
+					       none_type,
+					       /*is_template=*/false,
+					       /*is_namespace=*/false,
+					       /*check_dependency=*/false,
+					       /*ambiguous_decls=*/NULL,
+					       token->location);
+
 		    if (!is_overloaded_fn (decl)
 			/* Allow
 			   template<typename T>
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename18.C b/gcc/testsuite/g++.dg/cpp2a/typename18.C
new file mode 100644
index 00000000000..99468661491
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/typename18.C
@@ -0,0 +1,21 @@ 
+// PR c++/97297
+// { dg-do compile { target c++20 } }
+
+template <typename T>
+struct S {
+    int simple(T::type);
+
+    template <typename U>
+    int member(U::type);
+};
+
+template <typename T>
+int S<T>::simple(T::type) {
+    return 1;
+}
+
+template <typename T>
+template <typename U>
+int S<T>::member(U::type) {
+    return 2;
+}