Skip to content

Commit 198e967

Browse files
authored
Merge pull request #59207 from Huddie/update-cxx-operator-impl-templates-off
[cxx interoperability] Turn off non subscript templated operators
2 parents 2741944 + 0d7a041 commit 198e967

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
#include <algorithm>
7979
#include <string>
8080
#include <memory>
81-
#include <string>
8281

8382
using namespace swift;
8483
using namespace importer;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,6 +4153,17 @@ namespace {
41534153
if (!dc)
41544154
return nullptr;
41554155

4156+
bool isOperator = decl->getDeclName().getNameKind() ==
4157+
clang::DeclarationName::CXXOperatorName;
4158+
bool isNonSubscriptOperator =
4159+
isOperator && (decl->getDeclName().getCXXOverloadedOperator() !=
4160+
clang::OO_Subscript);
4161+
4162+
// For now, we don't support non-subscript operators which are templated
4163+
if (isNonSubscriptOperator && decl->isTemplated()) {
4164+
return nullptr;
4165+
}
4166+
41564167
// Handle cases where 2 CXX methods differ strictly in "constness"
41574168
// In such a case append a suffix ("Mutating") to the mutable version
41584169
// of the method when importing to swift

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,24 @@ template<class T> struct TemplatedArrayByVal {
186186
T ts[];
187187
T operator[](int i) { return ts[i]; }
188188
};
189+
189190
typedef TemplatedArrayByVal<double> TemplatedDoubleArrayByVal;
190191

191-
struct TemplatedSubscriptArrayByVal {
192+
template <class T>
193+
struct TemplatedByVal {
194+
T val;
195+
TemplatedByVal<T> operator+(TemplatedByVal other) {
196+
return TemplatedByVal{.val = val + other.val};
197+
}
198+
};
199+
200+
struct TemplatedOperatorArrayByVal {
192201
int *ptr;
193202
template<class T> T operator[](T i) { return ptr[i]; }
203+
template <class T>
204+
T *operator+(T i) {
205+
return ptr + i;
206+
}
194207
};
195208

196209
struct NonTrivial {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@
133133
// CHECK: }
134134
// CHECK: typealias TemplatedDoubleArrayByVal = __CxxTemplateInst19TemplatedArrayByValIdE
135135

136+
// CHECK: struct TemplatedByVal<T> {
137+
// CHECK-NEXT: }
136138

137-
// CHECK: struct TemplatedSubscriptArrayByVal {
139+
// CHECK: struct TemplatedOperatorArrayByVal {
138140
// CHECK: subscript(i: T) -> T { mutating get }
139141
// CHECK: @available(*, unavailable, message: "use subscript")
140142
// CHECK: mutating func __operatorSubscriptConst<T>(_ i: T) -> T
143+
// CHECK-NOT: mutating func __operatorPlus<T>(_ i: T) -> UnsafeMutablePointer<T>
141144
// CHECK: }
142145

143146
// CHECK: struct NonTrivialArrayByVal {

0 commit comments

Comments
 (0)