[clang] Fix crash on invalid std::initializer_list<T> template-id (#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 #132256
This commit is contained in:
offsetof 2025-04-01 10:44:10 +00:00 committed by GitHub
parent 518102f259
commit b0a7906517
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

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

View File

@ -12182,10 +12182,14 @@ QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) {
Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element),
Context.getTrivialTypeSourceInfo(Element,
Loc)));
QualType T = CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args);
if (T.isNull())
return QualType();
return Context.getElaboratedType(
ElaboratedTypeKeyword::None,
NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()),
CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args));
NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()), T);
}
bool Sema::isInitListConstructor(const FunctionDecl *Ctor) {

View File

@ -0,0 +1,18 @@
// RUN: %clang_cc1 %s -verify -std=c++20
namespace std {
template<class T, class = T::x> // expected-error 2 {{type 'int' cannot be used prior to '::' because it has no members}}
class initializer_list;
}
namespace gh132256 {
auto x = {1}; // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
void f() {
for(int x : {1, 2}); // expected-note {{in instantiation of default argument for 'initializer_list<int>' required here}}
}
}