[clang] [clang] Implement instantiation context note for checking template parameters (PR #126088)

Matheus Izvekov via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 20 09:04:26 PST 2025


================
@@ -5294,10 +5294,14 @@ def err_template_missing_args : Error<
   "%select{class template|function template|variable template|alias template|"
   "template template parameter|concept|template}0 %1 requires template "
   "arguments">;
-def err_template_arg_list_different_arity : Error<
-  "%select{too few|too many}0 template arguments for "
+def err_template_param_missing_arg : Error<
+  "missing template argument for template parameter">;
+def err_template_template_param_missing_param : Error<
+  "missing template parameter to bind to template template parameter">;
----------------
mizvekov wrote:

We can point to the template template parameter and argument source locations, so we shouldn't try to spell them in the diagnostic.

The 'parameter list arity mismatch' is an exact match oriented kind of diagnostic, which becomes confusing with P0522, where you can default the parameter to make this work.

Here we can be more helpful and say how the argument is incompatible with the template template parameter: we will point out in a later note which template parameter doesn't have a corresponding parameter in the template template parameter.

I think the ideal here is for the error to be formulated in such a way as to mention the template parameter which we will note later, so that it doesn't look like the note came out of the blue.

For a test case like:
```C++
template <class T1, class T2> struct A;
template <template <class T3> class TT> struct B;
template struct B<A>;
```

I made another change to clarify this error. Here is how this prints now with this patch:
```
test.cc:2:29: error: no template parameter in this template template parameter corresponds to non-defaulted template parameter of argument template
    2 | template <template <class T3> class TT> struct B;
      |                             ^
test.cc:1:27: note: template parameter is declared here
    1 | template <class T1, class T2> struct A;
      |                     ~~~~~~^~
test.cc:3:19: note: template template argument is incompatible with its corresponding template template parameter
    3 | template struct B<A>;
      |                   ^
test.cc:2:37: note: template parameter is declared here
    2 | template <template <class T3> class TT> struct B;
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~^~
1 error generated.
```

https://github.com/llvm/llvm-project/pull/126088


More information about the cfe-commits mailing list