Skip to content

[SR-12230] Improve diagnostic when enum case payload label doesn't match pattern #54656

@jckarter

Description

@jckarter
Previous ID SR-12230
Radar rdar://problem/59655515
Original Reporter @jckarter
Type Task
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Task
Assignee None
Priority Medium

md5: e162e203353a4b22da300ab68cf02876

Issue Description:

If you have a label mismatch in an enum case pattern like this:

enum Foo {
  case foo(bar: Int)
  case bar
}

func switchFoo(x: Foo) {
  switch x {
  case .foo(bas: let bas):
    print("foo bas \(bas)")
  case .bar:
    print("bar")
  }
}

then you get an error like this:

/Users/jgroff/foo.swift:10:13: error: tuple pattern element label 'bas' must be 'bar'
  case .foo(bas: let bas):
            ^

But in the user's mental model, this isn't really a tuple pattern, they're trying to match against a particular enum case. Having an error with a note referring to the enum case would probably be easier to understand. Something like:

/Users/jgroff/foo.swift:10:13: error: enum type 'Foo' has no case named `foo(bas:)`, did you mean `foo(bar:)`?
  case .foo(bas: let bas):
            ^
/Users/jgroff/foo.swift:2: note: case `foo(bar:)` declared here

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itself

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions