Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 19 additions & 4 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6926,11 +6926,26 @@ void Sema::AddOverloadCandidate(
/// have linkage. So that all entities of the same should share one
/// linkage. But in clang, different entities of the same could have
/// different linkage.
NamedDecl *ND = Function;
if (auto *SpecInfo = Function->getTemplateSpecializationInfo())
const NamedDecl *ND = Function;
bool IsImplicitlyInstantiated = false;
if (auto *SpecInfo = Function->getTemplateSpecializationInfo()) {
ND = SpecInfo->getTemplate();

if (ND->getFormalLinkage() == Linkage::Internal) {
IsImplicitlyInstantiated = SpecInfo->getTemplateSpecializationKind() ==
TSK_ImplicitInstantiation;
}

/// Don't remove inline functions with internal linkage from the overload
/// set if they are declared in a GMF.
/// The global module is meant to be a transition mechanism for C and C++
/// headers.
/// Inline functions with internal linkage are a common pattern in headers
/// to avoid ODR issues.
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to make the comments to be more explicitly to mention the behavior is not strictly conforming the standard. Maybe we need to add a NOTE. BTW, maybe this worth a paper to WG21. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea! One direction could be to create an additional exception to basic.link#17 for exposure of inline TU-local entities declared in a GMF, so that the program would be no longer ill-formed in that case, right? depr.local's note would have to be changed to reflect this.

I will add to the comment that the proposed behavior violates basic.link#17.

Copy link
Member

Choose a reason for hiding this comment

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

I feel the better wording will add a clause to https://eel.is/c++draft/basic.link#14. There are already some exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay! I have now updated the comment to include the fact that this fix/workaround violates [basic.link]p17, strictly speaking. I've also merged the main branch. On my machine, the (new) tests in ./tools/clang/test/Modules still pass. Does this work for you? Thanks again for the review!

const bool IsInlineFunctionInGMF =
Function->getOwningModule() &&
Function->getOwningModule()->isGlobalModule() &&
(IsImplicitlyInstantiated || Function->isInlined());

if (ND->getFormalLinkage() == Linkage::Internal && !IsInlineFunctionInGMF) {
Candidate.Viable = false;
Candidate.FailureKind = ovl_fail_module_mismatched;
return;
Expand Down
37 changes: 37 additions & 0 deletions clang/test/Modules/expose-static-inline-from-gmf-1.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm \
// RUN: -DTEST_INLINE
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify \
// RUN: -DTEST_INLINE
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify

//--- a.h
#ifdef TEST_INLINE
#define INLINE inline
#else
#define INLINE
#endif
static INLINE void func(long) {}
template <typename T = long> void a() { func(T{}); }

//--- a.cppm
module;
#include "a.h"
export module a;
export using ::a;

//--- test.cc
import a;
auto m = (a(), 0);

#ifdef TEST_INLINE
// expected-no-diagnostics
#else
// [email protected]:7 {{no matching function for call to 'func'}}
// [email protected]:2 {{in instantiation of function template specialization 'a<long>' requested here}}
#endif
22 changes: 22 additions & 0 deletions clang/test/Modules/expose-static-inline-from-gmf-2.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify

//--- a.h
template <typename G> static inline void func() {}
template <typename T = long> void a() { func<T>(); }

//--- a.cppm
module;
#include "a.h"
export module a;
export using ::a;

//--- test.cc
import a;
auto m = (a(), 0);

// expected-no-diagnostics
24 changes: 24 additions & 0 deletions clang/test/Modules/expose-static-inline-from-gmf-3.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify

//--- a.h
namespace ns {
template <typename G> static void func() {}
template <typename T = long> void a() { func<T>(); }
}

//--- a.cppm
module;
#include "a.h"
export module a;
export using ns::a;

//--- test.cc
import a;
auto m = (a(), 0);

// expected-no-diagnostics
40 changes: 40 additions & 0 deletions clang/test/Modules/expose-static-inline-from-gmf-4.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm \
// RUN: -DTEST_INLINE
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify \
// RUN: -DTEST_INLINE
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify

//--- a.h
#ifdef TEST_INLINE
#define INLINE inline
#else
#define INLINE
#endif
namespace ns {
template <typename G> static void func() {}
template <> INLINE void func<long>() {}
template <typename T = long> void a() { func<T>(); }
}

//--- a.cppm
module;
#include "a.h"
export module a;
export using ns::a;

//--- test.cc
import a;
auto m = (a(), 0);

#ifdef TEST_INLINE
// expected-no-diagnostics
#else
// [email protected]:9 {{no matching function for call to 'func'}}
// [email protected]:2 {{in instantiation of function template specialization 'ns::a<long>' requested here}}
#endif
26 changes: 26 additions & 0 deletions clang/test/Modules/expose-static-inline-from-gmf-5.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang -std=c++20 %t/a.cppm --precompile -o %t/a.pcm
// RUN: %clang -std=c++20 %t/test.cc -fprebuilt-module-path=%t -fsyntax-only -Xclang -verify

//--- a.h
namespace ns {
namespace {
template <typename G> void func() {}
}
template <typename T = long> void a() { func<T>(); }
}

//--- a.cppm
module;
#include "a.h"
export module a;
export using ns::a;

//--- test.cc
import a;
auto m = (a(), 0);

// expected-no-diagnostics