Skip to content

Commit

Permalink
[C++20][Modules] static data members of template classes should be al…
Browse files Browse the repository at this point in the history
…lowed in header units (#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 #60079.

Test Plan: check-clang
  • Loading branch information
dmpolukhin authored Jul 11, 2024
1 parent a18f45f commit 00fd188
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13313,7 +13313,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();
}
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CXX/module/module.import/p6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();

0 comments on commit 00fd188

Please sign in to comment.