Skip to content

if/switch expressions aren't allowed with an optional chain assignment #65906

@hamishknight

Description

@hamishknight

The following gives an error:

struct S {
  var x: Int
}
func foo(_ s: S?) {
  var s = s
  s?.x = if .random() { 0 } else { 1 }
  // error: 'if' may only be used as expression in return, throw, or as the source of an assignment 
}

But we ought to allow it.

rdar://109305454

Metadata

Metadata

Assignees

Labels

assignmentsFeature → expressions: assignmentsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsif elseFeature → statements: 'if' statementsoptional chainingFeature → expressions: optional chainingswift 5.9type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions