Skip to content

Commit b0a7906

Browse files
authored
[clang] Fix crash on invalid std::initializer_list<T> template-id (llvm#132284)
In `Sema::BuildStdInitializerList`, check that the synthesized template-id `std::initializer_list<T>` is valid (which might not be the case if the template has associated constraints or subsequent parameters with default arguments) before forming the type. Fixes llvm#132256
1 parent 518102f commit b0a7906

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Bug Fixes to C++ Support
371371
- Fixed a Clang regression in C++20 mode where unresolved dependent call expressions were created inside non-dependent contexts (#GH122892)
372372
- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
373373
and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
374+
- Fixed a crash caused by invalid declarations of ``std::initializer_list``. (#GH132256)
374375
- Clang no longer crashes when establishing subsumption between some constraint expressions. (#GH122581)
375376
- Clang now issues an error when placement new is used to modify a const-qualified variable
376377
in a ``constexpr`` function. (#GH131432)

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12182,10 +12182,14 @@ QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) {
1218212182
Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element),
1218312183
Context.getTrivialTypeSourceInfo(Element,
1218412184
Loc)));
12185+
12186+
QualType T = CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args);
12187+
if (T.isNull())
12188+
return QualType();
12189+
1218512190
return Context.getElaboratedType(
1218612191
ElaboratedTypeKeyword::None,
12187-
NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()),
12188-
CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args));
12192+
NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()), T);
1218912193
}
1219012194

1219112195
bool Sema::isInitListConstructor(const FunctionDecl *Ctor) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 %s -verify -std=c++20
2+
3+
namespace std {
4+
5+
template<class T, class = T::x> // expected-error 2 {{type 'int' cannot be used prior to '::' because it has no members}}
6+
class initializer_list;
7+
8+
}
9+
10+
namespace gh132256 {
11+
12+
auto x = {1}; // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
13+
14+
void f() {
15+
for(int x : {1, 2}); // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
16+
}
17+
18+
}

0 commit comments

Comments
 (0)