Skip to content

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

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

Closed
kr1sc opened this issue Jun 7, 2023 · 2 comments · Fixed by #65893
Closed
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 swift 5.9 switch Feature → statements: 'switch' statements type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error

Comments

@kr1sc
Copy link

kr1sc commented Jun 7, 2023

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
@kr1sc kr1sc added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 7, 2023
@hamishknight
Copy link
Contributor

This is a known issue, and is noted in the Xcode 15 release notes:

An if or switch expression that introduces a new variable may produce a spurious “cannot find ‘’ in scope” error when that if or switch is used in an assignment. (109192116)

Was recently fixed by #65893

@AnthonyLatsis AnthonyLatsis added swift 5.9 switch Feature → statements: 'switch' statements expressions Feature: expressions compiler The Swift compiler itself unexpected error Bug: Unexpected error type checker Area → compiler: Semantic analysis assignments Feature → expressions: assignments and removed triage needed This issue needs more specific labels labels Jun 8, 2023
@hamishknight
Copy link
Contributor

This should be fixed in Xcode 15 beta 3

@AnthonyLatsis AnthonyLatsis marked this as a duplicate of #66489 May 6, 2025
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 swift 5.9 switch Feature → statements: 'switch' statements type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants