Skip to content
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

[TypeChecker] Implement limited set of conversions between Swift and C pointers #37956

Merged
merged 14 commits into from
Oct 6, 2021

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Jun 16, 2021

Allow following pointer conversions in argument positions to C/ObjC imported calls:

  • Allow implicit conversion to Unsafe[Mutable]Pointer<UInt8> or
    Unsafe[Mutable]Pointer<Int8> when the argument type is either
    Unsafe[Mutable]Pointer<T> or Unsafe[Mutable]RawPointer.

  • Allow implicit conversion between signed and unsigned variants of trivial integer types:
    e.g. Unsafe[Mutable]Pointer<Int64> to/from Unsafe[Mutable]Pointer<UInt64>

Resolves: SR-10246

@xedin
Copy link
Contributor Author

xedin commented Jun 16, 2021

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - 7051c5a0595792bfb60f3db763b640a2f7ca1ca2

Install command
tar zxf swift-PR-37956-608-ubuntu16.04.tar.gz
More info

@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from 7051c5a to 2351391 Compare June 29, 2021 19:08
@xedin
Copy link
Contributor Author

xedin commented Jun 29, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 2351391f8fbfc68d5b6bcf9a8d960662fc29386a

@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from 2351391 to a10d35c Compare June 29, 2021 20:42
@xedin
Copy link
Contributor Author

xedin commented Jun 29, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a10d35cb0ff7fbd5af03e622eb81164968a1a116

@xedin
Copy link
Contributor Author

xedin commented Jun 29, 2021

@swift-ci please test Linux platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - a10d35cb0ff7fbd5af03e622eb81164968a1a116

@xedin
Copy link
Contributor Author

xedin commented Jun 29, 2021

@swift-ci please test macOS platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - a10d35cb0ff7fbd5af03e622eb81164968a1a116

lib/Sema/CSSimplify.cpp Outdated Show resolved Hide resolved
@theblixguy theblixguy added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Aug 11, 2021
@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from a10d35c to b145dc1 Compare August 12, 2021 01:17
@xedin
Copy link
Contributor Author

xedin commented Aug 12, 2021

@swift-ci please clean test

1 similar comment
@xedin
Copy link
Contributor Author

xedin commented Aug 12, 2021

@swift-ci please clean test

@swiftlang swiftlang deleted a comment from swift-ci Aug 12, 2021
@xedin
Copy link
Contributor Author

xedin commented Aug 12, 2021

@swift-ci please build toolchain

@xedin
Copy link
Contributor Author

xedin commented Aug 12, 2021

@swift-ci build toolchain

@xedin
Copy link
Contributor Author

xedin commented Aug 12, 2021

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - d89463d464bfa4abff37280cfb3e894ff222c272

Install command
tar zxf swift-PR-37956-638-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

macOS Toolchain
Download Toolchain
Git Sha - d89463d464bfa4abff37280cfb3e894ff222c272

Install command
tar -zxf swift-PR-37956-1079-osx.tar.gz --directory ~/

@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from d89463d to 0ec00f5 Compare August 24, 2021 05:00
@xedin xedin marked this pull request as ready for review September 20, 2021 20:42
@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from 0ec00f5 to 4690092 Compare September 20, 2021 20:47
@xedin xedin requested a review from hborla September 20, 2021 20:47
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
Allow following conversions in argument positions
(applies only to call to imported C/ObjC declarations):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> <-> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
…onal types

Support Swift -> C pointer conversions even if argument required
a value to optional promotion or is optional.
Make sure that conversions are properly supported when arguments
either is wrapped in an optional type or requires a value-to-optional
promotion because parameter is optional pointer.
…nversions

Just like other implicit conversions - always prefer solutions with
the lowest possible number of them.
This is a skeleton of a fix that would be used to diagnose situations
when Swift -> C pointer conversion was attempted on a Swift function.
…sion

Diagnose situations where Swift -> C pointer implicit conversion
is attempted on a Swift function instead of one imported from C header.

```swift
func test(_: UnsafePointer<UInt8>) {}

func pass_ptr(ptr: UnsafeRawPointer) {
  test(ptr) // Only okay if `test` was an imported C function.
}
```
…s for optionals

Wait for a value-to-optional promotion or optional-to-optional conversion
to happen before attempting Swift -> C pointer conversion.
Detect and diagnose situations when Swift -> C pointer conversion
is unsupported due to method not being imported from C header.
@xedin xedin force-pushed the implicit-swift-to-c-ptr-conversions branch from 4690092 to a9fdb7a Compare September 21, 2021 00:22
@xedin
Copy link
Contributor Author

xedin commented Sep 21, 2021

@swift-ci please clean test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a9fdb7a

@xedin
Copy link
Contributor Author

xedin commented Sep 21, 2021

@swift-ci please test Linux platform

@benrimmington benrimmington added swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process and removed swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review labels Oct 5, 2021
@xedin
Copy link
Contributor Author

xedin commented Oct 6, 2021

@swift-ci please smoke test

@xedin xedin changed the title [DNM][TypeChecker] Implement limited set of conversions between Swift and C pointers [TypeChecker] Implement limited set of conversions between Swift and C pointers Oct 6, 2021
@xedin xedin merged commit 1c0a306 into swiftlang:main Oct 6, 2021
@xedin
Copy link
Contributor Author

xedin commented Oct 6, 2021

I'm going to land changes to CHANGELOG separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants