Skip to content

Commit

Permalink
Use concurrent.Map in MacroAnnotation example (#19918)
Browse files Browse the repository at this point in the history
This is much safer if users copy-paste the example blindly into their
code.

See
https://contributors.scala-lang.org/t/scala-3-macro-annotations-sip-63-discussions/6593/2
  • Loading branch information
nicolasstucki authored Mar 19, 2024
2 parents a9373c8 + c26740c commit fca115a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
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

0 comments on commit fca115a

Please sign in to comment.