Skip to content

Metaprogramming + existential type fails to compile #15357

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
japgolly opened this issue Jun 1, 2022 · 4 comments
Closed

Metaprogramming + existential type fails to compile #15357

japgolly opened this issue Jun 1, 2022 · 4 comments
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices

Comments

@japgolly
Copy link
Contributor

japgolly commented Jun 1, 2022

Compiler version

3.1.2

Minimized code

import scala.quoted.*

// This works no problem
object UniversalTypeParam {
  trait T[A] { def a: A }

  inline def macroDef[A](inline t: T[A]): A =
    ${ macroBody[A]('t) }

  def macroBody[A](t: Expr[T[A]])(using Quotes, Type[A]): Expr[A] = {
    import quotes.reflect.*
    '{ $t.a }
  }
}

// This morally the same as above, but doesn't compile
object ExistentialTypeParam {
  trait T { type A; def a: A }
  type TA[X] = T { type A = X }

  inline def macroDef(inline t: T): t.A =
    ${ macroBody[t.A]('t) }

  def macroBody[A](t: Expr[TA[A]])(using Quotes, Type[A]): Expr[A] = {
    import quotes.reflect.*
    '{ $t.a }
  }
}

Output

[error] 22 |    ${ macroBody[t.A]('t) }
[error]    |                      ^
[error]    |                      access to parameter t from wrong staging level:
[error]    |                       - the definition is at level 0,
[error]    |                       - but the access is at level -1.
[error] one error found

Expectation

Should compile.

@japgolly japgolly added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 1, 2022
@japgolly
Copy link
Contributor Author

japgolly commented Jun 1, 2022

Workaround:

   inline def macroDef(inline t: T): t.A =
-    ${ macroBody[t.A]('t) }
+    workaround(t)
+
+  inline def workaround[A](inline t: TA[A]): A =
+    ${ macroBody[A]('t) }

@julienrf julienrf added area:metaprogramming and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 2, 2022
@nicolasstucki
Copy link
Contributor

It might be related to #13376

@nicolasstucki nicolasstucki added area:metaprogramming:quotes Issues related to quotes and splices and removed area:metaprogramming labels Jun 2, 2022
@nicolasstucki
Copy link
Contributor

In

inline def macroDef(inline t: T): t.A =  ${ macroBody[t.A]('t) }

I notice the use of t.A. It seems strange that t.A in considered a valid path because there are no guarantees that t is a stable path once it is inlined.

@nicolasstucki
Copy link
Contributor

Now it fails telling the user that inline t is not an immutable path. This is the correct failure.

-- [E083] Type Error: t/Test.scala:21:36 ---------------------------------------
21 |  inline def macroDef(inline t: T): t.A =
   |                                    ^
   |(t : ExistentialTypeParam.T) is not a valid type prefix, since it is not an immutable path
   |
   | longer explanation available when compiling with `-explain`
-- [E083] Type Error: t/Test.scala:22:17 ---------------------------------------
22 |    ${ macroBody[t.A]('t) }
   |                 ^
   |(t : ExistentialTypeParam.T) is not a valid type prefix, since it is not an immutable path
   |
   | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: t/Test.scala:22:22 ------------------------------
22 |    ${ macroBody[t.A]('t) }
   |                      ^
   |     Found:    (t : ExistentialTypeParam.T)
   |     Required: ExistentialTypeParam.T{type A = ExistentialTypeParam.T#A}
   |
   | longer explanation available when compiling with `-explain`

This also suggests the alternative workaround, which does compile.

-  inline def macroDef(inline t: T): t.A =
+  inline def macroDef(t: T): t.A =

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices
Projects
None yet
Development

No branches or pull requests

3 participants