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

[SR-9100] inout-to-pointer is used when passing an optional array to an Unsafe[Mutable]RawPointer? #51597

Open
hamishknight opened this issue Oct 27, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-9100
Radar None
Original Reporter @hamishknight
Type Bug
Environment

Swift version 4.2-dev (LLVM 80f624ef0e, Clang fb15cea7bb, Swift 38c1b02180)
Target: x86_64-apple-darwin17.7.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: e4003f5cda86a842b9e9a0ce822a9a68

Issue Description:

In the following, we use an array-to-pointer conversion:

func foo(_ ptr: UnsafeRawPointer?) {
  print(ptr!.assumingMemoryBound(to: Int.self).pointee)
}

var arr: [Int] = [5]
foo(&arr)
// prints: 5

but if we make arr optional, then we use inout-to-pointer:

func foo(_ ptr: UnsafeRawPointer?) {
  print(ptr!.assumingMemoryBound(to: [Int].self).pointee)
}

var arr: [Int]? = [5]
foo(&arr)
// prints: [5]

We should really be using array-to-pointer in both examples.

@belkadan
Copy link
Contributor

Hm. They should probably be consistent, and I guess array-to-pointer is the useful one, but it's a little unfortunate that there's then no way to request inout-to-pointer.

@hamishknight
Copy link
Collaborator Author

Though not quite as convenient as an implicit conversion, using withUnsafePointer(to:_:) is one option that allows you to get inout-to-pointer for an array (although it doesn't handle optionality). But given that it's probably not as common of a thing to ask for, maybe that's an okay balance.

@belkadan
Copy link
Contributor

Oh, right, this only comes up when going straight to UnsafeRawPointer, because otherwise it's not ambiguous.

@belkadan
Copy link
Contributor

Wait, hang on. You're not supposed to use & for an immutable array-to-pointer conversion.

@hamishknight
Copy link
Collaborator Author

Oh, really? Even though it can be expressed without &, using it with & to me seems to be the natural consequence of allowing both array-to-pointer and mutable-pointer-to-immutable-pointer argument conversions (although it's modelled by a single conversion in the constraint system).

It's also consistent with inout-to-pointer when used with immutable pointer types, e.g:

func foo(_ ptr: UnsafePointer<Int>) {}

var value = 0
foo(&value)

@belkadan
Copy link
Contributor

Yeah, inout-to-pointer is different because you need the address of the variable's storage. For array-to-pointer you don't.

@hamishknight
Copy link
Collaborator Author

I guess that's fair – although IMO there's a nice symmetry in allowing & for such a conversion, as I believe it means that you should be able to refactor an UnsafeMutableRawPointer parameter to a UnsafeRawPointer parameter without breaking any call-sites.

But anyway, should I file a separate bug for it?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants