Skip to content

Use concurrent.Map in MacroAnnotation example #19918

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions library/src/scala/annotation/MacroAnnotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait MacroAnnotation extends StaticAnnotation:
* This example shows how to modify a `def` and add a `val` next to it using a macro annotation.
* ```scala
* import scala.quoted.*
* import scala.collection.mutable
* import scala.collection.concurrent
*
* class memoize extends MacroAnnotation:
* def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
Expand All @@ -52,14 +52,14 @@ trait MacroAnnotation extends StaticAnnotation:
* (param.tpt.tpe.asType, tpt.tpe.asType) match
* case ('[t], '[u]) =>
* val cacheName = Symbol.freshName(name + "Cache")
* val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
* val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[concurrent.Map[t, u]], Flags.Private, Symbol.noSymbol)
* val cacheRhs =
* given Quotes = cacheSymbol.asQuotes
* '{ mutable.Map.empty[t, u] }.asTerm
* '{ concurrent.TrieMap.empty[t, u] }.asTerm
* val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
* val newRhs =
* given Quotes = tree.symbol.asQuotes
* val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]
* val cacheRefExpr = Ref(cacheSymbol).asExprOf[concurrent.Map[t, u]]
* val paramRefExpr = Ref(param.symbol).asExprOf[t]
* val rhsExpr = rhsTree.asExprOf[u]
* '{ $cacheRefExpr.getOrElseUpdate($paramRefExpr, $rhsExpr) }.asTerm
Expand All @@ -82,7 +82,7 @@ trait MacroAnnotation extends StaticAnnotation:
* and the macro will modify the definition to create
* ```scala
* val fibCache$macro$1 =
* scala.collection.mutable.Map.empty[Int, Int]
* scala.collection.concurrent.TrieMap.empty[Int, Int]
* def fib(n: Int): Int =
* fibCache$macro$1.getOrElseUpdate(
* n,
Expand Down
8 changes: 4 additions & 4 deletions tests/run-macros/annot-memo/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import scala.annotation.{experimental, MacroAnnotation}
import scala.quoted._
import scala.collection.mutable
import scala.collection.concurrent

@experimental
class memoize extends MacroAnnotation:
Expand All @@ -13,14 +13,14 @@ class memoize extends MacroAnnotation:
(param.tpt.tpe.asType, tpt.tpe.asType) match
case ('[t], '[u]) =>
val cacheName = Symbol.freshName(name + "Cache")
val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[concurrent.Map[t, u]], Flags.Private, Symbol.noSymbol)
val cacheRhs =
given Quotes = cacheSymbol.asQuotes
'{ mutable.Map.empty[t, u] }.asTerm
'{ concurrent.TrieMap.empty[t, u] }.asTerm
val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
val newRhs =
given Quotes = tree.symbol.asQuotes
val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]
val cacheRefExpr = Ref(cacheSymbol).asExprOf[concurrent.Map[t, u]]
val paramRefExpr = Ref(param.symbol).asExprOf[t]
val rhsExpr = rhsTree.asExprOf[u]
'{ $cacheRefExpr.getOrElseUpdate($paramRefExpr, $rhsExpr) }.asTerm
Expand Down