Skip to content

Unable to reuse the type expression of an inline match inside a branch #12474

Closed
@julienrf

Description

@julienrf

Compiler version

3.0.0

Minimized code

package bugreport

import scala.compiletime.erasedValue

trait Show[A]:
  def show(a: A): String

inline def showTuple[Types]: Show[Types] =
  inline erasedValue[Types] match
    case _: (head *: tail) =>
      val instance =
        new Show[head *: tail]:
          def show(tuple: head *: tail): String = "dummy"
      instance.asInstanceOf[Show[Types]]

@main def run() =
  showTuple[(Int, Int)]

Output

[error] -- Error: bugreport.scala:26:11 
[error] 26 |  showTuple[(Int, Int)]
[error]    |  ^^^^^^^^^^^^^^^^^^^^^
[error]    |object creation impossible, since def show(a: A): String in trait Show in package derivation is not defined 
[error]    |(Note that
[error]    | parameter A in def show(a: A): String in trait Show in package derivation does not match
[error]    | parameter (Int, Int) in def show(tuple: (Int, Int)): String in anonymous class Object with derivation.Show[head *: tail] {...}
[error]    | )
[error]    | This location contains code that was inlined from bugreport.scala:12

Expectation

It compiles.

Note that if I introduce a type alias within the branch and then I always refer to that type alias it works:

package derivation

import scala.compiletime.erasedValue

trait Show[A]:
  def show(a: A): String

inline def showTuple[Types]: Show[Types] =
  inline erasedValue[Types] match
    case _: (head *: tail) =>
      type X = head *: tail // Introduce a type alias
      val instance =
        new Show[X]:
          def show(tuple: X): String = "dummy"
      instance.asInstanceOf[Show[Types]]

@main def run() =
  showTuple[(Int, Int)] // OK

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