[Clang] [Parser] Improve diagnostic for friend concept (#105121)

Diagnose this early after parsing declaration specifiers; this allows us
to issue a better diagnostic. This also checks for `concept friend` and
concept declarations w/o a template-head because it’s easiest to do that
at the same time.

Fixes #45182.
This commit is contained in:
Sirraide 2024-08-22 23:33:40 +02:00 committed by GitHub
parent d010ec6af8
commit 8b5f606612
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 0 deletions

View File

@ -241,6 +241,8 @@ Improvements to Clang's diagnostics
- Don't emit duplicated dangling diagnostics. (#GH93386).
- Improved diagnostic when trying to befriend a concept. (#GH45182).
Improvements to Clang's time-trace
----------------------------------

View File

@ -974,6 +974,9 @@ def warn_cxx23_variadic_friends : Warning<
"variadic 'friend' declarations are incompatible with C++ standards before C++2c">,
DefaultIgnore, InGroup<CXXPre26Compat>;
def err_friend_concept : Error<
"friend declaration cannot be a concept">;
// C++11 default member initialization
def ext_nonstatic_member_init : ExtWarn<
"default member initializer for non-static data member is a C++11 "

View File

@ -3139,6 +3139,19 @@ Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclaration(
return Actions.BuildDeclaratorGroup(Decls);
}
// Befriending a concept is invalid and would already fail if
// we did nothing here, but this allows us to issue a more
// helpful diagnostic.
if (Tok.is(tok::kw_concept)) {
Diag(Tok.getLocation(),
DS.isFriendSpecified() || NextToken().is(tok::kw_friend)
? diag::err_friend_concept
: diag::
err_concept_decls_may_only_appear_in_global_namespace_scope);
SkipUntil(tok::semi, tok::r_brace, StopBeforeMatch);
return nullptr;
}
ParsingDeclarator DeclaratorInfo(*this, DS, DeclAttrs,
DeclaratorContext::Member);
if (TemplateInfo.TemplateParams)

View File

@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
template<class>
concept fooable = true;
struct S {
template<class> friend concept x = requires { requires true; }; // expected-error {{friend declaration cannot be a concept}}
template<class> friend concept fooable; // expected-error {{friend declaration cannot be a concept}}
template<class> concept friend fooable; // expected-error {{expected unqualified-id}}
friend concept fooable; // expected-error {{friend declaration cannot be a concept}}
concept friend fooable; // expected-error {{friend declaration cannot be a concept}}
concept fooable; // expected-error {{concept declarations may only appear in global or namespace scope}}
};