`knownDirectSubclasses` in macros depending on use site object order

521 views
Skip to first unread message

Hanns Holger Rutz

unread,
Dec 10, 2013, 4:28:39 AM12/10/13
to scala-internals
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi there,

I have a question regarding SI-7046 "won't fix". I am not sure this is
the same issue. I have the following problem with macros and
`knownDirectSubclasses`:

// built.sbt
scalaVersion := "2.11.0-M7"

resolvers += Resolver.sonatypeRepo("snapshots")

libraryDependencies += "org.scala-lang" % "scala-reflect" %
scalaVersion.value

addCompilerPlugin("org.scala-lang.plugins" % "macro-paradise" %
"2.0.0-SNAPSHOT" cross CrossVersion.full)

scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature")

// src/main/scala/de/sciss/macrotest
package de.sciss.macrotest

import scala.reflect.macros.Context
import language.experimental.macros

object Format {
def apply[A](): Unit = macro applyImpl[A]

def applyImpl[A: c.WeakTypeTag](c: Context)(): c.Expr[Unit] = {
import c.universe._
val aTpeW = c.weakTypeOf[A]
val aClazz = aTpeW.typeSymbol.asClass
val subs = aClazz.knownDirectSubclasses
println(subs.mkString("--- sub classes:\n '", "'\n '",
"'\n---"))
c.Expr[Unit](EmptyTree)
}
}

// src/test/scala/de/sciss/macrotest
package de.sciss.macrotest

object Baz {
Format[Foo]()
}

sealed trait Foo
case class Bar() extends Foo

Running `sbt test:compile`, this shows that when compiling the body of
`Baz`, there are no known sub classes of `Foo`. If I change the order to

package de.sciss.macrotest

sealed trait Foo
case class Bar() extends Foo

object Baz {
Format[Foo]()
}

Then it correctly prints type `Bar`.

So my questions are: is this SI-7046? will this behaviour be fix? Or:
is there a work around _for the design of the macro_ (not requiring
hoops on the use site)?

Thanks, .hh.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJSpt7HAAoJEKZFmaPaYk6QWl0P/jGxJRoxMk9p9phqNdyrv5x8
hkCKGAqBWojf601UWVJGV4MHD7xivE1kV3cnwJwn7rPYpZ1kjYZrfvU+OVHYQojZ
T6Aznw2Q9Re11xPXnjAUKbDT1/wmw6kLG9GR23oiok45EH/2pig38oqPGBOfTtlq
TiVBSKSmc639sScwQ1ZCUMAPuGejkQdP6WO0NKDwl//9faZhfdIaqioG1UCpU8xn
4i9NTvKv1fv6EjXdkIawRRh+W/B4LRaU9egVkmPjcAX4wcjat/ZveOUimODmgtcb
N/jdAxjo0WRsZriA5JWIMtL586kr/M07rQlQ3JaPukoejH8ErjwAj4zfeUx1bvaG
jM5UAtaIgLD5sBF9vw9Zr7WZuJxAwYD7pv7xOBVHnkcZoxsTwGqvQ5+m21pthjKa
XvnPvA6KxQUdSTQcYsmEGfo58CnhsADPN1fNIniYWMeoMHURmb1grwzC83LCEYNu
2AxiyWXFkLbOhcb/J3Ca9G+l6oz8ahkz8VbyicVeQiFhP/cY50/avjd1V5gFde1S
vxCaLqAvMVSQWM61GHWvkNNyVXnCcxZQr89kF4pQ2Bb1AkWHlwbmbmLG8HmwHdyV
Azc4nGg4Da3AtXZOWxvC9WRbtuqKqD/pXfFYuuYNl5zfofUbEyRwGgIN86ZgZPOG
MuIg9w2JwrUW7LdHQKXI
=AnBW
-----END PGP SIGNATURE-----

Hanns Holger Rutz

unread,
Dec 10, 2013, 4:39:47 AM12/10/13
to scala-internals
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

For easier cloning

https://github.com/Sciss/MacroKnownSubclassesTest

On 10/12/2013 10:28, Hanns Holger Rutz wrote:
> Hi there,
>
> I have a question regarding SI-7046 "won't fix". I am not sure this
> is the same issue. I have the following problem with macros and
> `knownDirectSubclasses`:
...


-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJSpuFiAAoJEKZFmaPaYk6Q7OMP/RQcVRP6IZIXc/fVf0R0VBkO
DhRLV9BxMsurlsy29H9kbln96lvwQl0SEIBS1w1ZxHKVP1f5m21GQeNxDZsWfaR0
jfelTr7UQjd6F3QDgsXAi7zhm2YwmrRmHHexHm6e0tah+TsoGUL3KLrCbM0R42gq
4V8FcoPwWQNqQoOj+t0wnz5AOAh5h6TFAXNeKtMtOVYp6P2QadYeSkeJy+6Cp8Rt
3YYpmL89eNbCCXvAyNjghTC/2qfHzsGGW/4lQyRXz5y3FoO6MZNBeABgcRDIOlyr
OAVjXynxYRmKMP5AwyMEs/2u/m0y8yXzqp39AfeCIcRKb3xW4UfA+hpp6mulH7kb
NdLX9soWLLmcXvQ5Xi8EnRgcPkw5v5GTX1QlGi1tjqUjDJjZWGqFFtHmRoLFll8k
NN3G4hLVvUBzasQ2TkQanws7qxb/T1uHtvpn1txVOQkYLkmQHo/xLjE8cNBZg9SW
vP8MZPWHFsmTLSu7KvNwcK2lAW4wziJL8pPWTcl6mN7RThVfvp0Kf4kUvVFp5om/
1sBD50WhTR1ZdF0jh5XdVVO0WdVZrlJdNRASK/odDqMUraguZwTwOYJkY4FxDBOs
s3jL0p/mVJwUN8AUBEjA3N030YpBimeCaHEE768MGjxz8Ld9LPWemtA1nCG/jj0u
pW3Rh/1GBNezz4OtZzpE
=Fp86
-----END PGP SIGNATURE-----

Eugene Burmako

unread,
Dec 10, 2013, 9:58:20 AM12/10/13
to <scala-internals@googlegroups.com>
Yes, this is SI-7046/SI-7588. 

To the best of my knowledge there's no reliable workaround at the moment. You could try to traverse c.enclosingUnit and force yet incomplete symbols on traits, classes and modules (that would load them into the knownDirectSubclasses list), but that's not going to work well if direct subclasses are not top-level and that also can lead to spurious cyclic reference errors.



--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hanns Holger Rutz

unread,
Dec 10, 2013, 10:16:43 AM12/10/13
to scala-i...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ok, that's a real pity. A thought the whole point of blackbox macros
was that the compiler _knows_ the macro's return type, so couldn't
they made (perhaps with an annotation?) to execute once the whole type
tree is known?

On 10/12/2013 15:58, Eugene Burmako wrote:
> Yes, this is SI-7046/SI-7588.
>
> To the best of my knowledge there's no reliable workaround at the
> moment. You could try to traverse c.enclosingUnit and force yet
> incomplete symbols on traits, classes and modules (that would load
> them into the knownDirectSubclasses list), but that's not going to
> work well if direct subclasses are not top-level and that also can
> lead to spurious cyclic reference errors.
>
>
> On 10 December 2013 10:28, Hanns Holger Rutz <con...@sciss.de
> -- You received this message because you are subscribed to the
> Google Groups "scala-internals" group. To unsubscribe from this
> group and stop receiving emails from it, send an email to
> scala-interna...@googlegroups.com
> <mailto:scala-internals%2Bunsu...@googlegroups.com>. For more
> options, visit https://groups.google.com/groups/opt_out.
>
>
> -- You received this message because you are subscribed to the
> Google Groups "scala-internals" group. To unsubscribe from this
> group and stop receiving emails from it, send an email to
> scala-interna...@googlegroups.com. For more options,
> visit https://groups.google.com/groups/opt_out.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJSpzBbAAoJEKZFmaPaYk6QaQMP/R8ih4klUoR+0hPt5IOBv165
IVD/aZdN0FFYcwVGTmpfDfc+n5YPvpKzad6CuJVOJCXgMr4IB3WA2+bZ3nT4Cb+k
D2jsVyONZ6Qde+peSvEbRuo9XAtL6HyBOev1b88MxUwugdZ0CverqyV+YNGkb3+p
At63xJt7x1rH5wSc3eRg8JunQOM/KDUn7yOYijeEwzqY32HVdvgO7fu6qfljW8cR
AUGZt2XP+5MSvZX2zciCEgCS18RLezjykZm+h5m55vNZ/jw+vhQIelY14J6IK7Fi
cEj7PN7iIXvio1ZseEqKB4u03IF4wpIPWpC722VPUkaewsRJrBoHvp/01ZAqHdiB
WcovcPodj7xvtS+mJ/jASS1QBfmHJ/0Lpe1pey/k2hg12F/aKnBOWt9CLEkazqaa
C+6SnMqBSm5OzUTSYYd+4NiTBbXlBZ/rIHoA0WJ/AdoL6VHhfY3rAUuUaTX3Fm28
t/Hn0wY0GIJwSyDnVl2E8VyAuitZtbdDy35sCov79p6NjUGC3E07lLOLDkkjtpBL
ex8HFSdVz8zeJgzR1WipumgUyQNhkyU+zoEX/Th3FsAdvprx/jIp/mNptFqlyBtQ
48uAdgmvHlsoHcahKdKuQAu3i3Ucqic31SHsry/ZTclzGKvaIOfJI82wejZUiOzw
93CmIxB/vO+U7SYhcTM0
=D4p3
-----END PGP SIGNATURE-----

Adriaan Moors

unread,
Dec 10, 2013, 10:30:16 AM12/10/13
to scala-i...@googlegroups.com
In principle that's possible, but it goes against the way the type checker is designed (it knows how to type check every tree as the need arises; in principle, we go top down, but as you ask for symbols' infos, it may need to jump ahead). knownDirectSubclasses relies on a full top-down pass having been completed before you call it.

Technically, we could provide a onTypecheckCompilationUnit that you could use, but this isn't currently planned for the macro api (well, I'll let Eugene comment on that, but I'm not aware of any plans here -- in any case, 2.11 is in ramp down)

Hanns Holger Rutz

unread,
Dec 10, 2013, 10:39:32 AM12/10/13
to scala-i...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ok, just wondering _if_ it was possible. I think being able to
synthesise serializers, type providers or whatever based on a sealed
trait hierarchy would be nice.

best, .h.h.
> -- You received this message because you are subscribed to the
> Google Groups "scala-internals" group. To unsubscribe from this
> group and stop receiving emails from it, send an email to
> scala-interna...@googlegroups.com. For more options,
> visit https://groups.google.com/groups/opt_out.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJSpzW0AAoJEKZFmaPaYk6QKwcP/j4zZerACMxu5gpmflZLidSc
p4PPdk3aRUYrKsw1jdEs48POMSFWbgir+IvG4eMykDSIgdJF0sa/w5JoxnsHRTFv
j5u166Tp/vx8Y9h0UUe33euPe0onjJ/Bq0NlIKPDurIdtqodv5lsQUs1j7ujkxIt
bX4WaZ+smS4tFf5UKS5B0miKdUPYY+FMMl9NCQ0PhWslyXGS67Me1oMZ0+g/X9AE
JBa/fUFv8RIRsnaeKdQCTCbqgdfiuRzG8DUoLWg1pO7tOPGsCIn7xGCrDwD1f+CL
RYVC4mik6qSVb+16hQ/q2iI90+bi6qNoen/VhZGK5p7247c0Xib2aus/mAh2S+F0
35PjVHZbC0abITY3Z04WnuGcLfqzvPSJieSj3p+jXUaLhQ+ecWy9kTDJidj8qbIx
2ZAaELnm14pP9mUkFUekA6pglkszbhaTg8nJ2jFwp+7OJmx7UwnNiEEdfdeJlsv3
cQx6K0jp5GFhluwy5pKhwlEwuvXsol44HlorXkzyMD5KHLCKlYmqh4GIHaD7xmgj
3TbjQUuuZ4RKYZA9em3ftHiIPPtaT7qcu9mF2rtwg8JKTImYGwfFtRVo1mmaAn+u
VyKT6NsUnOr60DHPRibo0x5kHFCSzoNSolJywm2NdHhXkglaLD+9zU7OSAkUBUWW
YUM/T5L92WjARPS+Tnj+
=t61T
-----END PGP SIGNATURE-----

Adriaan Moors

unread,
Dec 10, 2013, 10:43:35 AM12/10/13
to scala-i...@googlegroups.com
Agreed that it would be nice to make this just work.
It's possible to do this right now, if you're careful about the order in which you call these methods. 
(This is why macros/reflection are experimental.)

Making an "asynchronous" macro api is a bit harder.

Eugene Burmako

unread,
Dec 10, 2013, 10:45:05 AM12/10/13
to <scala-internals@googlegroups.com>
Yeah that'd definitely be nice. Currently it's not possible to achieve this in a robust way, but that's definitely on my to-do list.
Reply all
Reply to author
Forward
0 new messages