-
Notifications
You must be signed in to change notification settings - Fork 1.1k
How to determine if a lambda closes over state in a macro? #19349
-
I want to determine at compile-time if a lambda (closure) closes over state. Is there way to do this with a macro?
I know how to do this in a compiler plugin h/t Scala Native.
case fn @ Closure(env, target, _) => if env.nonEmpty then report.error( s"Closing over local state of ${env.map(_.symbol.show).mkString(", ")} in function transformed to CFuncPtr results in undefined behaviour.", fn.srcPos )
However, the definition of Closure
in the compiler AST differs from the Quotes
AST, specifically the lack of env
.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
The closure environment is not yet computed when we expand macros. This is something we compute in a later phase.
It might be hard or inconsistent if we compute the closure environment before or during the expansion of a macro. Macros can change the captured sets of closures outside their scope.
It might be simpler to provide a method that lists all local variables that are used but not defined in a specific tree.
Beta Was this translation helpful? Give feedback.