@@ -15,17 +15,17 @@ import transform.SymUtils._
15
15
import transform .TypeUtils ._
16
16
import transform .SyntheticMembers ._
17
17
import util .Property
18
- import annotation .tailrec
18
+ import annotation .{ tailrec , constructorOnly }
19
19
20
20
/** Synthesize terms for special classes */
21
- class Synthesizer (typer : Typer ):
21
+ class Synthesizer (typer : Typer )( using @ constructorOnly c : Context ) :
22
22
import ast .tpd ._
23
23
24
24
/** Handlers to synthesize implicits for special types */
25
25
type SpecialHandler = (Type , Span ) => Context ?=> Tree
26
- type SpecialHandlers = List [(ClassSymbol , SpecialHandler )]
26
+ private type SpecialHandlers = List [(ClassSymbol , SpecialHandler )]
27
27
28
- lazy val synthesizedClassTag : SpecialHandler = (formal, span) =>
28
+ val synthesizedClassTag : SpecialHandler = (formal, span) =>
29
29
formal.argInfos match
30
30
case arg :: Nil =>
31
31
fullyDefinedType(arg, " ClassTag argument" , span) match
@@ -51,7 +51,7 @@ class Synthesizer(typer: Typer):
51
51
/** Synthesize the tree for `'[T]` for an implicit `scala.quoted.Type[T]`.
52
52
* `T` is deeply dealiased to avoid references to local type aliases.
53
53
*/
54
- lazy val synthesizedTypeTag : SpecialHandler = (formal, span) =>
54
+ val synthesizedTypeTag : SpecialHandler = (formal, span) =>
55
55
def quotedType (t : Type ) =
56
56
if StagingContext .level == 0 then
57
57
ctx.compilationUnit.needsStaging = true // We will need to run ReifyQuotes
@@ -65,7 +65,7 @@ class Synthesizer(typer: Typer):
65
65
EmptyTree
66
66
end synthesizedTypeTag
67
67
68
- lazy val synthesizedTupleFunction : SpecialHandler = (formal, span) =>
68
+ val synthesizedTupleFunction : SpecialHandler = (formal, span) =>
69
69
formal match
70
70
case AppliedType (_, funArgs @ fun :: tupled :: Nil ) =>
71
71
def functionTypeEqual (baseFun : Type , actualArgs : List [Type ],
@@ -112,7 +112,7 @@ class Synthesizer(typer: Typer):
112
112
/** If `formal` is of the form Eql[T, U], try to synthesize an
113
113
* `Eql.eqlAny[T, U]` as solution.
114
114
*/
115
- lazy val synthesizedEql : SpecialHandler = (formal, span) =>
115
+ val synthesizedEql : SpecialHandler = (formal, span) =>
116
116
117
117
/** Is there an `Eql[T, T]` instance, assuming -strictEquality? */
118
118
def hasEq (tp : Type )(using Context ): Boolean =
@@ -183,7 +183,7 @@ class Synthesizer(typer: Typer):
183
183
/** Creates a tree that will produce a ValueOf instance for the requested type.
184
184
* An EmptyTree is returned if materialization fails.
185
185
*/
186
- lazy val synthesizedValueOf : SpecialHandler = (formal, span) =>
186
+ val synthesizedValueOf : SpecialHandler = (formal, span) =>
187
187
188
188
def success (t : Tree ) =
189
189
New (defn.ValueOfClass .typeRef.appliedTo(t.tpe), t :: Nil ).withSpan(span)
@@ -364,19 +364,19 @@ class Synthesizer(typer: Typer):
364
364
/** An implied instance for a type of the form `Mirror.Product { type MirroredType = T }`
365
365
* where `T` is a generic product type or a case object or an enum case.
366
366
*/
367
- lazy val synthesizedProductMirror : SpecialHandler = (formal, span) =>
367
+ val synthesizedProductMirror : SpecialHandler = (formal, span) =>
368
368
makeMirror(productMirror, formal, span)
369
369
370
370
/** An implied instance for a type of the form `Mirror.Sum { type MirroredType = T }`
371
371
* where `T` is a generic sum type.
372
372
*/
373
- lazy val synthesizedSumMirror : SpecialHandler = (formal, span) =>
373
+ val synthesizedSumMirror : SpecialHandler = (formal, span) =>
374
374
makeMirror(sumMirror, formal, span)
375
375
376
376
/** An implied instance for a type of the form `Mirror { type MirroredType = T }`
377
377
* where `T` is a generic sum or product or singleton type.
378
378
*/
379
- lazy val synthesizedMirror : SpecialHandler = (formal, span) =>
379
+ val synthesizedMirror : SpecialHandler = (formal, span) =>
380
380
formal.member(tpnme.MirroredType ).info match
381
381
case TypeBounds (mirroredType, _) =>
382
382
if mirroredType.termSymbol.is(CaseVal )
@@ -387,20 +387,15 @@ class Synthesizer(typer: Typer):
387
387
synthesizedSumMirror(formal, span)(using ctx)
388
388
case _ => EmptyTree
389
389
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)
404
399
405
400
def tryAll (formal : Type , span : Span )(using Context ): Tree =
406
401
def recur (handlers : SpecialHandlers ): Tree = handlers match
0 commit comments