Skip to content

[C++20][Modules] static data members of template classes should be allowed in header units #98309

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

dmpolukhin
Copy link
Contributor

@dmpolukhin dmpolukhin commented Jul 10, 2024

Summary:
There is no sense to report these cases as an error or add inline explicitly in these cases. If it is not required in normal headers. Similar to #60079.

Test Plan: check-clang

…owed in header units

Summary:
These is no sense to report this cases as an error or add `inline`
explicitly in this cases. If it is not required in normal headers.
Similar to llvm#60079.

Test Plan: check-clang

Reviewers: @kaimfrai, @ChuanqiXu9

Subscribers: @iains, @EugeneZelenko, @dwblaikie, @Arthapz
@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 Jul 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2024

@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: Dmitry Polukhin (dmpolukhin)

Changes

Summary:
These is no sense to report this cases as an error or add inline explicitly in this cases. If it is not required in normal headers. Similar to #60079.

Test Plan: check-clang


Full diff: https://github.com/llvm/llvm-project/pull/98309.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+2-1)
  • (modified) clang/test/CXX/module/module.import/p6.cpp (+10)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b3bfdacb01790..7d810b895f9e8 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13305,7 +13305,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
   if (getLangOpts().CPlusPlusModules && currentModuleIsHeaderUnit() &&
       !VDecl->isInvalidDecl() && VDecl->isThisDeclarationADefinition() &&
       VDecl->getFormalLinkage() == Linkage::External && !VDecl->isInline() &&
-      !VDecl->isTemplated() && !isa<VarTemplateSpecializationDecl>(VDecl)) {
+      !VDecl->isTemplated() && !isa<VarTemplateSpecializationDecl>(VDecl) &&
+      !VDecl->getInstantiatedFromStaticDataMember()) {
     Diag(VDecl->getLocation(), diag::err_extern_def_in_header_unit);
     VDecl->setInvalidDecl();
   }
diff --git a/clang/test/CXX/module/module.import/p6.cpp b/clang/test/CXX/module/module.import/p6.cpp
index 0ed8b5958dffe..cb2d799e5b565 100644
--- a/clang/test/CXX/module/module.import/p6.cpp
+++ b/clang/test/CXX/module/module.import/p6.cpp
@@ -67,3 +67,13 @@ void* tmpl_fn_ok
 inline int foo (int a) {
   return tmpl_OK (a);
 }
+
+template <typename T> struct S2 { static int v; };
+template <typename T> int S2<T>::v = 10;
+
+template <typename T> bool b() {
+    bool b1 = S2<T>::v == 10;
+    return b1 && true;
+}
+
+inline bool B = b<int>();

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

@dmpolukhin dmpolukhin merged commit 00fd188 into llvm:main Jul 11, 2024
11 checks passed
@dmpolukhin dmpolukhin deleted the template-class-static-data-member-in-header-unit branch July 11, 2024 07:49
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
…lowed in header units (llvm#98309)

Summary:
There is no sense to report these cases as an error or add `inline`
explicitly in these cases, if it is not required in normal headers.
Similar to llvm#60079.

Test Plan: check-clang
huangqinjin added a commit to huangqinjin/cxxmodules that referenced this pull request Nov 10, 2024
1. __cpp_lib_modules now is defined.
llvm/llvm-project#90091

2. clang-cl supports -x option for all languages.
llvm/llvm-project#89772

3. Header units allow non-inline external static members definitions.
llvm/llvm-project#98309

4. winsysroot alias to the GNU driver: -Xmicrosoft-windows-sys-root.
llvm/llvm-project#94731

5. clang-cl gained options for compiling C++ modules.
llvm/llvm-project#98761

6. fix unused argument warning for /std:c++20 when compiling c++ modules.
llvm/llvm-project#99300
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants