Skip to content

Commit 9babd8f

Browse files
committed
[cxx-interop][ClangImporter] Fix protocol renaming issue with objc++ interop enabled
When constructing SwiftLookupTable at clang module creation time, calls to `clang::Sema::LookupName` will fail in C++ mode (including ObjC++). The specific reason is that `clangSema.TUScope` (which we are passing in as a Scope) is `nullptr` as we have no active Parser. The C++ path in `clang::Sema::LookupName` is set to fail early and hard when the Scope passed in is nullptr. In most, if not all, calls to `LookupName`, we care about ObjC symbols and not C++ names. For example, the motivation behind this issue is that ObjC protocols are not being renamed when there is an ObjC class with the same name, leading to compilation failures.
1 parent 8b9248a commit 9babd8f

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

lib/ClangImporter/ImportName.cpp

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

1002-
if (clangSema.LookupName(lookupResult, /*scope=*/clangSema.TUScope)) {
1002+
// Only force the Objective-C codepath in LookupName if clangSema.TUScope is
1003+
// nullptr
1004+
if (clangSema.LookupName(lookupResult, /*scope=*/clangSema.TUScope,
1005+
/*AllowBuiltinCreation=*/false,
1006+
/*ForceNoCPlusPlus=*/!clangSema.TUScope)) {
10031007
if (std::any_of(lookupResult.begin(), lookupResult.end(), conflicts))
10041008
return true;
10051009
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ProtocolNamingConflict [extern_c] {
2+
header "protocol-naming-conflict.h"
3+
requires objc
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@protocol Foo
2+
@end
3+
@interface Foo <Foo>
4+
@end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ProtocolNamingConflict -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -enable-objc-interop | %FileCheck -check-prefix=CHECK-IDE-TEST %s
2+
// RUN: %swift-frontend -c -enable-experimental-cxx-interop -enable-objc-interop -I %S/Inputs %s -emit-sil -o - | %FileCheck %s
3+
4+
// REQUIRES: objc_interop
5+
6+
import ProtocolNamingConflict
7+
8+
// CHECK: class Thing : FooProtocol
9+
// CHECK-IDE-TEST: protocol FooProtocol
10+
// CHECK-IDE-TEST: class Foo : FooProtocol
11+
class Thing: FooProtocol {
12+
}

0 commit comments

Comments
 (0)