Go to file
Marek Polacek 517f5356bb c++: DR1710, template keyword in a typename-specifier [PR94057]
Consider

  template <typename T> class A {
    template <typename U> class B {
      void fn(typename A<T>::B<U>);
    };
  };

which is rejected with
error: 'typename A<T>::B' names 'template<class T> template<class U> class A<T>::B', which is not a type
whereas clang/icc/msvc accept it.

"typename A<T>::B<U>" is a typename-specifier.  Sadly, our comments
don't mention it anywhere, because the typename-specifier wasn't in C++11;
it was only added to the language in N1376.  Instead, we handle it as
an elaborated-type-specifier (not a problem thus far).   So we get to
cp_parser_nested_name_specifier_opt which has a loop that breaks if we
don't see a < or ::, but that means we can -- tentatively -- parse even
B<U> which is not a nested-name-specifier (it doesn't end with a ::).

I think this should compile because [temp.names]/4 says: "In a qualified-id
used as the name in a typename-specifier, elaborated-type-specifier,
using-declaration, or class-or-decltype, an optional keyword template
appearing at the top level is ignored.", added in DR 1710.  Also see
DR 1812.

This issue on its own is not a significant problem or a regression.
However, in C++20, the typename here becomes optional, and so this test
is rejected in C++20, but accepted in C++17:

  template <typename T> class A {
    template <typename U> class B {
      void fn(A<T>::B<U>);
    };
  };

Here we morph A<T>::B<U> into a typename-specifier, but that happens
in cp_parser_simple_type_specifier and we never handle it as above.
To fake the template keyword I'm afraid we need to use cp_parser_template_id
with template_keyword_p=true as in the patch below.  The tricky thing
is to avoid breaking concepts.

To handle DR 1710, I made cp_parser_nested_name_specifier_opt assume that
when we're naming a type, the template keyword is present, too.  That
revealed a bug: DR 1710 also says that the template keyword can be followed
by an alias template, but we weren't prepared to handle that.  alias-decl?.C
exercise this.

gcc/cp:

	DR 1710
	PR c++/94057 - template keyword in a typename-specifier.
	* parser.c (check_template_keyword_in_nested_name_spec): New.
	(cp_parser_nested_name_specifier_opt): Implement DR1710, optional
	'template'.  Call check_template_keyword_in_nested_name_spec.
	(cp_parser_simple_type_specifier): Assume that a <
	following a qualified-id in a typename-specifier begins
	a template argument list.

gcc/testsuite:

	DR 1710
	PR c++/94057 - template keyword in a typename-specifier.
	* g++.dg/cpp1y/alias-decl1.C: New test.
	* g++.dg/cpp1y/alias-decl2.C: New test.
	* g++.dg/cpp1y/alias-decl3.C: New test.
	* g++.dg/parse/missing-template1.C: Update dg-error.
	* g++.dg/parse/template3.C: Likewise.
	* g++.dg/template/error4.C: Likewise.
	* g++.dg/template/meminit2.C: Likewise.
	* g++.dg/template/dependent-name5.C: Likewise.
	* g++.dg/template/dependent-name7.C: New test.
	* g++.dg/template/dependent-name8.C: New test.
	* g++.dg/template/dependent-name9.C: New test.
	* g++.dg/template/dependent-name10.C: New test.
	* g++.dg/template/dependent-name11.C: New test.
	* g++.dg/template/dependent-name12.C: New test.
	* g++.dg/template/dependent-name13.C: New test.
	* g++.dg/template/dr1794.C: New test.
	* g++.dg/template/dr314.C: New test.
	* g++.dg/template/dr1710.C: New test.
	* g++.dg/template/dr1710-2.C: New test.
	* g++.old-deja/g++.pt/crash38.C: Update dg-error.
2020-03-26 18:46:08 -04:00
config Use a non-empty test program to test ability to link. 2020-02-12 13:22:07 -08:00
contrib
fixincludes
gcc c++: DR1710, template keyword in a typename-specifier [PR94057] 2020-03-26 18:46:08 -04:00
gnattools
gotools
include
INSTALL
intl
libada
libatomic
libbacktrace
libcc1
libcpp
libdecnumber
libffi
libgcc arm: unified syntax for libgcc when built with -Os [PR94220] 2020-03-26 10:59:34 +00:00
libgfortran Use au->lock exclusively for locking in async I/O. 2020-02-18 19:45:25 +01:00
libgo
libgomp
libhsail-rt
libiberty
libitm
libobjc Use a non-empty test program to test ability to link. 2020-02-12 13:22:07 -08:00
liboffloadmic Add `--with-toolexeclibdir=' configuration option 2020-01-24 11:24:25 +00:00
libphobos
libquadmath
libsanitizer
libssp
libstdc++-v3 libstdc++: Add some C++20 additions to <chrono> 2020-03-26 14:00:12 +00:00
libvtv
lto-plugin
maintainer-scripts
zlib
.dir-locals.el * .dir-locals.el: Add. 2013-10-16 18:25:31 +00:00
.gitattributes
.gitignore
ABOUT-NLS
ar-lib
ChangeLog
ChangeLog.jit
ChangeLog.tree-ssa
compile Update from upstream Automake files. 2014-11-16 14:07:13 +00:00
config-ml.in
config.guess
config.rpath
config.sub
configure configure - build libgomp by default for amdgcn 2020-03-09 17:00:39 +01:00
configure.ac
COPYING
COPYING3
COPYING3.LIB
COPYING.LIB
COPYING.RUNTIME
depcomp
install-sh
libtool-ldflags
libtool.m4
lt~obsolete.m4
ltgcc.m4 libtool.m4: Update to libtool 2.2.6. 2008-09-26 16:21:02 +00:00
ltmain.sh libtool.m4: Sort output of 'find' to enable deterministic builds. 2018-07-05 13:13:45 -06:00
ltoptions.m4
ltsugar.m4 libtool.m4: Update to libtool 2.2.6. 2008-09-26 16:21:02 +00:00
ltversion.m4
MAINTAINERS Update myself to MAINTAINERS 2020-03-11 23:32:40 -04:00
Makefile.def
Makefile.in
Makefile.tpl
missing
mkdep
mkinstalldirs
move-if-change Update move-if-change from gnulib 2014-11-16 16:12:44 +00:00
multilib.am
README
symlink-tree
test-driver
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.