Skip to content

Commit 0d363c3

Browse files
ChuanqiXu9tstellar
authored andcommitted
[C++20] [Modules] Don't diagnose duplicated friend declarations between modules incorrectly
Close #125521 We shouldn't use the ownership information for friend declarations to do anything. (cherry picked from commit c5a9a72)
1 parent 820c8c7 commit 0d363c3

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,8 @@ void ASTContext::PrintStats() const {
10551055
void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M,
10561056
bool NotifyListeners) {
10571057
if (NotifyListeners)
1058-
if (auto *Listener = getASTMutationListener())
1058+
if (auto *Listener = getASTMutationListener();
1059+
Listener && !ND->isUnconditionallyVisible())
10591060
Listener->RedefinedHiddenDefinition(ND, M);
10601061

10611062
MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M);

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,6 +3746,11 @@ void ASTDeclReader::checkMultipleDefinitionInNamedModules(ASTReader &Reader,
37463746
Func && Func->getTemplateSpecializationInfo())
37473747
return;
37483748

3749+
// The module ownership of in-class friend declaration is not straightforward.
3750+
// Avoid diagnosing such cases.
3751+
if (D->getFriendObjectKind() || Previous->getFriendObjectKind())
3752+
return;
3753+
37493754
Module *M = Previous->getOwningModule();
37503755
if (!M)
37513756
return;

clang/test/Modules/pr125521.cppm

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/mod2.cppm -emit-module-interface -o %t/mod2.pcm
6+
// RUN: %clang_cc1 -std=c++20 %t/mod1.cppm -emit-module-interface -o %t/mod1.pcm \
7+
// RUN: -fmodule-file=Mod2=%t/mod2.pcm
8+
// RUN: %clang_cc1 -std=c++20 %t/test.cc -fmodule-file=Mod2=%t/mod2.pcm -fmodule-file=Mod=%t/mod1.pcm \
9+
// RUN: -fsyntax-only -verify
10+
11+
// RUN: %clang_cc1 -std=c++20 %t/mod2.cppm -emit-module-interface -o %t/mod2.pcm
12+
// RUN: %clang_cc1 -std=c++20 %t/mod1.cppm -emit-module-interface -o %t/mod1.pcm \
13+
// RUN: -fmodule-file=Mod2=%t/mod2.pcm
14+
// RUN: %clang_cc1 -std=c++20 %t/mod1.pcm -fmodule-file=Mod2=%t/mod2.pcm -emit-llvm -o - \
15+
// RUN: | FileCheck %t/mod1.cppm
16+
17+
//--- hello.h
18+
template <typename V> int get() noexcept {return 0;};
19+
20+
template <typename T>
21+
class List
22+
{
23+
template <typename V> friend int get() noexcept;
24+
};
25+
26+
//--- mod2.cppm
27+
module;
28+
#include "hello.h"
29+
export module Mod2;
30+
export const char *modFn2() {
31+
List<int> a;
32+
return "hello";
33+
}
34+
35+
//--- mod1.cppm
36+
module;
37+
#include "hello.h"
38+
export module Mod;
39+
import Mod2;
40+
export extern "C" const char *modFn() {
41+
List<int> a;
42+
List<double> b;
43+
return modFn2();
44+
}
45+
46+
// Fine enough to check it won't crash.
47+
// CHECK: define {{.*}}@modFn
48+
49+
//--- test.cc
50+
// expected-no-diagnostics
51+
import Mod;
52+
import Mod2;
53+
54+
void test() {
55+
modFn();
56+
modFn2();
57+
}

0 commit comments

Comments
 (0)