scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.79k stars 1.04k forks source link

`inline match` not reducing on case alternatives #12073

Closed japgolly closed 3 years ago

japgolly commented 3 years ago

Similar to #12072. In this case, inline match expressions that work normally, fail to compile in overridden methods.

Compiler version

Both:

Minimized code

transparent inline def f: Option[String] =
  None

object Override {

  trait Trait { def s: String }

  object OK extends Trait {
    override transparent inline def s: String =
      inline f match {
        case Some("x") => "x"
        case Some("y") => "y"
        case None      => "-"
      }
  }

  // error: Some("y") | None
  object KO_1 extends Trait {
    override transparent inline def s: String =
      inline f match {
        case Some("x") => "x"
        case Some("y")
           | None      => "0"
      }
  }

  // error: no None
  object KO_2 extends Trait {
    override transparent inline def s: String =
      inline f match {
        case Some("x") => "x"
        case Some("y") => "y"
        case Some(z)   => "z"
        // case None      => "0"
      }
  }
}

object NonOverride {

  transparent inline def ok_1: String =
    inline f match {
      case Some("x") => "x"
      case Some("y") => "y"
      case None      => "-"
    }

  // ok: Some("y") | None
  transparent inline def ok_2: String =
    inline f match {
      case Some("x") => "x"
      case Some("y")
         | None      => "0"
    }

  // ok: no None
  transparent inline def ok_3: String =
    inline f match {
      case Some("x") => "x"
      case Some("y") => "y"
      case Some(z)   => "z"
      // case None      => "0"
    }

}

Output

[error] -- Error: /home/golly/scala3bug/sbt/src/main/scala/BUG.scala:20:13 -------------
[error] 20 |      inline f match {
[error]    |             ^
[error]    |       cannot reduce inline match with
[error]    |        scrutinee:  None : None.type
[error]    |        patterns :  case Some.unapply[String]("x"):Some[String]
[error]    |                    case (Some.unapply[String]("y"):Some[String]) | None
[error]    | This location contains code that was inlined from BUG.scala:20
[error] -- Error: /home/golly/scala3bug/sbt/src/main/scala/BUG.scala:30:13 -------------
[error] 30 |      inline f match {
[error]    |             ^
[error]    |             cannot reduce inline match with
[error]    |              scrutinee:  None : None.type
[error]    |              patterns :  case Some.unapply[String]("x"):Some[String]
[error]    |                          case Some.unapply[String]("y"):Some[String]
[error]    |                          case Some.unapply[String](z @ _):Some[String]
[error]    | This location contains code that was inlined from BUG.scala:30
[error] two errors found

Expectation

It should compile.

nicolasstucki commented 3 years ago

Minimized

inline def f: Unit =
  inline 1 match
    case 1 | 2 =>

def test = f
5 |def test = f
  |           ^
  |           cannot reduce inline match with
  |            scrutinee:  1 : (1 : Int)
  |            patterns :  case 1 | 2
  | This location contains code that was inlined from Foo.scala:2
nicolasstucki commented 3 years ago

Workaround: Avoid alternatives in inline matches.

-    case 1 | 2 =>
+    case 1 =>
+    case 2 =>