Skip to content

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

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
julienrf opened this issue May 14, 2021 · 1 comment · Fixed by #12847
Closed

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

julienrf opened this issue May 14, 2021 · 1 comment · Fixed by #12847

Comments

@julienrf
Copy link
Contributor

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
@julienrf
Copy link
Contributor Author

It seems that in some cases I can reuse the type expression head *: tail. The bug appears when I use it within the definition of an anonymous class (like in new Show[head *: tail]:).

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.

2 participants