Skip to content

Overriding with an inline method and inline match is unsound #24760

@ch1nq

Description

@ch1nq

Compiler version

3.7.4

3.8.1-RC1-bin-20251215-a227106-NIGHTLY

Minimized code

import scala.compiletime.erasedValue

object Test {
  type MatchType[T] = T match {
    case EmptyTuple => String
    case _          => Int
  }

  trait MyTrait[T] {
    def f: MatchType[T]
  }

  class MyClass[T] extends MyTrait[T] {
    override inline def f: MatchType[T] = inline erasedValue[T] match {
      case _: EmptyTuple => "empty"
      case _             => 42
    }
  }

  @main def main(): Unit = {
    val a: MyTrait[EmptyTuple] = new MyClass[EmptyTuple]
    val str: String = a.f
  }
}

Output

Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
        at Test$.main(overrideInline.scala:22)
        at main.main(overrideInline.scala:20)

Expectation

Should not compile

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions