From: Marek Polacek Date: Sat, 25 May 2019 14:46:15 +0000 (+0000) Subject: PR c++/90572 - wrong disambiguation in friend declaration. X-Git-Tag: releases/gcc-9.2.0~319 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d39d5a63a32383713f55f7a64483067b4237f52;p=thirdparty%2Fgcc.git PR c++/90572 - wrong disambiguation in friend declaration. * parser.c (cp_parser_constructor_declarator_p): Don't allow missing typename for friend declarations. * g++.dg/cpp2a/typename16.C: New test. * g++.dg/parse/friend13.C: New test. From-SVN: r271620 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 7548b65589c0..40c920280dc2 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2019-05-25 Marek Polacek + + Backported from mainline + 2019-05-25 Marek Polacek + + PR c++/90572 - wrong disambiguation in friend declaration. + * parser.c (cp_parser_constructor_declarator_p): Don't allow missing + typename for friend declarations. + 2019-05-20 Jonathan Wakely Backported from mainline diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 3d908916ae3f..a7218e752b16 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -27629,9 +27629,19 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, parser->num_template_parameter_lists = 0; /* Look for the type-specifier. It's not optional, but its typename - might be. */ + might be. Unless this is a friend declaration; we don't want to + treat + + friend S (T::fn)(int); + + as a constructor, but with P0634, we might assume a type when + looking for the type-specifier. It is actually a function named + `T::fn' that takes one parameter (of type `int') and returns a + value of type `S'. Constructors can be friends, but they must + use a qualified name. */ cp_parser_type_specifier (parser, - (flags & ~CP_PARSER_FLAGS_OPTIONAL), + (friend_p ? CP_PARSER_FLAGS_NONE + : (flags & ~CP_PARSER_FLAGS_OPTIONAL)), /*decl_specs=*/NULL, /*is_declarator=*/true, /*declares_class_or_enum=*/NULL, diff --git a/gcc/testsuite/g++.dg/cpp2a/typename16.C b/gcc/testsuite/g++.dg/cpp2a/typename16.C new file mode 100644 index 000000000000..7f4242a1dbae --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/typename16.C @@ -0,0 +1,11 @@ +// PR c++/90572 +// { dg-do compile { target c++2a } } + +struct X { X(int); }; + +template struct S { + friend X::X(T::t); +}; + +struct W { using t = int; }; +S s; diff --git a/gcc/testsuite/g++.dg/parse/friend13.C b/gcc/testsuite/g++.dg/parse/friend13.C new file mode 100644 index 000000000000..d716247a5da8 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/friend13.C @@ -0,0 +1,16 @@ +// PR c++/90572 + +template struct C { + friend C(T::fn)(); + friend C(T::fn)(int); + friend C(T::fn)(int, int); +}; + +struct X { }; + +template +struct B { + friend X(T::fn)(); + friend X(T::fn)(int); + friend X(T::fn)(int, int); +};