Skip to content

Commit 06bd74b

Browse files
authored
Fix implementation of [temp.param]p14's first sentence. (#83487)
The first sentence says: If a template-parameter of a class template, variable template, or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameter pack. However, we were only testing for "not a function function template", and referring to an older version of the standard. As far as I can tell, CWG2032 added the variable-template, and the alias-template pre-dates the standard on github. This patch started as a bug fix for #83461 , but ended up fixing a number of similar cases, so those are validated as well.
1 parent a038f97 commit 06bd74b

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ Bug Fixes to C++ Support
301301
- Clang now properly reports supported C++11 attributes when using
302302
``__has_cpp_attribute`` and parses attributes with arguments in C++03
303303
(`#82995 <https://github.com/llvm/llvm-project/issues/82995>`_)
304+
- Clang now properly diagnoses missing 'default' template arguments on a variety
305+
of templates. Previously we were diagnosing on any non-function template
306+
instead of only on class, alias, and variable templates, as last updated by
307+
CWG2032.
308+
Fixes (`#83461 <https://github.com/llvm/llvm-project/issues/83461>`_)
309+
304310

305311
Bug Fixes to AST Handling
306312
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,12 +3141,14 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
31413141
diag::note_template_param_prev_default_arg_in_other_module)
31423142
<< PrevModuleName;
31433143
Invalid = true;
3144-
} else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
3145-
// C++ [temp.param]p11:
3146-
// If a template-parameter of a class template has a default
3147-
// template-argument, each subsequent template-parameter shall either
3148-
// have a default template-argument supplied or be a template parameter
3149-
// pack.
3144+
} else if (MissingDefaultArg &&
3145+
(TPC == TPC_ClassTemplate || TPC == TPC_FriendClassTemplate ||
3146+
TPC == TPC_VarTemplate || TPC == TPC_TypeAliasTemplate)) {
3147+
// C++ 23[temp.param]p14:
3148+
// If a template-parameter of a class template, variable template, or
3149+
// alias template has a default template argument, each subsequent
3150+
// template-parameter shall either have a default template argument
3151+
// supplied or be a template parameter pack.
31503152
Diag((*NewParam)->getLocation(),
31513153
diag::err_template_param_default_arg_missing);
31523154
Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);

clang/test/SemaCXX/GH83461.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
3+
struct S {
4+
template<typename Ty = int>
5+
friend void foo(auto){}
6+
7+
template<typename Ty = int, typename Tz>
8+
friend void foo2(){}
9+
};
10+
11+
template<typename T>
12+
struct TemplS {
13+
template<typename Ty = int>
14+
friend void foo3(auto){}
15+
16+
template<typename Ty = int, typename Tz>
17+
friend void foo4(){}
18+
};
19+
20+
void Inst() {
21+
TemplS<int>();
22+
}
23+
// expected-error@+2{{template parameter missing a default argument}}
24+
// expected-note@+1{{previous default template argument defined here}}
25+
template<typename T = int, typename U>
26+
struct ClassTempl{};
27+
28+
struct HasFriendClassTempl {
29+
// expected-error@+1{{default template argument not permitted on a friend template}}
30+
template<typename T = int, typename U>
31+
friend struct Friend;
32+
33+
// expected-error@+3{{cannot define a type in a friend declaration}}
34+
// expected-error@+1{{default template argument not permitted on a friend template}}
35+
template<typename T = int, typename U>
36+
friend struct Friend2{};
37+
};
38+
39+
template<typename Ty>
40+
struct HasFriendClassTempl2 {
41+
// expected-error@+3{{template parameter missing a default argument}}
42+
// expected-note@+2{{previous default template argument defined here}}
43+
// expected-note@#INST2{{in instantiation of template class}}
44+
template<typename T = int, typename U>
45+
friend struct Friend;
46+
};
47+
48+
void Inst2() {
49+
HasFriendClassTempl2<int>(); // #INST2
50+
}
51+
52+
// expected-error@+2{{template parameter missing a default argument}}
53+
// expected-note@+1{{previous default template argument defined here}}
54+
template<typename T = int, typename U>
55+
static constexpr U VarTempl;
56+
57+
// expected-error@+2{{template parameter missing a default argument}}
58+
// expected-note@+1{{previous default template argument defined here}}
59+
template<typename T = int, typename U>
60+
using TypeAlias = U;

0 commit comments

Comments
 (0)