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-3512] Issue with IUO A
and inout Optional<A>
#46100
Comments
Comment by Omri Mor (JIRA) This seems to be a problem centered around inout and optional types. func test(val: inout Int?) { }
var a: Int = 0
test(val: &a)
// error: cannot pass immutable value as inout argument: implicit conversion from 'Int' to 'Int?' requires a temporary
func test2(val: inout [[Int]?]!) { }
var b: [[Int]] = [[1, 2, 3], [4, 5]]
test2(val: &b)
// error: cannot pass immutable value of type '[[Int]?]!' as inout argument In both cases, assigning a variable of the 'correct' type (e.g. I think there is a valid reason for this. Say there was a valid function as follows: func test3(val: inout Int?) { val = nil}
var c: Int = 2
test3(val: &c)
// runtime error? a non-optional value is nil! This also occurs with This last point is particularly confounding. While the Swift behavior is logical and excusable, the difference in behavior in allowing to pass arguments to C functions taking a pointer vs a pointer-to-pointer is contradictory and makes interacting with C APIs needlessly difficult. |
omor1 (JIRA User) It might be my English or I'm just a little bit confused by the latter example of yours. Are you saying that this is not a bug, but intended? Here is how I'm using the `inout` in my API:
Such constraints are most likely to be IUO's or non-optional. I assume when the example without `inout` is fine, where `!` is converted back to `?`, then the `inout` function should work the same way. |
Comment by Omri Mor (JIRA) Sorry, I wrote that when I was a bit tired and wasn't quite clear. I think that it is intended that one cannot pass a variable declared as I was also pointing out that a somewhat related issue that I encountered. class Type {
var t: TYPE
init(t: TYPE) {
self.t = t
}
deinit {
API_Type_free(&t)
}
} This works fine with the second provider of the interface, but not with the first provider. |
Exactly what this SR is all about. 🙂 I need `Type!` to work with `function(_: inout Type?)`. It's definitely a bug, because it works as expected without `inout`. |
Comment by Omri Mor (JIRA)
Well, I thought the issue I was having was related this this, but it isn't quite the same. I think I did have an issue with passing a |
I didn't read all of that but I think both of your conclusions are correct: this should work, and it should call the Optional overload. Unfortunately we didn't quite get IUO out of the compiler in time for the Swift 3 release, which "explains" the errors. |
Comment by Nathan Vander Wilt (JIRA) Using Swift 4.1 via Xcode 9.3, this issue appears to be fixed with `inout` but still a problem with `UnsafeMutablePointer`: var obj: SomeClass! ; class SomeClass {}
func inoutFunc(_: inout SomeClass?) {}
inoutFunc(&obj) // works
func pointerFunc(_: UnsafeMutablePointer<SomeClass?>) {}
pointerFunc(&obj) // <-- COMPILER ERROR See https://stackoverflow.com/questions/49928472/why-cant-i-pass-an-implicitly-unwrapped-optional-as-an-unsafemutablepointer for a bit more background. This affects usage of several lower-level Apple frameworks. |
This is fixed in recent snapshot builds as a result of #14299 |
@rudkx why was I assigned to the issue? I have not contributed the fix for it. |
Frequently people who resolve issues will assign it back to the originator to verify that the issue is in fact fixed in recent snapshots. I didn't actually put that request in the note when I assigned it, which is also relatively common. Some issue tracking systems to this assignment automatically (i.e. if a person marks something as resolved, it's auto-assigned back to the originator). So if you have a few moments, please try a recent snapshot from master and verify that in fact the issue you reported is fixed. natevw (JIRA User), it would be great if you could also confirm that the issue you're reporting is fixed in snapshots from master. |
Environment
Xcode Version 8.2.1, Swift 3.0.2 or DEVELOPMENT-SNAPSHOT-2016-12-15-a
Additional Detail from JIRA
md5: 315fa6188981bd509a2cc21e0e81feb7
Issue Description:
The text was updated successfully, but these errors were encountered: