Skip to content

Variance does not play well with pattern matching #22993

Open
@TomasMikula

Description

@TomasMikula

Compiler version

3.6.4

(Works in 3.3.5) It does not, in fact, work in 3.3.5. Scastie just did not report any error.

Minimized code

sealed trait Foo[+T]
class Bar[+T] extends Foo[T]

def downcast[T](foo: Foo[T]): Bar[T] =
  foo match
    case b: Bar[t] => 
      summon[t <:< T]
      b

(snippet has been editted to make Bar covariant)
https://scastie.scala-lang.org/EYEO0tDKSNqUJd4ypwRnKA
https://scastie.scala-lang.org/emJhbNmrQd2WYQEdjBzwow

Output

-- [E172] Type Error: test.scala:7:21 ------------------------------------------
7 |      summon[t <:< T]
  |                     ^
  |                     Cannot prove that t <:< T.
-- [E007] Type Mismatch Error: test.scala:8:6 ----------------------------------
8 |      b
  |      ^
  |      Found:    (b : Foo[T] & Bar[t])
  |      Required: Bar[T]
  |
  | longer explanation available when compiling with `-explain`
2 errors found

Expectation

Should typecheck, as it does with Scala 3.3.5.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions