-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Swiftify] Fix __sized_by and nullable return values #81693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/Interop/C/swiftify-import/Inputs/counted-by-lifetimebound.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#define __counted_by(x) __attribute__((__counted_by__(x))) | ||
#define __lifetimebound __attribute__((lifetimebound)) | ||
|
||
int * __counted_by(len) simple(int len, int len2, int * __counted_by(len2) __lifetimebound p); | ||
|
||
int * __counted_by(len) shared(int len, int * __counted_by(len) __lifetimebound p); | ||
|
||
int * __counted_by(len - offset) complexExpr(int len, int offset, int len2, int * __counted_by(len2) __lifetimebound p); | ||
|
||
int * __counted_by(len) _Null_unspecified nullUnspecified(int len, int len2, int * __counted_by(len2) __lifetimebound _Null_unspecified p); | ||
|
||
int * __counted_by(len) _Nonnull nonnull(int len, int len2, int * __counted_by(len2) __lifetimebound _Nonnull p); | ||
|
||
int * __counted_by(len) _Nullable nullable(int len, int len2, int * __counted_by(len2) __lifetimebound _Nullable p); | ||
|
||
typedef struct foo opaque_t; | ||
opaque_t * __counted_by(len) opaque(int len, int len2, opaque_t * __counted_by(len2) __lifetimebound p); | ||
|
||
int * __counted_by(len) noncountedLifetime(int len, int * __lifetimebound p); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/Interop/C/swiftify-import/Inputs/sized-by-lifetimebound.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#define __sized_by(x) __attribute__((__sized_by__(x))) | ||
#define __lifetimebound __attribute__((lifetimebound)) | ||
|
||
const void * __sized_by(len) simple(int len, int len2, const void * __sized_by(len2) __lifetimebound p); | ||
|
||
const void * __sized_by(len) shared(int len, const void * __sized_by(len) __lifetimebound p); | ||
|
||
const void * __sized_by(len - offset) complexExpr(int len, int offset, int len2, const void * __sized_by(len2) __lifetimebound p); | ||
|
||
const void * __sized_by(len) _Null_unspecified nullUnspecified(int len, int len2, const void * __sized_by(len2) __lifetimebound _Null_unspecified p); | ||
|
||
const void * __sized_by(len) _Nonnull nonnull(int len, int len2, const void * __sized_by(len2) __lifetimebound _Nonnull p); | ||
|
||
const void * __sized_by(len) _Nullable nullable(int len, int len2, const void * __sized_by(len2) __lifetimebound _Nullable p); | ||
|
||
typedef struct foo opaque_t; | ||
opaque_t * __sized_by(len) opaque(int len, int len2, opaque_t * __sized_by(len2) __lifetimebound p); | ||
|
||
const void * __sized_by(len) nonsizedLifetime(int len, const void * __lifetimebound p); |
96 changes: 96 additions & 0 deletions
96
test/Interop/C/swiftify-import/counted-by-lifetimebound.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
// REQUIRES: swift_feature_LifetimeDependence | ||
|
||
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByLifetimeboundClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s | ||
|
||
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedByLifetimebound.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature LifetimeDependence %s | ||
|
||
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __sized_by __lifetimebound parameters and return values. | ||
|
||
import CountedByLifetimeboundClang | ||
|
||
// CHECK: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func complexExpr(_ len: Int32, _ offset: Int32, _ len2: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32> | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(borrow p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func noncountedLifetime(_ len: Int32, _ p: UnsafeMutablePointer<Int32>!) -> MutableSpan<Int32> | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ len: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32> | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ len: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32> | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ len: Int32, _ p: inout MutableSpan<Int32>?) -> MutableSpan<Int32>? | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32> | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @lifetime(p: copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ len: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32> | ||
|
||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callComplexExpr(_ p: inout MutableSpan<CInt>) { | ||
let _: MutableSpan<CInt> = complexExpr(73, 37, 42, &p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNonnull(_ p: inout MutableSpan<CInt>) { | ||
let _: MutableSpan<CInt> = nonnull(73, &p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNullUnspecified(_ p: inout MutableSpan<CInt>) { | ||
let _: MutableSpan<CInt> = nullUnspecified(73, &p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNullable(_ p: inout MutableSpan<CInt>?) { | ||
let _: MutableSpan<CInt> = nullable(73, &p)! | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callShared(_ p: inout MutableSpan<CInt>) { | ||
let _: MutableSpan<CInt> = shared(CInt(p.count), &p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callSimple(_ p: inout MutableSpan<CInt>) { | ||
let _: MutableSpan<CInt> = simple(73, &p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNoncountedLifetime(_ p: UnsafeMutablePointer<CInt>) { | ||
let _: MutableSpan<CInt> = noncountedLifetime(73, p) | ||
} |
95 changes: 95 additions & 0 deletions
95
test/Interop/C/swiftify-import/sized-by-lifetimebound.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
// REQUIRES: swift_feature_LifetimeDependence | ||
|
||
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByLifetimeboundClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s | ||
|
||
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/SizedByLifetimebound.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature LifetimeDependence %s | ||
|
||
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __sized_by __lifetimebound parameters and return values. | ||
|
||
import SizedByLifetimeboundClang | ||
|
||
// CHECK: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func complexExpr(_ len: Int32, _ offset: Int32, _ len2: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ len: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(borrow p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nonsizedLifetime(_ len: Int32, _ p: UnsafeRawPointer!) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ len: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ len: Int32, _ p: RawSpan?) -> RawSpan? | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func opaque(_ len: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop | ||
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
// CHECK-NEXT: @lifetime(copy p) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ len: Int32, _ p: RawSpan) -> RawSpan | ||
|
||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callComplexExpr(_ p: RawSpan) { | ||
let _: RawSpan = complexExpr(73, 37, 42, p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNonnull(_ p: RawSpan) { | ||
let _: RawSpan = nonnull(73, p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNullUnspecified(_ p: RawSpan) { | ||
let _: RawSpan = nullUnspecified(73, p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNullable(_ p: RawSpan?) { | ||
let _: RawSpan = nullable(73, p)! | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callShared(_ p: RawSpan) { | ||
let _: RawSpan = shared(CInt(p.byteCount), p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callSimple(_ p: RawSpan) { | ||
let _: RawSpan = simple(73, p) | ||
} | ||
|
||
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) | ||
@inlinable | ||
public func callNonsizedLifetime(_ p: UnsafeRawPointer) { | ||
let _: RawSpan = nonsizedLifetime(73, p) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't think result in two calls to
_swiftifyOverrideLifetime()
, one inside the other? Is that necessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is necessary until we change the macro to not use immediately called closures for nullable pointers, because we need it each time a Span is returned from a function (which it technically is in the closure)