-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
TypeRepr of constructor parameters unreliable - dependent on compilation order #19795
Comments
val paramStrings = t.typeSymbol.primaryConstructor.paramSymss.flatten.map(s => t.memberType(s)) With that said, even when passing "-Yretain-trees" compilation option, which should help, I see that the type parameter can still be missing, so this is worth looking into. Minimisation for import scala.quoted.*
object Surface:
def funcImpl[A](using tpe: Type[A], quotes: Quotes): Expr[String] = {
import quotes.reflect.*
val t = TypeRepr.of(using tpe)
val paramStrings = t.typeSymbol.primaryConstructor.paramSymss.flatten
.map(s => s.tree.show)
val output = paramStrings.mkString(",")
println(s"params $output")
Expr(output)
}
inline def func[A]: String = ${ funcImpl[A] } main.scala: object Main {
println(Surface.func[TypeUtils.Wrap])
} TypeUtils.scala object TypeUtils {
trait MyOption[T]
class Wrap(val option: MyOption[Int])
} Running:
After which, running something like (with paths to stdlibs adjusted, and the previous output added onto the clsspath like a build tool would do):
|
Thanks for the advice. This works very well for my use case. I will submit a PR to airframe implementing this. |
… types (#3466) Do not use `.tree` to obtain parameter types, use `typeMember` instead. The code is simpler and more reliable, avoiding issue scala/scala3#19795 (#3417)
After investigating for a bit I am afraid that in general this is not something we can/should fix. The issue shows itself when compiling 2 times. First when compiling all files simultenaously, and second, when recompiling only the macro and main methods, using previous artifacts in the way of incremental compilation. To understand why this is the case we need to follow how the macro is being expanded by the compiler and what the The In summary: |
Thanks for investigation. I have created PR for the library meanwhile using your suggested code and I will try to be extra careful anytime I see |
The
TypeRepr
of class constructor parameters is sometimes wrong. Instead of anAppliedType
sometimes a plainTypeRef
is returned, missing type parameters.Compiler version
3.0.0, 3.3.2, 3.4.1-RC1
Minimized code
https://github.com/OndrejSpanel/surface-sandbox
The repo contains two reproduction methods: default branch contains a minimized repro, branch
surface-repro
contains repro usingaiframe-surface
library, which is how the issue was originaly found.Output
Expectation
Output for
option
should contain correct parameter type including type parameters. same way aslocalOpt
does.Note
The reproduction depends on compilation order. If you uncomment the line containing
TypeUtils.myMacro()
, the issue disappears and the output is as expected:The text was updated successfully, but these errors were encountered: