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

[5.8] In Swift 5.8, if a property of a optional value is assigned within a closure, it will cause the type inference of the closure's return value to change from void to optional void. #64027

Open
CrazyFanFan opened this issue Mar 2, 2023 · 4 comments
Labels
assignments Feature → expressions: assignments bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself expressions Feature: expressions optional chaining Feature → expressions: optional chaining swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@CrazyFanFan
Copy link
Contributor

In Swift 5.8, if a property of a optional value is assigned within a closure, it will cause the type inference of the closure's return value to change from void to optional void.

Description

Run the following code

class Foo {
    var value: Int = 1
}

var inner: Foo?
func foo() {
    let action = {
        inner?.value = 1
    }

    print(type(of: action))
}

foo()

Output

() -> Optional<()>

Environment

  • Xcode: Version 14.3 beta 2 (14E5207e)
  • Swift

swift-driver version: 1.75.1 Apple Swift version 5.8 (swiftlang-5.8.0.119.11 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0

@CrazyFanFan CrazyFanFan added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 2, 2023
@JessyCatterwaul
Copy link

JessyCatterwaul commented Mar 2, 2023

This is not specific to closures or 5.8, or a bug.

var value: Optional = 1
type(of: value? = 1) // Void?.self

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis accepts invalid Bug: Accepts invalid type inference Feature: type inference expressions Feature: expressions swift 5.9 optional chaining Feature → expressions: optional chaining and removed triage needed This issue needs more specific labels labels Mar 2, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Mar 2, 2023

How is value? = 1 legal in the first place?

Edit: Haha I never knew Swift had built-in optional assignments

cc @xedin

@AnthonyLatsis AnthonyLatsis removed the accepts invalid Bug: Accepts invalid label Mar 2, 2023
@xedin
Copy link
Contributor

xedin commented Mar 2, 2023

Optional projects l-valueness of it's object out that's how ? chains work.

@AnthonyLatsis AnthonyLatsis added unexpected behavior Bug: Unexpected behavior or incorrect output assignments Feature → expressions: assignments labels Mar 3, 2023
@AnthonyLatsis
Copy link
Collaborator

Optional projects l-valueness of it's object out that's how ? chains work.

I understand, somehow I just never realized (or forgot) that the postfix ? restriction was limited to loads, and not writes as well.


Back to the issue. This has been the case since around Swift 5.0 for the original example due the implicit return statement (i.e., since the feature was introduced), and even older versions if we’re talking about explicit returns. It has to do with the way we model these expressions in the AST. On one hand, the optionality comes with a bonus:

let didAssign = (w?.x?.y = z) != nil

But I agree that { inner?.value = 1 } is generally expected to be () -> Void.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assignments Feature → expressions: assignments bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself expressions Feature: expressions optional chaining Feature → expressions: optional chaining swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

No branches or pull requests

4 participants