Skip to content

Commit 6c53a24

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 75b9af0 + e1eb960 commit 6c53a24

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9193,6 +9193,14 @@ static bool SwiftifiableCAT(const clang::CountAttributedType *CAT) {
91939193
return CAT && CountedByExpressionValidator().Visit(CAT->getCountExpr());
91949194
}
91959195

9196+
static bool SwiftifiablePointerType(Type swiftType) {
9197+
// don't try to transform any Swift types that _SwiftifyImport doesn't know how to handle
9198+
Type nonnullType = swiftType->lookThroughSingleOptionalType();
9199+
PointerTypeKind PTK;
9200+
return nonnullType->isOpaquePointer() ||
9201+
(nonnullType->getAnyPointerElementType(PTK) && PTK != PTK_AutoreleasingUnsafeMutablePointer);
9202+
}
9203+
91969204
void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
91979205
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers))
91989206
return;
@@ -9226,10 +9234,11 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
92269234
return false;
92279235
};
92289236
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out);
9237+
Type swiftReturnTy = MappedDecl->getResultInterfaceType();
92299238
bool returnIsStdSpan = registerStdSpanTypeMapping(
9230-
MappedDecl->getResultInterfaceType(), ClangDecl->getReturnType());
9239+
swiftReturnTy, ClangDecl->getReturnType());
92319240
auto *CAT = ClangDecl->getReturnType()->getAs<clang::CountAttributedType>();
9232-
if (SwiftifiableCAT(CAT)) {
9241+
if (SwiftifiableCAT(CAT) && SwiftifiablePointerType(swiftReturnTy)) {
92339242
printer.printCountedBy(CAT, SwiftifyInfoPrinter::RETURN_VALUE_INDEX);
92349243
attachMacro = true;
92359244
}
@@ -9243,14 +9252,15 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
92439252
for (auto [index, clangParam] : llvm::enumerate(ClangDecl->parameters())) {
92449253
auto clangParamTy = clangParam->getType();
92459254
auto swiftParam = MappedDecl->getParameters()->get(index);
9255+
Type swiftParamTy = swiftParam->getInterfaceType();
92469256
bool paramHasBoundsInfo = false;
92479257
auto *CAT = clangParamTy->getAs<clang::CountAttributedType>();
9248-
if (SwiftifiableCAT(CAT)) {
9258+
if (SwiftifiableCAT(CAT) && SwiftifiablePointerType(swiftParamTy)) {
92499259
printer.printCountedBy(CAT, index);
92509260
attachMacro = paramHasBoundsInfo = true;
92519261
}
92529262
bool paramIsStdSpan = registerStdSpanTypeMapping(
9253-
swiftParam->getInterfaceType(), clangParamTy);
9263+
swiftParamTy, clangParamTy);
92549264
paramHasBoundsInfo |= paramIsStdSpan;
92559265

92569266
bool paramHasLifetimeInfo = false;
@@ -9261,7 +9271,7 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
92619271
if (clangParam->hasAttr<clang::LifetimeBoundAttr>()) {
92629272
printer.printLifetimeboundReturn(
92639273
index, !paramHasBoundsInfo &&
9264-
swiftParam->getInterfaceType()->isEscapable());
9274+
swiftParamTy->isEscapable());
92659275
paramHasLifetimeInfo = true;
92669276
returnHasLifetimeInfo = true;
92679277
}

test/Interop/C/swiftify-import/Inputs/module.modulemap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ module SizedByNoEscapeClang {
1414
header "sized-by-noescape.h"
1515
export *
1616
}
17-

test/Interop/ObjC/lit.local.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if 'objc_interop' not in config.available_features:
2+
config.unsupported = True
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module NoSwiftifyClang {
2+
header "objc-no-swiftify.h"
3+
export *
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#define __counted_by(x) __attribute__((__counted_by__(x)))
2+
3+
@interface SomeClass
4+
- (int)numberOfSegments;
5+
@end
6+
7+
void autoreleaseParam(SomeClass * __autoreleasing * __counted_by(len) p, int len); // expected-note{{declared here}}
8+
9+
SomeClass * __autoreleasing * __counted_by(len) autoreleaseReturn(int len);

test/Interop/ObjC/swiftify-import/counted-by-in-class.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// RUN: %target-swift-ide-test -plugin-path %swift-plugin-dir -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -print-module -module-to-print=Method -source-filename=x | %FileCheck %s
66
// RUN: %target-swift-frontend -plugin-path %swift-plugin-dir -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/method.swift -dump-macro-expansions -typecheck -verify
77

8-
// REQUIRES: objc_interop
9-
108
// CHECK: class Foo {
119
// CHECK: func bar(_ p: UnsafeMutableBufferPointer<Float>)
1210

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: swift_feature_SafeInteropWrappers
2+
3+
// RUN: %target-swift-ide-test -print-module -module-to-print=NoSwiftifyClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s
4+
5+
// RUN: %empty-directory(%t)
6+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/NoSwiftify.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s -verify -verify-additional-file %S/Inputs/objc-no-swiftify.h
7+
8+
import NoSwiftifyClang
9+
10+
// CHECK-NOT: @_alwaysEmitIntoClient public func callAutoreleaseParam
11+
// CHECK-NOT: @_alwaysEmitIntoClient public func callAutoreleaseReturn
12+
13+
public func callAutoreleaseParam(_ p: UnsafeMutableBufferPointer<SomeClass>) {
14+
// expected-error@+2{{missing argument for parameter #2 in call}}
15+
// expected-error@+1{{cannot convert value of type 'UnsafeMutableBufferPointer<SomeClass>' to expected argument type 'AutoreleasingUnsafeMutablePointer<SomeClass?>'}}
16+
autoreleaseParam(p)
17+
}
18+
19+
public func callAutoreleaseReturn() {
20+
// expected-error@+1{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer<SomeClass?>?' to specified type 'UnsafeMutableBufferPointer<SomeClass>'}}
21+
let p: UnsafeMutableBufferPointer<SomeClass> = autoreleaseReturn(10)
22+
}

0 commit comments

Comments
 (0)