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

Chaining Match Types fails at value level #13855

Closed
ThijsBroersen opened this issue Oct 31, 2021 · 3 comments · Fixed by #14354
Closed

Chaining Match Types fails at value level #13855

ThijsBroersen opened this issue Oct 31, 2021 · 3 comments · Fixed by #14354

Comments

@ThijsBroersen
Copy link

ThijsBroersen commented Oct 31, 2021

Compiler version

3.1.0

Minimized code

type A[X] = X match
  case Int => Int
  case _   => B[X]

def a[X](x: X): A[X] = x match
  case v: Int => v
  case _      => b(x)

type B[X] = X match
  case String => String

def b[X](x: X): B[X] = x match
  case v: String => v

Output

-- [E007] Type Mismatch Error: foo.scala:6:17 ----------------------------------
6 |  case v: Int => v
  |                 ^
  |               Found:    (v : X & Int)
  |               Required: A[X]
  |
  |               Note: a match type could not be fully reduced:
  |
  |                 trying to reduce  A[X]
  |                 failed since selector  X
  |                 does not match  case Int => Int
  |                 and cannot be shown to be disjoint from it either.
  |                 Therefore, reduction cannot advance to the remaining case
  |
  |                   case _ => B[X]

longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: foo.scala:7:18 ----------------------------------
7 |  case _      => b(x)
  |                 ^^^^
  |                 Found:    B[X]
  |                 Required: A[X]
  |
  |                 Note: a match type could not be fully reduced:
  |
  |                   trying to reduce  B[X]
  |                   failed since selector  X
  |                   does not match  case String => String
  |                   and cannot be shown to be disjoint from it either.

longer explanation available when compiling with `-explain`
2 errors found

Expectation

Should compile?
The type calculation works fine for chained Match Types. Only when adding the value level implementation it fails.

This works:

type A[X] = X match
  case Int => Int
  case _   => B[X]

def a[X](x: X): A[X] = (x match
  case v: Int => v
  case _      => b(x)
).asInstanceOf[A[X]]

type B[X] = X match
  case String => String

def b[X](x: X): B[X] = x match
  case v: String => v
@bishabosha
Copy link
Member

bishabosha commented Nov 1, 2021

minimised to

type A[X] = X match
  case Int => Int
  case _   => Int

def a[X](x: X): A[X] = x match
  case x: Int => x
  case _      => 0

the way to fix this minimised case and the example is to replace _ with Any:

type A[X] = X match
  case Int => Int
  case Any => Int

def a[X](x: X): A[X] = x match
  case x: Int => x
  case _: Any => 0

@bishabosha
Copy link
Member

I think this basically waits on #12261

@ThijsBroersen
Copy link
Author

minimised to

type A[X] = X match
  case Int => Int
  case _   => Int

def a[X](x: X): A[X] = x match
  case x: Int => x
  case _      => 0

the way to fix this minimised case and the example is to replace _ with Any:

type A[X] = X match
  case Int => Int
  case Any => Int

def a[X](x: X): A[X] = x match
  case x: Int => x
  case _: Any => 0

@bishabosha thanks! Makes completely sense!

OlivierBlanvillain added a commit to dotty-staging/dotty that referenced this issue Jan 26, 2022
bishabosha added a commit that referenced this issue Jan 26, 2022
Xavientois pushed a commit to Xavientois/dotty that referenced this issue Feb 2, 2022
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants