Skip to content

Commit 93ebe63

Browse files
committed
[C++20] [Modules] Fix ADL for friend in modules
Close #170235 The cause of the issue is it didn't check friendness for decls in ordinary namespace if it isn't visible. It is fine for code before modules, since everything is visible. But it is not true after modules came in. This patch adjusts this. Note that this doesn't change the control flow for non-modules codes, as the decls in ordinary namespace is always visible then it won't never fall in following friendness check.
1 parent 82c6ad6 commit 93ebe63

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

clang/lib/Sema/SemaLookup.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3927,7 +3927,8 @@ void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc,
39273927
break;
39283928
}
39293929

3930-
if (!getLangOpts().CPlusPlusModules)
3930+
if (!D->getOwningModule() ||
3931+
!D->getOwningModule()->getTopLevelModule()->isNamedModule())
39313932
continue;
39323933

39333934
if (D->isInExportDeclContext()) {
@@ -3959,7 +3960,9 @@ void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc,
39593960
break;
39603961
}
39613962
}
3962-
} else if (D->getFriendObjectKind()) {
3963+
}
3964+
3965+
if (D->getFriendObjectKind()) {
39633966
auto *RD = cast<CXXRecordDecl>(D->getLexicalDeclContext());
39643967
// [basic.lookup.argdep]p4:
39653968
// Argument-dependent lookup finds all declarations of functions and

clang/test/Modules/pr170235.cppm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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/lib.cppm -emit-module-interface -o %t/lib.pcm
6+
// RUN: %clang_cc1 -std=c++20 %t/main.cpp -fmodule-file=lib=%t/lib.pcm -fsyntax-only -verify
7+
//
8+
// RUN: %clang_cc1 -std=c++20 %t/lib.cppm -emit-reduced-module-interface -o %t/lib.pcm
9+
// RUN: %clang_cc1 -std=c++20 %t/main.cpp -fmodule-file=lib=%t/lib.pcm -fsyntax-only -verify
10+
11+
//--- lib.cppm
12+
export module lib;
13+
namespace lib {
14+
struct A;
15+
// Definition comes BEFORE the class declaration
16+
int foo(const A &, int) { return 42; }
17+
18+
struct A {
19+
// Friend declaration inside the class
20+
friend int foo(const A &, int);
21+
};
22+
23+
export A a{};
24+
}
25+
//--- main.cpp
26+
// expected-no-diagnostics
27+
import lib;
28+
int main() {
29+
// Should be found via ADL since lib::a is of type lib::A
30+
auto res1 = foo(lib::a, 1);
31+
return 0;
32+
}

0 commit comments

Comments
 (0)