llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.91k stars 11.52k forks source link

Clang produces unhelpful diagnostics for a concept declared in a function head #61740

Open cjdb opened 1 year ago

cjdb commented 1 year ago

Given void f(concept int);, Clang will emit the following:

error: variable has incomplete type 'void'
void f(concept int);
     ^
error: expected expression
void f(concept int);
       ^

Neither of these diagnostics describe the two problems with the source. Better diagnostics would look like:

error: concepts can only be declared a global entity, but it's declared as a function parameter here
void f(concept int);
       ^
error: expected unqualified-id
void f(concept int);
               ^

I'd personally prefer for the second one to be expected an unqualified identifier, got keyword 'int', but that's beyond the scope of this commit due to how much it impacts.

11happy commented 7 months ago

Hello @cjdb, I have written a local fix but not sure if this is alsong the expected lines of work, here the diagnostic can be changed accordingly I have added the following code to ExprResult Parser::ParseCastExpression in the clang/lib/Parse/ParseExpr.cpp

case tok::kw_concept:{
    if(getLangOpts().CPlusPlus20 || getLangOpts().C23)
      Diag(Tok.getLocation(),diag::err_concept_decls_may_only_appear_in_global_namespace_scope);
  }

working :

/home/happy/c++/n1.cpp:9:9: error: concept declarations may only appear in global or namespace scope
    9 |  void f(concept int);
      |         ^