scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

annotation macros in the presentation compiler #10902

Closed fommil closed 6 years ago

fommil commented 6 years ago

This is moving https://github.com/scalamacros/paradise/issues/105 to here, now that paradise will be part of scalac

TL;DR macro annotations don't seem to work very well in the PC. I have never been able to minimise it, and a lot of the time I'm hearing this through users who complain about it.

It would be good to get to the bottom of it, and it's probably either a one line fix or impossible. Simulacrum causes red squigglies. I don't know what in particular about it is doing this, because Monocle apparently works better.

Personally, I have taken to using compiler plugins for my own things instead of annotation macros, but of course I can't convince the ecosystem to swap over.

First step: write a minimal failing test.

adriaanm commented 6 years ago

It's not impossible, but extremely hard. I have no idea how to do it. I also want to reiterate that, while macro annotations are part of scalac, they are experimental and will change in 3.0. The main goal of the coming changes is to actually make it feasible to implement better tooling for them.

I think we need to make macro annotations no more powerful than what's needed to implement case class synthesis: they can contribute trees, but they exist in the next "stage", in the sense that synthetic trees cannot refer to other synthetic trees. The remaining challenges is how to deal with references from user code to synthetic trees (if you generalise case class synthesis, how do you know there's going to be an apply method on the companion object until you have run the @case macro on the class? How do you know to trigger that macro when type checking the user code that needs the apply method?). Separate compilation. etc etc

fommil commented 6 years ago

All of these things are solved by https://gitlab.com/fommil/scalaz-deriving/blob/master/deriving-plugin/src/main/scala/scalaz/plugins/deriving/AnnotationPlugin.scala with the tradeoff that the annotation is not name checked. The general principle is to generate members, pre typer, that forward to blackbox macros. A possible improvement would be to do a poor man's known subtypes and add annotations to the entire ADT. A separate intellij plugin is required, but it is usually easy to write such a thing and I'm sure some bytecode analysis on the intellij side could probably remove even that requirement.

But this ticket is about supporting Scala 2 code that exists today and will most likely still exist in 5 to 10 years time. I suspect the reason why paradise doesn't work in the PC is because nobody's ever really looked into it.

I can put together pcplod tests using simulacrum if that would help... but I have no idea where to begin with a fully standalone test.

fommil commented 6 years ago

I put together a very small test https://github.com/mpilquist/simulacrum/pull/106

run with sbt coreJVM/testOnly *Presentation*