Skip to content

Bad diagnostic for type mismatch in switch expression when optional promotion is involved #85353

@tridiak

Description

@tridiak

Description

No response

Reproduction

enum SpellFieldDBv4 {
	case spellid
	case name
	case school

}

struct SpellFieldsv4 {
	var spellid : Int
	var name : String
	var school : String?

      func stringVal(idx: SpellFieldDBv4) -> String? {
		return switch idx {
			case .name: name
			case .school: school    (1)
     }

}


(1) Value of optional type 'String?' must be unwrapped to a value of type 'String'
Minimal XC project attached.

Expected behavior

No compiler error for that line.

Environment

Apple Swift version 6.2.1 (swiftlang-6.2.1.4.8 clang-1700.4.4.1)
Target: arm64-apple-macosx26.0

XC 26.1

Additional information

Work around is to drop 'return switch ...'
and use

switch idx {
    case .school: return school
}

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsswitchFeature → statements: 'switch' statementstype checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions