Skip to content

Match types fail to infer correct type #19936

Closed
@lbialy

Description

@lbialy

Compiler version

This fails differently on 3.3.3 and 3.4.0.

Minimized code

//> using scala 3.3.3 // 3.4.0
import scala.compiletime.ops.int.S

trait Slider[N <: Int & Singleton, A]:
  type Out <: Tuple = N match
    case 0    => EmptyTuple
    case S[n] => Tuple.Append[A, Slider[n, A]#Out]

extension [A](v: Vector[A])
  def slidingN(n: Int & Singleton): Iterator[Slider[n.type, A]#Out] =
    v.sliding(n).map { block =>
      block.foldLeft[Tuple](EmptyTuple)((acc, a) => acc :* a).asInstanceOf[Slider[n.type, A]#Out]
    }

@main def test: Unit =
  val it: Iterator[(Int, Int)] = (1 to 20).toVector.slidingN(2) // this compiles on 3.4.0 but fails to compile on 3.3.3
  it.map(_._1.toString).foreach(println)

  val it2: Iterator[Int] = (1 to 20).toVector.slidingN(2).map(_._1) // this fails to compile on both

Output

For 3.3.3:

[error] ./test.scala:15:61
[error] Match type reduction failed since selector Int
[error] matches none of the cases
[error]
[error]     case EmptyTuple => Slider[(1 : Int), Int]#Out *: EmptyTuple
[error]     case x *: xs => x *: Tuple.Append[xs, Slider[(1 : Int), Int]#Out]
[error]   val it: Iterator[(Int, Int)] = (1 to 20).toVector.slidingN(2)
[error]                                                             ^
[error] ./test.scala:18:65
[error] Match type reduction failed since selector Int
[error] matches none of the cases
[error]
[error]     case EmptyTuple => Slider[(1 : Int), Int]#Out *: EmptyTuple
[error]     case x *: xs => x *: Tuple.Append[xs, Slider[(1 : Int), Int]#Out]
[error]   val it2: Iterator[Int] = (1 to 20).toVector.slidingN(2).map(_._1)
[error]                                                                 ^
Error compiling project (Scala 3.3.3, JVM (17))

For 3.4.0:

[error] ./test.scala:15:38
[error] Match type reduction failed since selector Int
[error] matches none of the cases
[error]
[error]     case EmptyTuple => Slider[(1 : Int), Int]#Out *: EmptyTuple
[error]     case x *: xs => x *: Tuple.Append[xs, Slider[(1 : Int), Int]#Out]
[error]   (1 to 20).toVector.slidingN(2).map(_._1)
[error]                                      ^^^^
Error compiling project (Scala 3.4.0, JVM (17))
Compilation failed

Expectation

I expect this code to infer return type as Iterator[(Int, Int)] correctly in both use cases.

Metadata

Metadata

Labels

area:match-typesitype:bugitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)regressionThis worked in a previous version but doesn't anymore

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions