Skip to content

Commit 7641518

Browse files
committed
Streamline Synthesizer initialization
1 parent 4b7ef1e commit 7641518

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,18 +689,19 @@ trait Implicits { self: Typer =>
689689
}
690690
}
691691

692-
lazy val synthesize = Synthesizer(this)
692+
private var synthesizer: Synthesizer | Null = null
693693

694694
/** Find an implicit argument for parameter `formal`.
695695
* Return a failure as a SearchFailureType in the type of the returned tree.
696696
*/
697697
def inferImplicitArg(formal: Type, span: Span)(using Context): Tree =
698-
inferImplicit(formal, EmptyTree, span)(using ctx) match {
698+
inferImplicit(formal, EmptyTree, span)(using ctx) match
699699
case SearchSuccess(arg, _, _) => arg
700700
case fail @ SearchFailure(failed) =>
701701
if fail.isAmbiguous then failed
702-
else synthesize.tryAll(formal, span).orElse(failed)
703-
}
702+
else
703+
if synthesizer == null then synthesizer = Synthesizer(this)
704+
synthesizer.nn.tryAll(formal, span).orElse(failed)
704705

705706
/** Search an implicit argument and report error if not found */
706707
def implicitArgTree(formal: Type, span: Span)(using Context): Tree = {

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import transform.SymUtils._
1515
import transform.TypeUtils._
1616
import transform.SyntheticMembers._
1717
import util.Property
18-
import annotation.tailrec
18+
import annotation.{tailrec, constructorOnly}
1919

2020
/** Synthesize terms for special classes */
21-
class Synthesizer(typer: Typer):
21+
class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
2222
import ast.tpd._
2323

2424
/** Handlers to synthesize implicits for special types */
2525
type SpecialHandler = (Type, Span) => Context ?=> Tree
26-
type SpecialHandlers = List[(ClassSymbol, SpecialHandler)]
26+
private type SpecialHandlers = List[(ClassSymbol, SpecialHandler)]
2727

28-
lazy val synthesizedClassTag: SpecialHandler = (formal, span) =>
28+
val synthesizedClassTag: SpecialHandler = (formal, span) =>
2929
formal.argInfos match
3030
case arg :: Nil =>
3131
fullyDefinedType(arg, "ClassTag argument", span) match
@@ -51,7 +51,7 @@ class Synthesizer(typer: Typer):
5151
/** Synthesize the tree for `'[T]` for an implicit `scala.quoted.Type[T]`.
5252
* `T` is deeply dealiased to avoid references to local type aliases.
5353
*/
54-
lazy val synthesizedTypeTag: SpecialHandler = (formal, span) =>
54+
val synthesizedTypeTag: SpecialHandler = (formal, span) =>
5555
def quotedType(t: Type) =
5656
if StagingContext.level == 0 then
5757
ctx.compilationUnit.needsStaging = true // We will need to run ReifyQuotes
@@ -65,7 +65,7 @@ class Synthesizer(typer: Typer):
6565
EmptyTree
6666
end synthesizedTypeTag
6767

68-
lazy val synthesizedTupleFunction: SpecialHandler = (formal, span) =>
68+
val synthesizedTupleFunction: SpecialHandler = (formal, span) =>
6969
formal match
7070
case AppliedType(_, funArgs @ fun :: tupled :: Nil) =>
7171
def functionTypeEqual(baseFun: Type, actualArgs: List[Type],
@@ -112,7 +112,7 @@ class Synthesizer(typer: Typer):
112112
/** If `formal` is of the form Eql[T, U], try to synthesize an
113113
* `Eql.eqlAny[T, U]` as solution.
114114
*/
115-
lazy val synthesizedEql: SpecialHandler = (formal, span) =>
115+
val synthesizedEql: SpecialHandler = (formal, span) =>
116116

117117
/** Is there an `Eql[T, T]` instance, assuming -strictEquality? */
118118
def hasEq(tp: Type)(using Context): Boolean =
@@ -183,7 +183,7 @@ class Synthesizer(typer: Typer):
183183
/** Creates a tree that will produce a ValueOf instance for the requested type.
184184
* An EmptyTree is returned if materialization fails.
185185
*/
186-
lazy val synthesizedValueOf: SpecialHandler = (formal, span) =>
186+
val synthesizedValueOf: SpecialHandler = (formal, span) =>
187187

188188
def success(t: Tree) =
189189
New(defn.ValueOfClass.typeRef.appliedTo(t.tpe), t :: Nil).withSpan(span)
@@ -364,19 +364,19 @@ class Synthesizer(typer: Typer):
364364
/** An implied instance for a type of the form `Mirror.Product { type MirroredType = T }`
365365
* where `T` is a generic product type or a case object or an enum case.
366366
*/
367-
lazy val synthesizedProductMirror: SpecialHandler = (formal, span) =>
367+
val synthesizedProductMirror: SpecialHandler = (formal, span) =>
368368
makeMirror(productMirror, formal, span)
369369

370370
/** An implied instance for a type of the form `Mirror.Sum { type MirroredType = T }`
371371
* where `T` is a generic sum type.
372372
*/
373-
lazy val synthesizedSumMirror: SpecialHandler = (formal, span) =>
373+
val synthesizedSumMirror: SpecialHandler = (formal, span) =>
374374
makeMirror(sumMirror, formal, span)
375375

376376
/** An implied instance for a type of the form `Mirror { type MirroredType = T }`
377377
* where `T` is a generic sum or product or singleton type.
378378
*/
379-
lazy val synthesizedMirror: SpecialHandler = (formal, span) =>
379+
val synthesizedMirror: SpecialHandler = (formal, span) =>
380380
formal.member(tpnme.MirroredType).info match
381381
case TypeBounds(mirroredType, _) =>
382382
if mirroredType.termSymbol.is(CaseVal)
@@ -387,20 +387,15 @@ class Synthesizer(typer: Typer):
387387
synthesizedSumMirror(formal, span)(using ctx)
388388
case _ => EmptyTree
389389

390-
private var mySpecialHandlers: SpecialHandlers = null
391-
392-
private def specialHandlers(using Context) =
393-
if mySpecialHandlers == null then
394-
mySpecialHandlers = List(
395-
defn.ClassTagClass -> synthesizedClassTag,
396-
defn.QuotedTypeClass -> synthesizedTypeTag,
397-
defn.EqlClass -> synthesizedEql,
398-
defn.TupledFunctionClass -> synthesizedTupleFunction,
399-
defn.ValueOfClass -> synthesizedValueOf,
400-
defn.Mirror_ProductClass -> synthesizedProductMirror,
401-
defn.Mirror_SumClass -> synthesizedSumMirror,
402-
defn.MirrorClass -> synthesizedMirror)
403-
mySpecialHandlers
390+
val specialHandlers = List(
391+
defn.ClassTagClass -> synthesizedClassTag,
392+
defn.QuotedTypeClass -> synthesizedTypeTag,
393+
defn.EqlClass -> synthesizedEql,
394+
defn.TupledFunctionClass -> synthesizedTupleFunction,
395+
defn.ValueOfClass -> synthesizedValueOf,
396+
defn.Mirror_ProductClass -> synthesizedProductMirror,
397+
defn.Mirror_SumClass -> synthesizedSumMirror,
398+
defn.MirrorClass -> synthesizedMirror)
404399

405400
def tryAll(formal: Type, span: Span)(using Context): Tree =
406401
def recur(handlers: SpecialHandlers): Tree = handlers match

0 commit comments

Comments
 (0)