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

Closure for Pointing Optional Pointer's Type Check Issue #71580

Open
WindowsMEMZ opened this issue Feb 13, 2024 · 0 comments
Open

Closure for Pointing Optional Pointer's Type Check Issue #71580

WindowsMEMZ opened this issue Feb 13, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@WindowsMEMZ
Copy link
Contributor

Description

When using if to return pointer with optional value in closure (e.g. UnsafeMutablePointer<Int?>), Swift is unable to check the closure return value's type correctly.

Reproduction

var var1: Int? = nil
var var2: Int? = nil
var type = 0
func Update() {
    {// () -> UnsafeMutablePointer<Int?> in
        if type == 0 { 
            return withUnsafeMutablePointer(to: &var1) { $0 } 
        } else { 
            return withUnsafeMutablePointer(to: &var2) { $0 } 
        } 
    }().pointee = 5 // Error: Inout argument could be set to a value with a type other than 'Int'; use a value declared as type 'Int?' instead
}
Update()
print(var1)
print(var2)

Expected behavior

When the closure's return value is not specified, the closure is expected to return UnsafeMutablePointer<Int?> value.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

No response

@WindowsMEMZ WindowsMEMZ added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 13, 2024
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant