Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6955] Pattern matching with generic-typed values can't do implicit casting #49503

Open
hamishknight opened this issue Feb 8, 2018 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-6955
Radar None
Original Reporter @hamishknight
Type Bug
Environment

Swift version 4.1-dev (LLVM b1f1b1f5b8, Clang d8b11579e8, Swift a455db6)
Target: x86_64-apple-darwin17.3.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: aecd7e6fe6ed6d3cd2c53f25c945b2e6

Issue Description:

The following fails to compile:

enum E {
  case a
}

func foo<T>(_ t: T) {
  switch t {
  case E.a: // error: Enum case 'a' is not a member of type 'T'
    print("is E.a")
  default:
    print("isn't E.a")
  }
}

However it does compile if the value being switched over is an existential:

enum E {
  case a
}

func foo(_ t: Any) {
  switch t {
  case E.a:
    print("is E.a")
  default:
    print("isn't E.a")
  }
}

Unless there's a good reason not to allow this, IMO the logic in TypeCheckPattern.cpp should check for archetypes as well as existentials.

@hamishknight
Copy link
Collaborator Author

This is especially non-obvious in places where users can't see the generic placeholder, for example this compiles:

enum E : Error {
  case a
}

var e: Error = E.a
switch e {
case E.a:
  print("is E.a")
default:
  print("isn't E.a")
}

but this doesn't:

extension Error {
  func foo() {
    switch self {
    case E.a: // error: Enum case 'a' is not a member of type 'Self'
      print("is E.a")
    default:
      print("isn't E.a")
    }
  }
}

var e: Error = E.a
e.foo()

@belkadan
Copy link
Contributor

belkadan commented Feb 8, 2018

Makes sense to me. @jckarter, any opinions?

@jckarter
Copy link
Member

jckarter commented Feb 8, 2018

I agree this should work.

@hamishknight
Copy link
Collaborator Author

I've opened a PR here: #14496 @jckarter would you be available to review it?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants