-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
If the std::initializer_list is exported out of module, its DeclContext is not a namespace as `Sema::isStdInitializerList` expects, but an `Decl::Kind::Export` and only its parent is a namespace. So this commit makes `Sema::isStdInitializerList` account for that.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overall idea looks good. But the implementation may be improved. I didn't look at the implementation of std module in MSVC. But I am guess it is because the clang compiler failed to skip the transparent export
or language linkage
context.
If yes, please attach a test and the release notes.
@@ -11919,8 +11919,12 @@ bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { | |||
CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); | |||
if (TemplateClass->getIdentifier() != | |||
&PP.getIdentifierTable().get("initializer_list") || | |||
!getStdNamespace()->InEnclosingNamespaceSetOf( | |||
TemplateClass->getDeclContext())) |
There was a problem hiding this comment.
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()
- Improve implementation - Add a regression test - Add release notes
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: None (jijjijj) ChangesIf the std::initializer_list is exported out of module, its I'm really new to clang so I'm not 100% sure that was the issue, it seems so and it fixes compilation. Also I probably need to add tests but I'd like someone to approve the idea first. Fixes #118218 Full diff: https://github.com/llvm/llvm-project/pull/118537.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 59e3a6609123d2..2e25f487bfb528 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -796,6 +796,8 @@ Bug Fixes to C++ Support
- Fixed an assertion failure caused by mangled names with invalid identifiers. (#GH112205)
- 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)
+- Fixed recognition of ``std::initializer_list`` when it's surrounded with ``extern "C++"`` and exported
+ out of a module. (#GH118218)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f7a0b3c059ec91..d5229143709c03 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11927,7 +11927,7 @@ bool Sema::isStdInitializerList(QualType Ty, QualType *Element) {
if (TemplateClass->getIdentifier() !=
&PP.getIdentifierTable().get("initializer_list") ||
!getStdNamespace()->InEnclosingNamespaceSetOf(
- TemplateClass->getDeclContext()))
+ TemplateClass->getNonTransparentDeclContext()))
return false;
// This is a template called std::initializer_list, but is it the right
// template?
diff --git a/clang/test/Modules/initializer-list-recognition-through-export-and-linkage-issue-118218.cpp b/clang/test/Modules/initializer-list-recognition-through-export-and-linkage-issue-118218.cpp
new file mode 100644
index 00000000000000..70ad8951e63356
--- /dev/null
+++ b/clang/test/Modules/initializer-list-recognition-through-export-and-linkage-issue-118218.cpp
@@ -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;
+}
\ No newline at end of file
|
int main() { | ||
A{}.func({1,1}); | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: new line
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with nits
…rted out of a module - Add a newline before EOF - Improve release notes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM then
I'll merge this directly since it looks like you don't have commit access. |
@jijjijj Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
/cherry-pick 9f1e9f6 |
It looks nice to backport this if we're still in the release circle for 19.x |
Failed to cherry-pick: 9f1e9f6 https://github.com/llvm/llvm-project/actions/runs/12625144536 Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request |
@jijjijj automated backport failed. You can create a PR to release/19.x manually to backport it. |
@ChuanqiXu9 here's the PR: #121739 |
If the std::initializer_list is exported out of module, its
DeclContext
is not a namespace asSema::isStdInitializerList
expects, but anDecl::Kind::Export
and only its parent is a namespace. So this commit makesSema::isStdInitializerList
account for that.I'm really new to clang so I'm not 100% sure that was the issue, it seems so and it fixes compilation. Also I probably need to add tests but I'd like someone to approve the idea first.
Fixes #118218