Skip to content

[clang] fix redecl chain assumption when checking linkage consistency #153996

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 1 commit into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1604,17 +1604,20 @@ LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D,
// We have just computed the linkage for this decl. By induction we know
// that all other computed linkages match, check that the one we just
// computed also does.
NamedDecl *Old = nullptr;
for (auto *I : D->redecls()) {
auto *T = cast<NamedDecl>(I);
if (T == D)
// We can't assume the redecl chain is well formed at this point,
// so keep track of already visited declarations.
for (llvm::SmallPtrSet<const Decl *, 4> AlreadyVisited{D}; /**/; /**/) {
D = cast<NamedDecl>(const_cast<NamedDecl *>(D)->getNextRedeclarationImpl());
if (!AlreadyVisited.insert(D).second)
break;
if (D->isInvalidDecl())
continue;
if (!T->isInvalidDecl() && T->hasCachedLinkage()) {
Old = T;
if (auto OldLinkage = D->getCachedLinkage();
OldLinkage != Linkage::Invalid) {
assert(LV.getLinkage() == OldLinkage);
break;
}
}
assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
#endif

return LV;
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Modules/GH153933.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/B.cppm -emit-module-interface -o %t/B.pcm
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -fprebuilt-module-path=%t %t/C.cpp

//--- A.hpp
template<class> struct A {};
template<class T> struct B {
virtual A<T> v() { return {}; }
};
B<void> x;

//--- B.cppm
module;
#include "A.hpp"
export module B;
using ::x;

//--- C.cpp
#include "A.hpp"
import B;
Loading