Skip to content

Commit 7b21437

Browse files
authored
Merge pull request #86004 from egorzhdan/egorzhdan/renamed-operators
[cxx-interop] Import operators renamed via `swift_name` consistently
2 parents 828813c + 9fafb64 commit 7b21437

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,11 @@ namespace {
37223722
clang::OverloadedOperatorKind cxxOperatorKind) {
37233723
if (cxxOperatorKind == clang::OverloadedOperatorKind::OO_None)
37243724
return true;
3725+
// If this operator was renamed via swift_name attribute, the imported
3726+
// Swift function already has the specified name. Do not apply any special
3727+
// handling to it.
3728+
if (importedName.hasCustomName())
3729+
return true;
37253730

37263731
auto dc = func->getDeclContext();
37273732
auto typeDecl = dc->getSelfNominalTypeDecl();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ module NonMemberOutOfLine {
1717
header "non-member-out-of-line.h"
1818
requires cplusplus
1919
}
20+
21+
module RenamedOperators {
22+
header "renamed-operators.h"
23+
requires cplusplus
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct HasRenamedOperatorStar {
2+
int value;
3+
4+
const int &operator*() const __attribute__((swift_name("dereference()"))) {
5+
return value;
6+
}
7+
};
8+
9+
struct HasRenamedOperatorPlusPlus {
10+
int value;
11+
12+
HasRenamedOperatorPlusPlus &operator++() __attribute__((swift_name("plusPlus()"))) {
13+
value++;
14+
return *this;
15+
}
16+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=RenamedOperators -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s
2+
3+
// CHECK: struct HasRenamedOperatorStar {
4+
// CHECK-NOT: prefix static func * (lhs: HasRenamedOperatorStar)
5+
// CHECK: func dereference() -> UnsafePointer<Int32>
6+
// CHECK: }
7+
8+
// CHECK: struct HasRenamedOperatorPlusPlus {
9+
// CHECK-NOT: prefix static func ++ (lhs: HasRenamedOperatorPlusPlus)
10+
// CHECK: mutating func plusPlus() -> UnsafeMutablePointer<HasRenamedOperatorPlusPlus>
11+
// CHECK: }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=default
2+
3+
import RenamedOperators
4+
5+
let star = HasRenamedOperatorStar(value: 123)
6+
_ = *star // expected-error {{'*' is not a prefix unary operator}}
7+
8+
let plusPlus = HasRenamedOperatorStar(value: 123)
9+
plusPlus++ // expected-error {{cannot find operator '++' in scope; did you mean '+= 1'?}}

0 commit comments

Comments
 (0)