Skip to content

Enhance type checking for pattern match related to opaque types #11669

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

Open
liufengyun opened this issue Mar 9, 2021 · 0 comments
Open

Enhance type checking for pattern match related to opaque types #11669

liufengyun opened this issue Mar 9, 2021 · 0 comments

Comments

@liufengyun
Copy link
Contributor

liufengyun commented Mar 9, 2021

In #11526, we harden type checking for object and enum value patterns. However, we have to adapt the following test due to an interaction with opaque types:

File: tests/run/option-extract.scala

enum Option[+A]:
  case Some(x: A)
  case None

  opaque type ExtractResult[B] = (=> B) => B

  def extract[B](f: A => B): ExtractResult[B] =
-   def result(default: => B): B = this match
+   def result(default: => B): B = (this: Option[A]) match
      case None => default
      case Some(elem) => f(elem)
    result

  extension [B](er: ExtractResult[B])
    def orElse(default: => B): B = er(default)
end Option

The problem with the code above is that the new typing rule requires None to be a subtype of the scrutinee type { type ExtractResult = ... } & Option[A], which is not the case.

The changes in bf3b50a seem to fix the problem. However, it's not clear that it's the right thing to do.

As a compromise, we don't strip the opaque refinement in widening ThisType, and change the test code in tests/run/option-extract.scala instead as it's rare code pattern.

We need to revisit this when we have a better idea about whether it is always safe to strip the opaque refinements in widening ThisType.

@liufengyun liufengyun changed the title Enhance type checking for pattern match Enhance type checking for pattern match related to opaque types Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant