Skip to content

inline match not reducing on case alternatives #12073

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
japgolly opened this issue Apr 13, 2021 · 2 comments · Fixed by #12137
Closed

inline match not reducing on case alternatives #12073

japgolly opened this issue Apr 13, 2021 · 2 comments · Fixed by #12137

Comments

@japgolly
Copy link
Contributor

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

Compiler version

Both:

  • 3.0.0-RC1
  • 3.0.1-RC1-bin-20210411-b44cafa-NIGHTLY

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
Copy link
Contributor

nicolasstucki commented Apr 13, 2021

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 nicolasstucki changed the title inline match on Option have problems with None when overriding a method inline match not reducing on case alternatives Apr 13, 2021
@nicolasstucki
Copy link
Contributor

Workaround: Avoid alternatives in inline matches.

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

odersky added a commit to dotty-staging/dotty that referenced this issue Apr 18, 2021
michelou pushed a commit to michelou/scala3 that referenced this issue Apr 20, 2021
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants