Skip to content

Commit 8bbac9b

Browse files
committed
Generate lazy vals for parameterless classes in deriving
1 parent 16e2066 commit 8bbac9b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

compiler/src/dotty/tools/dotc/transform/CacheAliasImplicits.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ class CacheAliasImplicits extends MiniPhase with IdentityDenotTransformer { this
7171
case _ => false
7272
}
7373
}
74-
if (isCached) {
75-
sym.copySymDenotation(
76-
initFlags = sym.flags &~ Method | Lazy,
77-
info = sym.info.widenExpr)
78-
.installAfter(thisPhase)
79-
cpy.ValDef(tree)(tree.name, tree.tpt, tree.rhs)
80-
}
74+
if isCached then
75+
if true then
76+
println(i"NOT CACHING $tree")
77+
tree
78+
else
79+
sym.copySymDenotation(
80+
initFlags = sym.flags &~ Method | Lazy,
81+
info = sym.info.widenExpr)
82+
.installAfter(thisPhase)
83+
cpy.ValDef(tree)(tree.name, tree.tpt, tree.rhs)
8184
else tree
8285
}
8386

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ trait Deriving {
5454
// If we set the Synthetic flag here widenGiven will widen too far and the
5555
// derived instance will have too low a priority to be selected over a freshly
5656
// derived instance at the summoning site.
57+
val flags = if info.isInstanceOf[MethodOrPoly] then Given | Method else Given | Lazy
5758
synthetics +=
58-
newSymbol(ctx.owner, instanceName, Given | Method, info, coord = pos.span)
59+
newSymbol(ctx.owner, instanceName, flags, info, coord = pos.span)
5960
.entered
6061
}
6162

@@ -96,8 +97,7 @@ trait Deriving {
9697
def addInstance(derivedParams: List[TypeSymbol], evidenceParamInfos: List[List[Type]], instanceTypes: List[Type]): Unit = {
9798
val resultType = typeClassType.appliedTo(instanceTypes)
9899
val monoInfo =
99-
if evidenceParamInfos.isEmpty then
100-
if derivedParams.isEmpty then ExprType(resultType) else resultType
100+
if evidenceParamInfos.isEmpty then resultType
101101
else ImplicitMethodType(evidenceParamInfos.map(typeClassType.appliedTo), resultType)
102102
val derivedInfo =
103103
if derivedParams.isEmpty then monoInfo
@@ -295,7 +295,8 @@ trait Deriving {
295295
}
296296

297297
def syntheticDef(sym: Symbol): Tree = inContext(ctx.fresh.setOwner(sym).setNewScope) {
298-
tpd.polyDefDef(sym.asTerm, typeclassInstance(sym))
298+
if sym.is(Method) then tpd.polyDefDef(sym.asTerm, typeclassInstance(sym))
299+
else tpd.ValDef(sym.asTerm, typeclassInstance(sym)(Nil)(Nil))
299300
}
300301

301302
synthetics.map(syntheticDef).toList

0 commit comments

Comments
 (0)