scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Expected Warning not happening when using higher-kinded types

Open matheussbernardo opened this issue 1 year ago • 2 comments

Compiler version

scalac -version $ Scala compiler version 3.4.0 -- Copyright 2002-2024, LAMP/EPFL

Minimized example

trait T[A] {
  def v: A
}

trait T2[F[_], A] {
  def v: A
}

object doesntWarn {
  val x = new T2[Option, Int | String] {
    def v: Int | String = "asdf"
  }

  val y: T2[Option, Int] = x match {
    case z: T2[Option, Int] =>
      z
  }
}

object warns {
  val x = new T[Int | String] {
    def v: Int | String = "asdf"
  }

  val y: T[Int] = x match {
    case z: T[Int] =>
      z
  }
}

Output Error/Warning message

-- [E092] Pattern Match Unchecked Warning: Main.scala:26:9 ---------------------
26 |    case z: T[Int] =>
   |         ^
   |the type test for T[Int] cannot be checked at runtime because its type arguments can't be determined from T[Int | String]
   |
   | longer explanation available when compiling with `-explain`
1 warning found

Why this Error/Warning was not helpful

Hello! I am wondering why only the second example warns. Is higher kinded-types messing with something here? Is this expected?

Thanks in advance!

matheussbernardo avatar May 15 '24 07:05 matheussbernardo

Hello! I am wondering why only the second example warns. Is higher kinded-types messing with something here? Is this expected?

I don't think this is expected. We should investigate why this happens, thanks for reporting @matheussbernardo

Gedochao avatar May 17 '24 12:05 Gedochao

same problem here with 3.4.1 i think

....collectFirst{ case c@XResolved[JobTitle.type](_, _) => c}
[warn]    |                                              ^
[warn]    |the type test for ....XResolved[X.JobTitle.type]
[warn]    |  cannot be checked at runtime because its type arguments can't be determined from ....XResolved[X]
[warn]    |
[warn]    | longer explanation available when compiling with `-explain`
final case class XResolved[A <: X](
    column: A,
    id: String
)
case object JobTitle extends X

funny enough it still appears to work

visox avatar May 22 '24 10:05 visox