Skip to content

Be able to check -Yretain-trees in macros #17692

@cchantep

Description

@cchantep

Compiler option -Yretain-trees has a drastic impact on compiler plugins capabilities, and also on macros based on dotc.

Being able at least to check whether this is set would be useful, e.g.

def foo(using q: Quotes) = {
  import q.reflect.{ report, CompilationInfo }

  if (!CompilationInfo.retainTrees) {
    report.errorAndAbort("Option -Yretain-trees must be set")
  }

  // ...
}

Draft:

diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
index 3de7f0bfc6..716ef64fb4 100644
--- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
+++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
@@ -78,6 +78,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
 
     object CompilationInfo extends CompilationInfoModule:
       def isWhileTyping: Boolean = !ctx.isAfterTyper
+      def retainTrees: Boolean = ctx.settings.YretainTrees.value
     end CompilationInfo
 
     extension (expr: Expr[Any])
diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala
index a375709844..2f2439d2dd 100644
--- a/library/src/scala/quoted/Quotes.scala
+++ b/library/src/scala/quoted/Quotes.scala
@@ -238,6 +238,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
        *  This will be true when the macro is used in a transparent inline.
        */
       def isWhileTyping: Boolean
+
+      /** Are trees retains for symbol? (see `-Yretain-trees`) */
+      def retainTrees: Boolean
     }
 
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions