Skip to content

mismatched branches in switch expressions with > 2 cases could be diagnosed better #83864

@owenv

Description

@owenv

Description

This program:

let x = switch 0 {
case 1: 1
case 2: false
case 3: ""
case 4: [1]
}

produces:

/Users/ovoorhees/Desktop/foo.swift:4:9: error: branches have mismatching types 'String' and '[Int]'
2 | case 1: 1
3 | case 2: false
4 | case 3: ""
  |         `- error: branches have mismatching types 'String' and '[Int]'
5 | case 4: [1]
6 | }

/Users/ovoorhees/Desktop/foo.swift:3:9: error: branches have mismatching types 'Bool' and '[Int]'
1 | let x = switch 0 {
2 | case 1: 1
3 | case 2: false
  |         `- error: branches have mismatching types 'Bool' and '[Int]'
4 | case 3: ""
5 | case 4: [1]

/Users/ovoorhees/Desktop/foo.swift:2:9: error: branches have mismatching types 'Int' and '[Int]'
1 | let x = switch 0 {
2 | case 1: 1
  |         `- error: branches have mismatching types 'Int' and '[Int]'
3 | case 2: false
4 | case 3: ""

Reproduction

let x = switch 0 {
case 1: 1
case 2: false
case 3: ""
case 4: [1]
}

Expected behavior

Ideally these would all be consolidated into one error covering all the branches, with notes pointing to each branch

Environment

6.2

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions