Skip to content

Commit e6856b0

Browse files
committed
Allow memo in traits
1 parent 0620928 commit e6856b0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import transform.SymUtils._
1616
import Contexts.Context
1717
import Names.{Name, TermName}
1818
import NameKinds.{InlineAccessorName, InlineBinderName, InlineScrutineeName, MemoCacheName}
19+
import NameOps._
1920
import ProtoTypes.selectionProto
2021
import SymDenotations.SymDenotation
2122
import Inferencing.fullyDefinedType
@@ -407,7 +408,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
407408

408409
/** The expansion of `memo(op)` where `op: T` is:
409410
*
410-
* { if (memo$N == null) memo$N = op; $memo.asInstanceOf[T] }
411+
* { if (memo$N == null) memo$N_=(op); $memo.asInstanceOf[T] }
411412
*
412413
* This creates as a side effect a memo cache symbol $memo$N` of type `T | Null`.
413414
* TODO: Restrict this to non-null types, once nullability checking is in.
@@ -425,10 +426,17 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
425426
else Synthetic | Mutable | Private | Local,
426427
info = OrType(argType, defn.NullType),
427428
coord = call.span)
429+
val memoSetter = ctx.newSymbol(
430+
owner = cacheOwner,
431+
name = memoVar.name.setterName,
432+
flags = memoVar.flags | Method | Accessor,
433+
info = MethodType(argType :: Nil, defn.UnitType),
434+
coord = call.span
435+
)
428436
val memoRef = ref(memoVar).withSpan(call.span)
429437
val cond = If(
430438
memoRef.select(defn.Any_==).appliedTo(Literal(Constant(null))),
431-
memoRef.becomes(callValueArgss.head.head),
439+
ref(memoSetter).withSpan(call.span).becomes(callValueArgss.head.head),
432440
Literal(Constant(())))
433441
val expr = memoRef.cast(argType)
434442
Block(cond :: Nil, expr)

0 commit comments

Comments
 (0)