Skip to content

Since 3.8.0-RC1, inline match with inlined values sometimes does not compile as it erases values that it accesses #24588

@rmgk

Description

@rmgk

Compiler version

3.8.0-RC1 and newer (tested 3.8.1-RC1-bin-20251128-fe49539-NIGHTLY)
(compiles in 3.7.4)

Minimized code

Scastie: https://scastie.scala-lang.org/l4JwtPFxQBaKZqMFoAOzaA

object Test {
  inline def dispatch(inline v: Long | String) =
    inline v match
      case str: String => printString(str)
      case long: Long  => printLong(long)

  def printString(s: String) = println(s)
  def printLong(s: Long) = println(s)


  @main
  def run() =
    dispatch(System.nanoTime().toString)
    dispatch(s"${System.nanoTime().toString}")

}

Output

Compiling project (Scala 3.8.1-RC1-bin-20251128-fe49539-NIGHTLY, JVM (17))
[error] ./test.scala:14:5
[error] value str is unusable in method run because it refers to an erased expression
[error] in the selector of an inline match that reduces to
[error] 
[error] {
[error]   erased val $scrutinee2: String =
[error]     _root_.scala.StringContext.apply(["","" : String]*).s(
[error]       [System.nanoTime().toString() : Any]*)
[error]   erased val str: String = $scrutinee2
[error]   {
[error]     Test.printString(str)
[error]   }
[error] }
[error]     dispatch(s"${System.nanoTime().toString}")
[error]     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expectation

For some reason, specifically when passing s"…" to the dispatch inline def directly, the above error happens. All other strings I tried to pass compile as expected.
Works fine if the parameter of the inline def is not inline.

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