From c26740cb0fe06d61e0e5aeeec1efca9f98d0fe67 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 11 Mar 2024 13:57:26 +0100 Subject: [PATCH] Use `concurrent.Map` in `MacroAnnotation` example This is much safer if users copy-paste the example blindly into their code. --- library/src/scala/annotation/MacroAnnotation.scala | 10 +++++----- tests/run-macros/annot-memo/Macro_1.scala | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/library/src/scala/annotation/MacroAnnotation.scala b/library/src/scala/annotation/MacroAnnotation.scala index a76289ecea61..02e9470f06fd 100644 --- a/library/src/scala/annotation/MacroAnnotation.scala +++ b/library/src/scala/annotation/MacroAnnotation.scala @@ -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] = @@ -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 @@ -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, diff --git a/tests/run-macros/annot-memo/Macro_1.scala b/tests/run-macros/annot-memo/Macro_1.scala index c18260cc0f95..cd990e1d6cce 100644 --- a/tests/run-macros/annot-memo/Macro_1.scala +++ b/tests/run-macros/annot-memo/Macro_1.scala @@ -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: @@ -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