Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,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)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably keep this as an additional sanity check, however I think in this particular case the error is in the validity check in LookupStdInitializerList, where it is checking the minimum required template parameters, rather than ensuring real correctness.

It should probably be doing Params->size() != 1, and ensuring that there isn't a parameter pack or any default template arguments.

Changing those validity checks will also result in a much more direct error saying std::initializer_list is defined incorrectly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a separate PR for this change: #133822

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) {
Expand Down
18 changes: 18 additions & 0 deletions clang/test/SemaCXX/invalid-std-initializer-list.cpp
Original file line number Diff line number Diff line change
@@ -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}}
}
Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually wrap the test with a namespace of its corresponding GH number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a namespace and a release note.


}