Skip to content

Commit 746080d

Browse files
committed
[ClangImporter] Suffix ambiguous protocol names if C++ interop is enabled
The Clang Importer when C++ interop is not enabled, disambigate an Obj-C class and protocol that are named the same by appending `Protocol` to the protocol. This was not happening when C++ interop was enabled, but should also apply to Obj-C++ modules. The fix is providing an starting scope for the search, which the C++ name lookup need to actually find the similarly named counterpart. Includes a test to avoid this problem creeping in again, and locally it did not break any other tests.
1 parent 58b414d commit 746080d

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -999,15 +999,20 @@ bool NameImporter::hasNamingConflict(const clang::NamedDecl *decl,
999999
lookupResult.setAllowHidden(true);
10001000
lookupResult.suppressDiagnostics();
10011001

1002-
if (clangSema.LookupName(lookupResult, /*scope=*/nullptr)) {
1002+
if (clangSema.LookupName(lookupResult, /*scope=*/clangSema.TUScope)) {
10031003
if (std::any_of(lookupResult.begin(), lookupResult.end(), conflicts))
10041004
return true;
10051005
}
10061006

1007-
lookupResult.clear(clang::Sema::LookupTagName);
1008-
if (clangSema.LookupName(lookupResult, /*scope=*/nullptr)) {
1009-
if (std::any_of(lookupResult.begin(), lookupResult.end(), conflicts))
1010-
return true;
1007+
// No need to lookup tags if we are using C++ mode.
1008+
if (!clang::LangStandard::getLangStandardForKind(
1009+
clangSema.getLangOpts().LangStd)
1010+
.isCPlusPlus()) {
1011+
lookupResult.clear(clang::Sema::LookupTagName);
1012+
if (clangSema.LookupName(lookupResult, /*scope=*/nullptr)) {
1013+
if (std::any_of(lookupResult.begin(), lookupResult.end(), conflicts))
1014+
return true;
1015+
}
10111016
}
10121017

10131018
return false;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@protocol TheClashingName
2+
@end
3+
4+
@interface TheClashingName <TheClashingName>
5+
@end

test/Interop/Cxx/class/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ module InvalidNestedStruct {
8787
header "invalid-nested-struct.h"
8888
requires cplusplus
8989
}
90+
91+
module ClassProtocolNameClash {
92+
header "class-protocol-name-clash.h"
93+
requires cplusplus
94+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-swift-frontend %s -c -enable-cxx-interop -I %S/Inputs
2+
//
3+
// REQUIRES: objc_interop
4+
5+
import ClassProtocolNameClash
6+
7+
class Subclass : TheClashingName {}

0 commit comments

Comments
 (0)