Skip to content

if expression with opaque types and availability.  #69812

Open
@zienag

Description

@zienag

Description
SE-0360 introduced a possibility to return different types under #available condition. But I found that it doesn't work with if expressions introduced in SE-0380

Steps to reproduce

protocol P {}
struct S1: P {}
struct S2: P {}

// Compiles fine
func f1() -> some P {
  if #available(macOS 99, *) {
    return S1()
  } else {
    return S2()
  }
}

// error: Branches have mismatching types 'S1' and 'S2'
func f2() -> some P {
  if #available(macOS 99, *) {
    S1()
  } else {
    S2()
  }
}

Expected behavior
f2 compiles and behaves exactly as f1
Environment
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Xcode 15.0

Metadata

Metadata

Assignees

Labels

#availableFeature → statements: Availability conditionavailabilityThe @available attribute and availability checking in generalbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsif elseFeature → statements: 'if' statementsopaque result typesFeature → types → opaque types: opaque result typesopaque typesFeature → types: opaque typesswift 6.2type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions