public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/88373, wrong parse error with ~
@ 2018-12-06 16:33 Marek Polacek
  2018-12-06 19:39 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2018-12-06 16:33 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

This patch fixes a bogus parse error with ~ in a template-argument-list.  We have

  S<int, ~value <int>>

and cp_parser_template_argument just tries to parse each argument as a type,
id-expression, etc to see what sticks.  When it sees ~value, it tries to parse
it using cp_parser_class_name (because of the ~), which ends up calling
cp_parser_lookup_name, looking for "value", but finds nothing.  Since it's an
unqualified-id followed by <, we treat "~value<int>" as a template name
function.  It isn't followed by "(args)", so we simulate parse error.  As a
consequence of this error, the parsing of the outermost template-id S fails.

The problem is that when we're looking up the name in cp_parser_class_name,
tag_type is typename_type, which means bindings that do not refer to types
are ignored, so the variable template "value" isn't found and we're toast.

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

2018-12-06  Marek Polacek  <polacek@redhat.com>

	PR c++/88373 - wrong parse error with ~.
	* parser.c (cp_parser_template_name): Check tag_type for
	none_type.

	* g++.dg/cpp2a/fn-template19.C: New test.

diff --git gcc/cp/parser.c gcc/cp/parser.c
index ac19cb4b9bb..2f55855ce9f 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -16579,7 +16579,8 @@ cp_parser_template_name (cp_parser* parser,
       if (!found
 	  && (cxx_dialect > cxx17)
 	  && !scoped_p
-	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
+	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
+	  && tag_type == none_type)
 	{
 	  /* [temp.names] says "A name is also considered to refer to a template
 	     if it is an unqualified-id followed by a < and name lookup finds
diff --git gcc/testsuite/g++.dg/cpp2a/fn-template19.C gcc/testsuite/g++.dg/cpp2a/fn-template19.C
new file mode 100644
index 00000000000..1d6b43bb7ce
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp2a/fn-template19.C
@@ -0,0 +1,11 @@
+// PR c++/88373
+// { dg-do compile }
+// { dg-options "-std=c++2a" }
+
+template <class T>
+constexpr T value = T {};
+
+template <class T, T t>
+struct S {};
+
+using U = S <int, ~value <int>>;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C++ PATCH for c++/88373, wrong parse error with ~
  2018-12-06 16:33 C++ PATCH for c++/88373, wrong parse error with ~ Marek Polacek
@ 2018-12-06 19:39 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2018-12-06 19:39 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 12/6/18 11:33 AM, Marek Polacek wrote:
> This patch fixes a bogus parse error with ~ in a template-argument-list.  We have
> 
>    S<int, ~value <int>>
> 
> and cp_parser_template_argument just tries to parse each argument as a type,
> id-expression, etc to see what sticks.  When it sees ~value, it tries to parse
> it using cp_parser_class_name (because of the ~), which ends up calling
> cp_parser_lookup_name, looking for "value", but finds nothing.  Since it's an
> unqualified-id followed by <, we treat "~value<int>" as a template name
> function.  It isn't followed by "(args)", so we simulate parse error.  As a
> consequence of this error, the parsing of the outermost template-id S fails.
> 
> The problem is that when we're looking up the name in cp_parser_class_name,
> tag_type is typename_type, which means bindings that do not refer to types
> are ignored, so the variable template "value" isn't found and we're toast.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?

OK.

Jason

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-12-06 19:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-06 16:33 C++ PATCH for c++/88373, wrong parse error with ~ Marek Polacek
2018-12-06 19:39 ` Jason Merrill

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).