Skip to content

[5.9] Switch expression won't compile when assigning value to already defined variable. #66412

@kr1sc

Description

@kr1sc

Description

Function foo1will not compile but function foo2 will. Compilator error is Cannot find 'val' in scope. The difference is only with declaration and initialization of bar variable.

func foo1() {
    let value: Int? = nil
    
    var bar: Int = 1
    bar = switch value {
    case let val?: val
    default: 0
    }
}

func foo2() {
    let value: Int? = nil
    
    var bar: Int = switch value {
    case let val?: val
    default: 0
    }
}

Steps to reproduce

Try to compile above code in Xcode 15.0 beta.

Expected behavior

Both functions compile.

Environment

  • Swift compiler version info
swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
Target: arm64-apple-macosx13.0
  • Xcode version info
Version 15.0 beta (15A5160n)
  • Deployment target:
iOS 17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    assignmentsFeature → expressions: assignmentsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsswift 5.9switchFeature → statements: 'switch' statementstype checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions