Skip to content

Cannot create instance of parameterized trait in macro #9020

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
cb372 opened this issue May 21, 2020 · 2 comments · Fixed by #9198
Closed

Cannot create instance of parameterized trait in macro #9020

cb372 opened this issue May 21, 2020 · 2 comments · Fixed by #9198

Comments

@cb372
Copy link
Contributor

cb372 commented May 21, 2020

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

@nicolasstucki
Copy link
Contributor

Indeed, it is a bug.

It can worked around it with

    '{
      type X = $tpe
      new Show[X] {
        def show(t: X): String = "TODO"
      }
    }

@nicolasstucki
Copy link
Contributor

It is only the type in the extends clauses that are not properly spliced

  '{
      type X = T
      new Show[X] {
        def show(t: T): String = "TODO"
      }
    }

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jun 18, 2020
@nicolasstucki nicolasstucki linked a pull request Jun 18, 2020 that will close this issue
nicolasstucki added a commit that referenced this issue Jun 18, 2020
Fix #9020: Handle spliced types in parents
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.

3 participants