Skip to content

[C++20][modules] Fix std::initializer_list recognition if it's exported out of a module #118537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ Bug Fixes to C++ Support
- Fixed an incorrect lambda scope of generic lambdas that caused Clang to crash when computing potential lambda
captures at the end of a full expression. (#GH115931)
- Clang no longer rejects deleting a pointer of incomplete enumeration type. (#GH99278)
- Fixed recognition of ``std::initializer_list`` when it's surrounded with ``extern "C++"`` and exported
Copy link
Member

Choose a reason for hiding this comment

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

I guess it is worth mentioning the pattern comes from MSVC's std module implementation.

out of a module (which is the case e.g. in MSVC's implementation of ``std`` module). (#GH118218)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11927,7 +11927,7 @@ bool Sema::isStdInitializerList(QualType Ty, QualType *Element) {
if (TemplateClass->getIdentifier() !=
&PP.getIdentifierTable().get("initializer_list") ||
!getStdNamespace()->InEnclosingNamespaceSetOf(
TemplateClass->getDeclContext()))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can fix it by replacing getDeclContext() with getNonTransparentContext()

TemplateClass->getNonTransparentDeclContext()))
return false;
// This is a template called std::initializer_list, but is it the right
// template?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/std.cppm -emit-module-interface -o %t/std.pcm
// RUN: %clang_cc1 -std=c++20 %t/mod.cppm -fprebuilt-module-path=%t -emit-module-interface -o %t/mod.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -verify %t/main.cpp

//--- std.cppm
export module std;

extern "C++" {
namespace std {
export template <class E>
class initializer_list {
const E* _1;
const E* _2;
};
}
}

//--- mod.cppm
export module mod;

import std;

export struct A {
void func(std::initializer_list<int>) {}
};

//--- main.cpp
// expected-no-diagnostics
import std;
import mod;

int main() {
A{}.func({1,1});
return 0;
}
Loading