Skip to content

Commit e7fe6f0

Browse files
committed
Fix tests and re-enable support for CXX operators
1 parent 540e52a commit e7fe6f0

22 files changed

+53
-83
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,7 +2807,6 @@ static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
28072807

28082808
const clang::Decl *D = ClangNode.castAsDecl();
28092809
auto &ClangASTContext = ModuleFilter->getClangASTContext();
2810-
28112810
// We don't handle Clang submodules; pop everything up to the top-level
28122811
// module.
28132812
auto OwningClangModule = getClangTopLevelOwningModule(ClangNode,
@@ -2880,8 +2879,8 @@ class FilteringVisibleDeclConsumer : public swift::VisibleDeclConsumer {
28802879

28812880
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
28822881
DynamicLookupInfo dynamicLookupInfo) override {
2883-
if (isVisibleFromModule(ModuleFilter, VD))
2884-
NextConsumer.foundDecl(VD, Reason, dynamicLookupInfo);
2882+
if (!VD->hasClangNode() || isVisibleFromModule(ModuleFilter, VD))
2883+
NextConsumer.foundDecl(VD, Reason, dynamicLookupInfo);
28852884
}
28862885
};
28872886

@@ -3112,7 +3111,7 @@ void ClangImporter::lookupValue(DeclName name, VisibleDeclConsumer &consumer) {
31123111
ClangNode ClangImporter::getEffectiveClangNode(const Decl *decl) const {
31133112
// Directly...
31143113
if (auto clangNode = decl->getClangNode())
3115-
return clangNode;
3114+
return clangNode;
31163115

31173116
// Or via the nested "Code" enum.
31183117
if (auto *errorWrapper = dyn_cast<StructDecl>(decl)) {
@@ -4121,43 +4120,43 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
41214120
auto clangTU = clangCtx.getTranslationUnitDecl();
41224121

41234122
bool declFound = false;
4124-
// For operators we have to look up static member functions in addition to the
4125-
// top-level function lookup below.
4126-
auto declBaseName = (SwiftContext.LangOpts.EnableCXXInterop && name.isOperator())
4127-
? DeclBaseName(SwiftContext.getIdentifier("__operator" + getOperatorNameForToken(std::string{name.getBaseName().getIdentifier()})))
4128-
: name.getBaseName();
41294123

41304124
if (name.isOperator()) {
4131-
for (auto entry : table.lookupMemberOperators(declBaseName)) {
4132-
if (isVisibleClangEntry(entry)) {
4133-
if (auto decl = dyn_cast_or_null<ValueDecl>(
4134-
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4135-
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4136-
declFound = true;
4137-
for (auto alternate: getAlternateDecls(decl)) {
4138-
if (alternate->getName().matchesRef(name)) {
4139-
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4140-
DynamicLookupInfo::AnyObject);
4141-
}
4142-
}
4143-
}
4144-
}
4145-
}
4146-
for (auto entry : table.lookupMemberOperators(name.getBaseName())) {
4147-
if (isVisibleClangEntry(entry)) {
4148-
if (auto decl = dyn_cast_or_null<ValueDecl>(
4149-
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4150-
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4151-
declFound = true;
4152-
for (auto alternate : getAlternateDecls(decl)) {
4153-
if (alternate->getName().matchesRef(name)) {
4154-
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4155-
DynamicLookupInfo::AnyObject);
4156-
}
4157-
}
4158-
}
4159-
}
4160-
}
4125+
for (auto entry : table.lookupMemberOperators(name.getBaseName())) {
4126+
if (isVisibleClangEntry(entry)) {
4127+
if (auto decl = dyn_cast_or_null<ValueDecl>(
4128+
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4129+
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4130+
declFound = true;
4131+
for (auto alternate : getAlternateDecls(decl)) {
4132+
if (alternate->getName().matchesRef(name)) {
4133+
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4134+
DynamicLookupInfo::AnyObject);
4135+
}
4136+
}
4137+
}
4138+
}
4139+
}
4140+
4141+
// If CXXInterop is enabled we need to check the modified operator name as well
4142+
if(SwiftContext.LangOpts.EnableCXXInterop) {
4143+
auto declBaseName = DeclBaseName(SwiftContext.getIdentifier("__operator" + getOperatorNameForToken(name.getBaseName().getIdentifier().str().str())));
4144+
for (auto entry : table.lookupMemberOperators(declBaseName)) {
4145+
if (isVisibleClangEntry(entry)) {
4146+
if (auto decl = dyn_cast_or_null<ValueDecl>(
4147+
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4148+
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4149+
declFound = true;
4150+
for (auto alternate: getAlternateDecls(decl)) {
4151+
if (alternate->getName().matchesRef(name)) {
4152+
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4153+
DynamicLookupInfo::AnyObject);
4154+
}
4155+
}
4156+
}
4157+
}
4158+
}
4159+
}
41614160
}
41624161

41634162
for (auto entry : table.lookup(name.getBaseName(), clangTU)) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,12 +3601,12 @@ namespace {
36013601
cxxOperatorKind != clang::OverloadedOperatorKind::OO_Call &&
36023602
cxxOperatorKind != clang::OverloadedOperatorKind::OO_Subscript) {
36033603

3604-
auto d = makeOperator(MD, cxxMethod);
3605-
result->addMember(d);
3604+
auto opFuncDecl = makeOperator(MD, cxxMethod);
36063605

3607-
Impl.addAlternateDecl(MD, d);
3606+
Impl.addAlternateDecl(MD, opFuncDecl);
3607+
3608+
Impl.markUnavailable(MD, "use " + std::string{clang::getOperatorSpelling(cxxOperatorKind)} + " instead");
36083609

3609-
Impl.markUnavailable(MD, "use - operator");
36103610
// Make the actual member operator private.
36113611
MD->overwriteAccess(AccessLevel::Private);
36123612
}
@@ -4151,11 +4151,6 @@ namespace {
41514151
if (!dc)
41524152
return nullptr;
41534153

4154-
// Support for importing operators is temporarily disabled: rdar://91070109
4155-
if (decl->getDeclName().getNameKind() == clang::DeclarationName::CXXOperatorName &&
4156-
decl->getDeclName().getCXXOverloadedOperator() != clang::OO_Subscript)
4157-
return nullptr;
4158-
41594154
// Handle cases where 2 CXX methods differ strictly in "constness"
41604155
// In such a case append a suffix ("Mutating") to the mutable version
41614156
// of the method when importing to swift

test/Interop/Cxx/implementation-only-imports/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ module DeclB {
2828
requires cplusplus
2929
}
3030

31+
module Helper {
32+
header "helper.h"
33+
export *
34+
requires cplusplus
35+
}
3136

test/Interop/Cxx/implementation-only-imports/check-operator-visibility-inversed.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// that the operator decl can be found when at least one of the
99
// modules is not `@_implementationOnly`.
1010

11-
// XFAIL: *
12-
1311
import UserA
1412
@_implementationOnly import UserB
1513

test/Interop/Cxx/implementation-only-imports/check-operator-visibility.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// that the operator decl can be found when at least one of the
99
// modules is not `@_implementationOnly`.
1010

11-
// XFAIL: *
12-
1311
@_implementationOnly import UserA
1412
import UserB
1513

test/Interop/Cxx/implementation-only-imports/skip-forward-declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ public func createAWrapper() {
1212
let _ = MagicWrapper()
1313
}
1414

15-
// CHECK: struct 'MagicWrapper' cannot be used in an '@inlinable' function because 'UserA' was imported implementation-only
15+
// CHECK: struct 'MagicWrapper' cannot be used in an '@inlinable' function because 'Helper' was imported implementation-only

test/Interop/Cxx/namespace/free-functions-module-interface.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// CHECK-NEXT: struct X {
88
// CHECK-NEXT: init()
99
// CHECK-NEXT: }
10-
// TODO: check this again when operators are re-enabled: rdar://91070109 (also please enable this in the execution test "free-functions.swift")
11-
// CHECK-NOT: static func +
10+
// CHECK-NEXT: static func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!
1211
// CHECK-NEXT: static func sameNameInChild() -> UnsafePointer<CChar>!
1312
// CHECK-NEXT: static func sameNameInSibling() -> UnsafePointer<CChar>!
1413
// CHECK-NEXT: enum FunctionsNS2 {
@@ -21,8 +20,7 @@
2120
// CHECK-NEXT: static func definedInDefs() -> UnsafePointer<CChar>!
2221
// CHECK-NEXT: }
2322

24-
// TODO: check this again when operators are re-enabled: rdar://91070109 (also please enable this in the execution test "free-functions.swift")
25-
// CHECK-NOT: static func +
23+
// CHECK-NEXT: static func + (_: FunctionsNS1.X, _: FunctionsNS1.X) -> UnsafePointer<CChar>!
2624

2725
// CHECK-NEXT: enum FunctionsNS4 {
2826
// CHECK-NEXT: static func sameNameInSibling() -> UnsafePointer<CChar>!

test/Interop/Cxx/namespace/free-functions.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ NamespacesTestSuite.test("Basic functions") {
2020
expectEqual(String(cString: basicFunctionLowestLevelCString!),
2121
"FunctionsNS1::FunctionsNS2::FunctionsNS3::basicFunctionLowestLevel")
2222

23-
// TODO: check this again when operators are re-enabled: rdar://91070109
24-
// let x = FunctionsNS1.X()
25-
// expectEqual(String(cString: x + x),
26-
// "FunctionsNS1::operator+(X, X)")
23+
let x = FunctionsNS1.X()
24+
expectEqual(String(cString: x + x),
25+
"FunctionsNS1::operator+(X, X)")
2726
}
2827

2928
NamespacesTestSuite.test("Forward declared functions") {

test/Interop/Cxx/operators/member-inline-irgen.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
22
//
33
// We can't yet call member functions correctly on Windows (SR-13129).
4-
// XFAIL: *
54

65
import MemberInline
76

test/Interop/Cxx/operators/member-inline-module-interface.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=MemberInline -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
2-
// XFAIL: *
32

43
// CHECK: struct LoadableIntWrapper {
54
// CHECK: static func - (lhs: inout LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper

test/Interop/Cxx/operators/member-inline-silgen.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
2-
// XFAIL: *
32

43
import MemberInline
54

test/Interop/Cxx/operators/member-inline-typechecker.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
2-
// XFAIL: *
32

43
import MemberInline
54

test/Interop/Cxx/operators/member-inline.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
22

33
// REQUIRES: executable_test
4-
// XFAIL: *
54

65
import MemberInline
76
import StdlibUnittest

test/Interop/Cxx/operators/member-out-of-line-irgen.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
22
//
33
// We can't yet call member functions correctly on Windows (SR-13129).
4-
// XFAIL: *
54

65
import MemberOutOfLine
76

test/Interop/Cxx/operators/member-out-of-line-silgen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s -check-prefix CHECK-%target-abi
22

3-
// XFAIL: *
4-
53
import MemberOutOfLine
64

75
public func add(_ lhs: LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs + rhs }

test/Interop/Cxx/operators/member-out-of-line.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
// REQUIRES: executable_test
88

9-
// XFAIL: *
10-
119
import MemberOutOfLine
1210
import StdlibUnittest
1311

test/Interop/Cxx/operators/non-member-inline-module-interface.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=NonMemberInline -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

3-
// XFAIL: *
4-
53
// CHECK: func + (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper
64
// CHECK-NEXT: func - (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper
75
// CHECK-NEXT: func * (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper

test/Interop/Cxx/operators/non-member-inline-typechecker.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
22

3-
// XFAIL: *
4-
53
import NonMemberInline
64

75
let lhs = LoadableIntWrapper(value: 42)

test/Interop/Cxx/operators/non-member-inline.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//
33
// REQUIRES: executable_test
44

5-
// XFAIL: *
6-
75
import NonMemberInline
86
import StdlibUnittest
97

test/Interop/Cxx/operators/non-member-out-of-line-irgen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
22

3-
// XFAIL: *
4-
53
import NonMemberOutOfLine
64

75
public func add(_ lhs: LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs + rhs }

test/Interop/Cxx/operators/non-member-out-of-line-silgen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
22

3-
// XFAIL: *
4-
53
import NonMemberOutOfLine
64

75
public func add(_ lhs: LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs + rhs }

test/Interop/Cxx/operators/non-member-out-of-line.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
// REQUIRES: executable_test
88

9-
// XFAIL: *
10-
119
import NonMemberOutOfLine
1210
import StdlibUnittest
1311

0 commit comments

Comments
 (0)