Skip to content

Switch expression results cannot be assigned to self. #66489

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
JessyCatterwaul opened this issue Jun 9, 2023 · 1 comment
Closed

Switch expression results cannot be assigned to self. #66489

JessyCatterwaul opened this issue Jun 9, 2023 · 1 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

Comments

@JessyCatterwaul
Copy link

Compiles:

func ƒ<Value>(value: Value) -> Value {
  switch value {
  case let value: value
  }
}

Does not compile:

extension Bool { // Type doesn't matter, though `Never` will crash the compiler.
  init(value: Self) {
    self = switch value {
    case let value: value // This will compile with a warning, but assign the argument, not the bound value.
    case let v: v // Cannot find 'v' in scope
    }
  }
}

Wrapping in a closure is a solution:

extension Bool {
  init(value: Self) {
    self = { switch value {
    case let value: value
    } } ()
  }
}
@JessyCatterwaul JessyCatterwaul added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 9, 2023
@hamishknight
Copy link
Contributor

Duplicate of #66412, filed #66490 to track the Never crash

@hamishknight hamishknight closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2023
@AnthonyLatsis AnthonyLatsis added duplicate Resolution: Duplicates another issue compiler The Swift compiler itself switch Feature → statements: 'switch' statements expressions Feature: expressions unexpected error Bug: Unexpected error type checker Area → compiler: Semantic analysis assignments Feature → expressions: assignments swift 5.9 and removed triage needed This issue needs more specific labels labels Jun 9, 2023
@AnthonyLatsis AnthonyLatsis removed the duplicate Resolution: Duplicates another issue label 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

No branches or pull requests

3 participants