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

Conversation

jijjijj
Copy link
Contributor

@jijjijj jijjijj commented Dec 3, 2024

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.

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

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.
Copy link

github-actions bot commented Dec 3, 2024

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 @ followed by their GitHub username.

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.

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a 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()))
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()

jijjijj and others added 2 commits December 7, 2024 19:17
@jijjijj jijjijj marked this pull request as ready for review December 7, 2024 16:48
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules labels Dec 7, 2024
@jijjijj jijjijj requested a review from ChuanqiXu9 December 7, 2024 16:49
@llvmbot
Copy link
Member

llvmbot commented Dec 7, 2024

@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: None (jijjijj)

Changes

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.

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:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1)
  • (added) clang/test/Modules/initializer-list-recognition-through-export-and-linkage-issue-118218.cpp (+39)
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;
}
Copy link
Member

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
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.

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

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

LGTM with nits

@ChuanqiXu9 ChuanqiXu9 changed the title [clang][modules] Fix std::initializer_list recognition if it's exported out of a module [C++20][modules] Fix std::initializer_list recognition if it's exported out of a module Dec 10, 2024
…rted out of a module

- Add a newline before EOF
- Improve release notes
@jijjijj jijjijj requested a review from ChuanqiXu9 December 11, 2024 11:57
Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

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

LGTM then

@ChuanqiXu9
Copy link
Member

I'll merge this directly since it looks like you don't have commit access.

@ChuanqiXu9 ChuanqiXu9 merged commit 9f1e9f6 into llvm:main Dec 12, 2024
7 checks passed
Copy link

@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!

@ChuanqiXu9
Copy link
Member

/cherry-pick 9f1e9f6

@ChuanqiXu9
Copy link
Member

It looks nice to backport this if we're still in the release circle for 19.x

@llvmbot
Copy link
Member

llvmbot commented Jan 6, 2025

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

@ChuanqiXu9
Copy link
Member

@jijjijj automated backport failed. You can create a PR to release/19.x manually to backport it.

@jijjijj
Copy link
Contributor Author

jijjijj commented Jan 6, 2025

@ChuanqiXu9 here's the PR: #121739

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category release:cherry-pick-failed
Projects
Development

Successfully merging this pull request may close these issues.

Clang: modules and std::initializer_list do not like each other
3 participants