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  <pola...@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>>;

Reply via email to