@@ -35,6 +35,10 @@ object Annotations {
35
35
def argumentConstant (i : Int )(implicit ctx : Context ): Option [Constant ] =
36
36
for (ConstantType (c) <- argument(i) map (_.tpe)) yield c
37
37
38
+ /** The tree evaluaton is in progress. */
39
+ def isEvaluating : Boolean = false
40
+
41
+ /** The tree evaluation has finished. */
38
42
def isEvaluated : Boolean = true
39
43
40
44
def ensureCompleted (implicit ctx : Context ): Unit = tree
@@ -72,6 +76,7 @@ object Annotations {
72
76
}
73
77
myTree.asInstanceOf [Tree ]
74
78
79
+ override def isEvaluating : Boolean = myTree == null
75
80
override def isEvaluated : Boolean = myTree.isInstanceOf [Tree ]
76
81
}
77
82
@@ -87,24 +92,31 @@ object Annotations {
87
92
override def ensureCompleted (implicit ctx : Context ): Unit = ()
88
93
}
89
94
90
- case class ConcreteBodyAnnotation (body : Tree ) extends BodyAnnotation {
95
+ class ConcreteBodyAnnotation (body : Tree ) extends BodyAnnotation {
91
96
def tree (implicit ctx : Context ): Tree = body
92
97
}
93
98
94
- case class LazyBodyAnnotation ( private var bodyExpr : Context => Tree ) extends BodyAnnotation {
95
- // TODO: Make `bodyExpr` an IFT once #6865 os in bootstrap
96
- private var evaluated = false
97
- private var myBody : Tree = _
98
- def tree ( implicit ctx : Context ) : Tree = {
99
- if (evaluated) assert(myBody != null )
100
- else {
101
- evaluated = true
102
- myBody = bodyExpr (ctx)
103
- bodyExpr = null
99
+ abstract class LazyBodyAnnotation extends BodyAnnotation {
100
+ // Copy-pasted from LazyAnnotation to avoid having to turn it into a trait
101
+ protected var myTree : Tree | ( Context => Tree )
102
+ def tree ( using ctx : Context ) : Tree =
103
+ assert(myTree != null )
104
+ myTree match {
105
+ case treeFn : ( Context => Tree ) @ unchecked =>
106
+ myTree = null
107
+ myTree = treeFn (ctx)
108
+ case _ =>
104
109
}
105
- myBody
106
- }
107
- override def isEvaluated : Boolean = evaluated
110
+ myTree.asInstanceOf [Tree ]
111
+
112
+ override def isEvaluating : Boolean = myTree == null
113
+ override def isEvaluated : Boolean = myTree.isInstanceOf [Tree ]
114
+ }
115
+
116
+ object LazyBodyAnnotation {
117
+ def apply (bodyFn : Context ?=> Tree ): LazyBodyAnnotation =
118
+ new LazyBodyAnnotation :
119
+ protected var myTree : Tree | (Context => Tree ) = ctx => bodyFn(using ctx)
108
120
}
109
121
110
122
object Annotation {
0 commit comments