Skip to content

Sema: if case _ as casting pattern doesn't work despite being implemented already #60808

@simanerush

Description

@simanerush

Steps To Reproduce
For example, this code snippet

protocol T {}
struct A: T {}

func test_switch<T>(arr: T) {
  if case _ as A = arr {}
}

should work fine. However, it gives the following error:

error: cannot convert value of type 'T' to specified type 'A'
  if case _ as A = arr {}
                   ^~~
                       as! A

and emits non-relevant diagnostics.

Expected behavior
Even though this works by binding the result of the cast to a constant (if case let t as A = arr {}), the case specified above should also work.

Environment (please fill out the following information)

  • OS: macOS 12.5.1
  • Swift version: 5.8-dev

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)compilerThe Swift compiler itselfpattern matchingFeature: pattern matchingpatternsFeature: patternsswift 5.8type 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