Re: [PATCH] c++: ICE with bogus late return type [PR99803]

2021-04-15 Thread Jason Merrill via Gcc-patches

On 4/15/21 10:02 PM, Marek Polacek wrote:

On Thu, Apr 15, 2021 at 03:31:24PM -0400, Jason Merrill wrote:

On 4/14/21 9:21 PM, Marek Polacek wrote:

Here we ICE when compiling this code in C++20, because we're trying to
slam a 'typename' after the ->.  The cp_parser_template_id call just
before the spot I'm changing parsed A::template A as a BASELINK
that contains a constructor, but make_typename_type crashes on that.

My fix is the same as c++/88325, add an is_overloaded_fn check.


Instead of handling this in various callers, maybe make_typename_type should
handle the error, like it already does for e.g.

   struct A {  template  static T t; };
   A(unsigned) -> A:: template t;


Okay, done.
  

Incidentally, in the testcase for 88325:


A::A () // { dg-error "partial specialization" }


this error is misleading; this isn't a partial specialization, it's
redundant template arguments while trying to define the primary template.  I
wouldn't worry about fixing the compiler now, but let's not test for the
wrong error.


Makes sense.

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


OK.


-- >8 --
Here we ICE when compiling this code in C++20, because we're trying to
slam a 'typename' after the ->.  The cp_parser_template_id call just
before the spot I'm changing parsed A::template A as a BASELINK
that contains a constructor, but make_typename_type crashes on that.

This patch makes make_typename_type more robust instead of checking
for is_overloaded_fn prior calling it.

gcc/cp/ChangeLog:

PR c++/99803
* decl.c (make_typename_type): Give an error and return when
name is is_overloaded_fn.
* parser.c (cp_parser_class_name): Don't check is_overloaded_fn
before calling make_typename_type.

gcc/testsuite/ChangeLog:

PR c++/99803
* g++.dg/cpp2a/typename14.C: Don't expect particular error
messages.
* g++.dg/cpp2a/typename19.C: New test.
---
  gcc/cp/decl.c   | 8 +++-
  gcc/cp/parser.c | 4 +---
  gcc/testsuite/g++.dg/cpp2a/typename14.C | 4 ++--
  gcc/testsuite/g++.dg/cpp2a/typename19.C | 5 +
  4 files changed, 15 insertions(+), 6 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename19.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 1cb47313923..6668a65acbf 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4055,6 +4055,12 @@ make_typename_type (tree context, tree name, enum 
tag_types tag_type,
error ("%qD used without template arguments", name);
return error_mark_node;
  }
+  else if (is_overloaded_fn (name))
+{
+  if (complain & tf_error)
+   error ("%qD is a function, not a type", name);
+  return error_mark_node;
+}
gcc_assert (identifier_p (name));
gcc_assert (TYPE_P (context));
  
@@ -4066,7 +4072,7 @@ make_typename_type (tree context, tree name, enum tag_types tag_type,

error ("%q#T is not a class", context);
return error_mark_node;
  }
-
+
/* When the CONTEXT is a dependent type,  NAME could refer to a
   dependent base class of CONTEXT.  But look inside it anyway
   if CONTEXT is a currently open scope, in case it refers to a
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 940751b5f05..3640ae51331 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -24730,9 +24730,7 @@ cp_parser_class_name (cp_parser *parser,
decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
  
/* If this is a typename, create a TYPENAME_TYPE.  */

-  if (typename_p
-  && decl != error_mark_node
-  && !is_overloaded_fn (decl))
+  if (typename_p && decl != error_mark_node)
  {
decl = make_typename_type (scope, decl, typename_type,
 /*complain=*/tf_error);
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename14.C 
b/gcc/testsuite/g++.dg/cpp2a/typename14.C
index 8d82b6b8d34..ba7dad8245f 100644
--- a/gcc/testsuite/g++.dg/cpp2a/typename14.C
+++ b/gcc/testsuite/g++.dg/cpp2a/typename14.C
@@ -8,7 +8,7 @@ template struct A
  
  template

  template
-A::A () // { dg-error "partial specialization" }
+A::A () // { dg-error "" }
  {
  }
  
@@ -19,7 +19,7 @@ template struct B
  
  template

  template
-B::foo(int) // { dg-error "partial specialization|declaration" }
+B::foo(int) // { dg-error "" }
  {
return 1;
  }
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename19.C 
b/gcc/testsuite/g++.dg/cpp2a/typename19.C
new file mode 100644
index 000..320a14d6a0c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/typename19.C
@@ -0,0 +1,5 @@
+// PR c++/99803
+// { dg-do compile { target c++20 } }
+
+struct A { template A(T); };
+auto A(unsigned) -> A::template A; // { dg-error "not a type" }

base-commit: ee351f7fdbd82f8947fe9a0e74cea65d216a8549





Re: [PATCH] c++: ICE with bogus late return type [PR99803]

2021-04-15 Thread Marek Polacek via Gcc-patches
On Thu, Apr 15, 2021 at 03:31:24PM -0400, Jason Merrill wrote:
> On 4/14/21 9:21 PM, Marek Polacek wrote:
> > Here we ICE when compiling this code in C++20, because we're trying to
> > slam a 'typename' after the ->.  The cp_parser_template_id call just
> > before the spot I'm changing parsed A::template A as a BASELINK
> > that contains a constructor, but make_typename_type crashes on that.
> > 
> > My fix is the same as c++/88325, add an is_overloaded_fn check.
> 
> Instead of handling this in various callers, maybe make_typename_type should
> handle the error, like it already does for e.g.
> 
>   struct A {  template  static T t; };
>   A(unsigned) -> A:: template t;

Okay, done.
 
> Incidentally, in the testcase for 88325:
> 
> > A::A () // { dg-error "partial specialization" }
> 
> this error is misleading; this isn't a partial specialization, it's
> redundant template arguments while trying to define the primary template.  I
> wouldn't worry about fixing the compiler now, but let's not test for the
> wrong error.

Makes sense.

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

-- >8 --
Here we ICE when compiling this code in C++20, because we're trying to
slam a 'typename' after the ->.  The cp_parser_template_id call just
before the spot I'm changing parsed A::template A as a BASELINK
that contains a constructor, but make_typename_type crashes on that.

This patch makes make_typename_type more robust instead of checking
for is_overloaded_fn prior calling it.

gcc/cp/ChangeLog:

PR c++/99803
* decl.c (make_typename_type): Give an error and return when
name is is_overloaded_fn.
* parser.c (cp_parser_class_name): Don't check is_overloaded_fn
before calling make_typename_type.

gcc/testsuite/ChangeLog:

PR c++/99803
* g++.dg/cpp2a/typename14.C: Don't expect particular error
messages.
* g++.dg/cpp2a/typename19.C: New test.
---
 gcc/cp/decl.c   | 8 +++-
 gcc/cp/parser.c | 4 +---
 gcc/testsuite/g++.dg/cpp2a/typename14.C | 4 ++--
 gcc/testsuite/g++.dg/cpp2a/typename19.C | 5 +
 4 files changed, 15 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename19.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 1cb47313923..6668a65acbf 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4055,6 +4055,12 @@ make_typename_type (tree context, tree name, enum 
tag_types tag_type,
error ("%qD used without template arguments", name);
   return error_mark_node;
 }
+  else if (is_overloaded_fn (name))
+{
+  if (complain & tf_error)
+   error ("%qD is a function, not a type", name);
+  return error_mark_node;
+}
   gcc_assert (identifier_p (name));
   gcc_assert (TYPE_P (context));
 
@@ -4066,7 +4072,7 @@ make_typename_type (tree context, tree name, enum 
tag_types tag_type,
error ("%q#T is not a class", context);
   return error_mark_node;
 }
-  
+
   /* When the CONTEXT is a dependent type,  NAME could refer to a
  dependent base class of CONTEXT.  But look inside it anyway
  if CONTEXT is a currently open scope, in case it refers to a
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 940751b5f05..3640ae51331 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -24730,9 +24730,7 @@ cp_parser_class_name (cp_parser *parser,
   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
 
   /* If this is a typename, create a TYPENAME_TYPE.  */
-  if (typename_p
-  && decl != error_mark_node
-  && !is_overloaded_fn (decl))
+  if (typename_p && decl != error_mark_node)
 {
   decl = make_typename_type (scope, decl, typename_type,
 /*complain=*/tf_error);
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename14.C 
b/gcc/testsuite/g++.dg/cpp2a/typename14.C
index 8d82b6b8d34..ba7dad8245f 100644
--- a/gcc/testsuite/g++.dg/cpp2a/typename14.C
+++ b/gcc/testsuite/g++.dg/cpp2a/typename14.C
@@ -8,7 +8,7 @@ template struct A
 
 template
 template
-A::A () // { dg-error "partial specialization" }
+A::A () // { dg-error "" }
 {
 }
 
@@ -19,7 +19,7 @@ template struct B
 
 template
 template
-B::foo(int) // { dg-error "partial specialization|declaration" }
+B::foo(int) // { dg-error "" }
 {
   return 1;
 }
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename19.C 
b/gcc/testsuite/g++.dg/cpp2a/typename19.C
new file mode 100644
index 000..320a14d6a0c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/typename19.C
@@ -0,0 +1,5 @@
+// PR c++/99803
+// { dg-do compile { target c++20 } }
+
+struct A { template A(T); };
+auto A(unsigned) -> A::template A; // { dg-error "not a type" }

base-commit: ee351f7fdbd82f8947fe9a0e74cea65d216a8549
-- 
2.30.2



Re: [PATCH] c++: ICE with bogus late return type [PR99803]

2021-04-15 Thread Jason Merrill via Gcc-patches

On 4/14/21 9:21 PM, Marek Polacek wrote:

Here we ICE when compiling this code in C++20, because we're trying to
slam a 'typename' after the ->.  The cp_parser_template_id call just
before the spot I'm changing parsed A::template A as a BASELINK
that contains a constructor, but make_typename_type crashes on that.

My fix is the same as c++/88325, add an is_overloaded_fn check.


Instead of handling this in various callers, maybe make_typename_type 
should handle the error, like it already does for e.g.


  struct A {  template  static T t; };
  A(unsigned) -> A:: template t;

Incidentally, in the testcase for 88325:


A::A () // { dg-error "partial specialization" }


this error is misleading; this isn't a partial specialization, it's 
redundant template arguments while trying to define the primary 
template.  I wouldn't worry about fixing the compiler now, but let's not 
test for the wrong error.



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

gcc/cp/ChangeLog:

PR c++/99803
* parser.c (cp_parser_simple_type_specifier): Don't call
cp_parser_make_typename_type for is_overloaded_fn.

gcc/testsuite/ChangeLog:

PR c++/99803
* g++.dg/cpp2a/typename19.C: New test.
---
  gcc/cp/parser.c | 2 +-
  gcc/testsuite/g++.dg/cpp2a/typename19.C | 5 +
  2 files changed, 6 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename19.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 3a107206318..3c506d891c9 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -18903,7 +18903,7 @@ cp_parser_simple_type_specifier (cp_parser* parser,
  if (TREE_CODE (type) != TYPE_DECL)
{
  /* ...unless we pretend we have seen 'typename'.  */
- if (typename_p)
+ if (typename_p && !is_overloaded_fn (type))
type = cp_parser_make_typename_type (parser, type,
 token->location);
  else
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename19.C 
b/gcc/testsuite/g++.dg/cpp2a/typename19.C
new file mode 100644
index 000..bd7e5110e00
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/typename19.C
@@ -0,0 +1,5 @@
+// PR c++/99803
+// { dg-do compile { target c++20 } }
+
+struct A { template A(T); };
+auto A(unsigned) -> A::template A; // { dg-error "not name a type" }

base-commit: a87d3f964df31d4fbceb822c6d293e85c117d992





[PATCH] c++: ICE with bogus late return type [PR99803]

2021-04-14 Thread Marek Polacek via Gcc-patches
Here we ICE when compiling this code in C++20, because we're trying to
slam a 'typename' after the ->.  The cp_parser_template_id call just
before the spot I'm changing parsed A::template A as a BASELINK
that contains a constructor, but make_typename_type crashes on that.

My fix is the same as c++/88325, add an is_overloaded_fn check.

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

gcc/cp/ChangeLog:

PR c++/99803
* parser.c (cp_parser_simple_type_specifier): Don't call
cp_parser_make_typename_type for is_overloaded_fn.

gcc/testsuite/ChangeLog:

PR c++/99803
* g++.dg/cpp2a/typename19.C: New test.
---
 gcc/cp/parser.c | 2 +-
 gcc/testsuite/g++.dg/cpp2a/typename19.C | 5 +
 2 files changed, 6 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/typename19.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 3a107206318..3c506d891c9 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -18903,7 +18903,7 @@ cp_parser_simple_type_specifier (cp_parser* parser,
  if (TREE_CODE (type) != TYPE_DECL)
{
  /* ...unless we pretend we have seen 'typename'.  */
- if (typename_p)
+ if (typename_p && !is_overloaded_fn (type))
type = cp_parser_make_typename_type (parser, type,
 token->location);
  else
diff --git a/gcc/testsuite/g++.dg/cpp2a/typename19.C 
b/gcc/testsuite/g++.dg/cpp2a/typename19.C
new file mode 100644
index 000..bd7e5110e00
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/typename19.C
@@ -0,0 +1,5 @@
+// PR c++/99803
+// { dg-do compile { target c++20 } }
+
+struct A { template A(T); };
+auto A(unsigned) -> A::template A; // { dg-error "not name a type" }

base-commit: a87d3f964df31d4fbceb822c6d293e85c117d992
-- 
2.30.2