Skip to content

typed patterns get more precise types in scalac than dotty #3208

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

Closed
allanrenucci opened this issue Sep 28, 2017 · 4 comments · Fixed by #8413
Closed

typed patterns get more precise types in scalac than dotty #3208

allanrenucci opened this issue Sep 28, 2017 · 4 comments · Fixed by #8413

Comments

@allanrenucci
Copy link
Contributor

The following code snippet fails to compile

class Test {
  trait E
  trait Marker

  def test(es: List[E]): List[E] = es.collect { case e: Marker => e }
}
-- Error: tests/allan/Test.scala:5:69 ------------------------------------------
5 |  def test(es: List[E]): List[E] = es.collect { case e: Marker => e }
  |                                                                     ^
  |Cannot construct a collection of type That
  |
  |where:    That is a type variable with constraint <: scala.collection.immutable.List[Test.this.E]
  | with elements of type Test.this.Marker based on a collection of type scala.collection.immutable.List[Test.this.E].
@smarter
Copy link
Member

smarter commented Sep 28, 2017

It works if you replace case e: Marker by case e: E with Marker. In scalac, e gets the type E with Marker in the original code, but in dotty it only gets the type Marker, I don't know what the intended/specified behavior is, but it seems that the scalac behavior is not completely consistent: https://users.scala-lang.org/t/type-inference-in-pattern-matching/1708

@smarter
Copy link
Member

smarter commented Sep 28, 2017

Had a quick look at the spec and I couldn't find anything substantiating the current scalac behavior, in particular http://www.scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html#typed-patterns says:

A typed pattern x:T consists of a pattern variable x and a type pattern T. The type of x is the type pattern T, [...]

@odersky Are you OK with replicating the behavior of scalac here? Here's a simple example illustrating the difference:

trait X
trait Y

object Test {
  def foo(x: X) = x match {
    case y: Y =>
      val b: X = y // compiles with scalac, type mismatch with dotty
  }
}

@smarter smarter changed the title Cannot construct a collection of type That typed patterns get more precise types in scalac than dotty Sep 28, 2017
@odersky
Copy link
Contributor

odersky commented Sep 29, 2017

Pattern matching desperately needs a spec. It would be good if someone could start working on the issue. I think the scalac behavior on the test cases makes sense, though.

@allanrenucci
Copy link
Contributor Author

allanrenucci commented Oct 4, 2017

Here is another example that does not compile with Dotty, I found in the community build:

trait MyError { this: Throwable =>
  ...
}


try ...
catch {
  case e: MyError =>
    throw e
}

The self-type is not required to compile with scalac but it was in the example

allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Oct 5, 2017
allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Oct 9, 2017
cheeseng pushed a commit to cheeseng/scalatest that referenced this issue Oct 11, 2017
allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Oct 17, 2017
allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Nov 1, 2017
@odersky odersky self-assigned this Jan 27, 2018
allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Apr 18, 2018
allanrenucci added a commit to dotty-staging/scalatest that referenced this issue Apr 19, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Sep 6, 2018
For a pattern like `x @ P`, x get the type of the scrutinee and pattern.
E.g:
```
(x: X) match case { y: Y => ... }
```
Above, `y` type to `X & Y`.
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Sep 6, 2018
For a pattern like `x @ P`, x get the type of the scrutinee and pattern.
E.g:
```
(x: X) match case { y: Y => ... }
```
Above, `y` type to `X & Y`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment