Skip to content

Cannot create instance of parameterized trait in macro #9020

@cb372

Description

@cb372

Apologies if I'm doing something stupid but I think this code should work?

Dotty version is 0.25.0-bin-20200516-9450bf2-NIGHTLY.

Minimized code

// Show.scala
trait Show[T] {
  def show(t: T): String
}

object Show {
  inline def deriveWithMacro[T]: Show[T] = ${ impl[T] }

  import quoted._
  def impl[T](using ctx: QuoteContext, tpe: Type[T]): Expr[Show[T]] =
    '{
      new Show[$tpe] {
        def show(t: $tpe): String = "TODO"
      }
    }
}
// Foo.scala
case class Foo(x: String)

object Bar {
  println(Show.deriveWithMacro[Foo].show(Foo("")))
}

Output

[error] 4 |  println(Show.deriveWithMacro[Foo].show(Foo("")))
[error]   |          ^^^^^^^^^^^^^^^^^^^^^^^^^
[error]   |  object creation impossible, since def show(t: T): String is not defined
[error]   |  (Note that T does not match Foo)
[error]   | This location contains code that was inlined from Show.scala:13
[error]   | This location contains code that was inlined from Show.scala:13
[error] one error found
[error] (muService / Compile / compileIncremental) Compilation failed

Expectation

No compile error, prints TODO

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