Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to determine if a lambda closes over state in a macro? #19349

Unanswered
armanbilge asked this question in Metaprogramming
Discussion options

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
 )

https://github.com/scala-native/scala-native/blob/e90d39ead9c79b72d2df1d14ede3b8a8ffac0d94/nscplugin/src/main/scala-3/scala/scalanative/nscplugin/NirGenExpr.scala#L2677-L2682

However, the definition of Closure in the compiler AST differs from the Quotes AST, specifically the lack of env.

https://github.com/lampepfl/dotty/blob/938d405f05e3b47eb18183a6d6330b6324505cdf/compiler/src/dotty/tools/dotc/ast/Trees.scala#L594

https://github.com/lampepfl/dotty/blob/938d405f05e3b47eb18183a6d6330b6324505cdf/library/src/scala/quoted/Quotes.scala#L1390

You must be logged in to vote

Replies: 1 comment

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /