Skip to content

[5.8] Switch expression causes build failure because of type inference change #63979

@keith

Description

@keith

With Swift 5.7 this code compiles fine:

class Foo {
    private var action2: () -> Void = {}
    func bar() {
        let action = { [weak self] in
            switch true {
            case true:
                self?.bar()
            case false:
                self?.bar()
            }
        }

        self.action2 = action
    }
}

With Swift 5.8 it fails to compile because action is inferred as () -> ()? instead of () -> Void. I assume this is because of switch changing to be an expression, and now the optional result of self?.bar() is used as the return value, but since it breaks the build I figured I would report it anyways in case that's unexpected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsregressionswift 5.9switchFeature → statements: 'switch' statementstype checkerArea → compiler: Semantic analysistype inferenceFeature: type inferenceunexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions