Skip to content

Commit 8023810

Browse files
committed
[interop][SwiftToCxx] ensure swift::Int and swift::UInt are usable in generic context
Fixes swiftlang#63452 (cherry picked from commit 85a431e)
1 parent 5550394 commit 8023810

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/Type.h"
2020
#include "swift/IRGen/IRABIDetailsProvider.h"
2121
#include "swift/IRGen/Linking.h"
22+
#include "clang/Basic/TargetInfo.h"
2223
#include "llvm/ADT/STLExtras.h"
2324

2425
using namespace swift;
@@ -164,9 +165,26 @@ void printPrimitiveGenericTypeTraits(raw_ostream &os, ASTContext &astContext,
164165

165166
// Pointer types.
166167
// FIXME: support raw pointers?
167-
astContext.getOpaquePointerType()};
168-
169-
for (Type type : llvm::makeArrayRef(supportedPrimitiveTypes)) {
168+
astContext.getOpaquePointerType(),
169+
170+
astContext.getIntType(), astContext.getUIntType()};
171+
172+
auto primTypesArray = llvm::makeArrayRef(supportedPrimitiveTypes);
173+
174+
// Ensure that `long` and `unsigned long` are treated as valid
175+
// generic Swift types (`Int` and `UInt`) on platforms
176+
// that do define `Int`/`ptrdiff_t` as `long` and don't define `int64_t` to be
177+
// `long`.
178+
auto &clangTI =
179+
astContext.getClangModuleLoader()->getClangASTContext().getTargetInfo();
180+
bool isSwiftIntLong =
181+
clangTI.getPtrDiffType(0) == clang::TransferrableTargetInfo::SignedLong;
182+
bool isInt64Long =
183+
clangTI.getInt64Type() == clang::TransferrableTargetInfo::SignedLong;
184+
if (!(isSwiftIntLong && !isInt64Long))
185+
primTypesArray = primTypesArray.drop_back(2);
186+
187+
for (Type type : primTypesArray) {
170188
auto typeInfo = *typeMapping.getKnownCxxTypeInfo(
171189
type->getNominalOrBoundGenericNominal());
172190

test/Interop/SwiftToCxx/core/swift-impl-defs-in-cxx.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@
9393
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSdN;
9494
// CHECK-NEXT: // type metadata address for OpaquePointer.
9595
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss13OpaquePointerVN;
96-
// CHECK-EMPTY:
97-
// CHECK-NEXT: #ifdef __cplusplus
96+
// CHECK: #ifdef __cplusplus
9897
// CHECK-NEXT: }
9998
// CHECK-NEXT: #endif
10099
// CHECK-EMPTY:
@@ -224,7 +223,7 @@
224223
// CHECK-NEXT: }
225224
// CHECK-NEXT: };
226225
// CHECK-EMPTY:
227-
// CHECK-NEXT: #pragma clang diagnostic pop
226+
// CHECK: #pragma clang diagnostic pop
228227
// CHECK-EMPTY:
229228
// CHECK-NEXT: } // namespace swift
230229
// CHECK-EMPTY:

test/Interop/SwiftToCxx/stdlib/stdlib-dep-inline-in-cxx.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public func test() -> String {
1717
return ""
1818
}
1919

20+
@_expose(Cxx)
21+
public func testIntArray() -> [Int] {
22+
return []
23+
}
24+
2025
// CHECK: namespace swift SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("swift") {
2126
// CHECK: class SWIFT_SYMBOL("{{.*}}") String final {

0 commit comments

Comments
 (0)