@@ -32,25 +32,37 @@ object Potentials {
32
32
def source : Tree
33
33
}
34
34
35
- /** The object pointed by `this` */
36
- case class ThisRef ()(val source : Tree ) extends Potential {
37
- def show (using Context ): String = " this"
38
-
35
+ sealed trait Refinable extends Potential {
39
36
/** Effects of a method call or a lazy val access
37
+ *
38
+ * The method performs prefix substitution
40
39
*/
41
40
def effectsOf (sym : Symbol )(implicit env : Env ): Effects = trace(" effects of " + sym.show, init, r => Effects .show(r.asInstanceOf )) {
42
41
val cls = sym.owner.asClass
43
- env.summaryOf(cls).effectsOf(sym)
42
+ val effs = env.summaryOf(cls).effectsOf(sym)
43
+ this match
44
+ case _ : ThisRef => effs
45
+ case _ => Effects .asSeenFrom(effs, this )
44
46
}
45
47
46
48
/** Potentials of a field, a method call or a lazy val access
49
+ *
50
+ * The method performs prefix substitution
47
51
*/
48
52
def potentialsOf (sym : Symbol )(implicit env : Env ): Potentials = trace(" potentials of " + sym.show, init, r => Potentials .show(r.asInstanceOf )) {
49
53
val cls = sym.owner.asClass
50
- env.summaryOf(cls).potentialsOf(sym)
54
+ val pots = env.summaryOf(cls).potentialsOf(sym)
55
+ this match
56
+ case _ : ThisRef => pots
57
+ case _ => Potentials .asSeenFrom(pots, this )
51
58
}
52
59
}
53
60
61
+ /** The object pointed by `this` */
62
+ case class ThisRef ()(val source : Tree ) extends Refinable {
63
+ def show (using Context ): String = " this"
64
+ }
65
+
54
66
/** The object pointed by `C.super.this`, mainly used for override resolution */
55
67
case class SuperRef (pot : Potential , supercls : ClassSymbol )(val source : Tree ) extends Potential {
56
68
override def size : Int = pot.size
@@ -64,30 +76,10 @@ object Potentials {
64
76
* @param classSymbol The concrete class of the object
65
77
* @param outer The potential for `this` of the enclosing class
66
78
*/
67
- case class Warm (classSymbol : ClassSymbol , outer : Potential )(val source : Tree ) extends Potential {
79
+ case class Warm (classSymbol : ClassSymbol , outer : Potential )(val source : Tree ) extends Refinable {
68
80
override def level : Int = 1 + outer.level
69
81
def show (using Context ): String = " Warm[" + classSymbol.show + " , outer = " + outer.show + " ]"
70
82
71
- /** Effects of a method call or a lazy val access
72
- *
73
- * The method performs prefix substitution
74
- */
75
- def effectsOf (sym : Symbol )(implicit env : Env ): Effects = trace(" effects of " + sym.show, init, r => Effects .show(r.asInstanceOf )) {
76
- val cls = sym.owner.asClass
77
- val effs = env.summaryOf(cls).effectsOf(sym)
78
- Effects .asSeenFrom(effs, this )
79
- }
80
-
81
- /** Potentials of a field, a method call or a lazy val access
82
- *
83
- * The method performs prefix substitution
84
- */
85
- def potentialsOf (sym : Symbol )(implicit env : Env ): Potentials = trace(" potentials of " + sym.show, init, r => Potentials .show(r.asInstanceOf )) {
86
- val cls = sym.owner.asClass
87
- val pots = env.summaryOf(cls).potentialsOf(sym)
88
- Potentials .asSeenFrom(pots, this )
89
- }
90
-
91
83
def resolveOuter (cls : ClassSymbol )(implicit env : Env ): Potentials =
92
84
env.resolveOuter(this , cls)
93
85
}
@@ -117,7 +109,7 @@ object Potentials {
117
109
case class Outer (pot : Potential , classSymbol : ClassSymbol )(val source : Tree ) extends Potential {
118
110
// be lenient with size of outer selection, no worry for non-termination
119
111
override def size : Int = pot.size
120
- override def level : Int = pot.size
112
+ override def level : Int = pot.level
121
113
def show (using Context ): String = pot.show + " .outer[" + classSymbol.show + " ]"
122
114
}
123
115
@@ -126,7 +118,7 @@ object Potentials {
126
118
assert(field != NoSymbol )
127
119
128
120
override def size : Int = potential.size + 1
129
- override def level : Int = potential.size
121
+ override def level : Int = potential.level
130
122
def show (using Context ): String = potential.show + " ." + field.name.show
131
123
}
132
124
@@ -135,7 +127,7 @@ object Potentials {
135
127
assert(method != NoSymbol )
136
128
137
129
override def size : Int = potential.size + 1
138
- override def level : Int = potential.size
130
+ override def level : Int = potential.level
139
131
def show (using Context ): String = potential.show + " ." + method.name.show
140
132
}
141
133
@@ -163,20 +155,20 @@ object Potentials {
163
155
extension (pot : Potential ) def toPots : Potentials = Potentials .empty + pot
164
156
165
157
extension (ps : Potentials ) def select (symbol : Symbol , source : Tree )(using Context ): Summary =
166
- ps.foldLeft(Summary .empty) { case ((pots, effs), pot) =>
158
+ ps.foldLeft(Summary .empty) { case (Summary (pots, effs), pot) =>
167
159
// max potential length
168
160
// TODO: it can be specified on a project basis via compiler options
169
161
if (pot.size > 2 )
170
- (pots, effs + Promote (pot)(source) )
162
+ summary + Promote (pot)(pot. source)
171
163
else if (symbol.isConstructor)
172
- (pots + pot, effs + MethodCall (pot, symbol)(source))
164
+ Summary (pots + pot, effs + MethodCall (pot, symbol)(source))
173
165
else if (symbol.isOneOf(Flags .Method | Flags .Lazy ))
174
- (
166
+ Summary (
175
167
pots + MethodReturn (pot, symbol)(source),
176
168
effs + MethodCall (pot, symbol)(source)
177
169
)
178
170
else
179
- (pots + FieldReturn (pot, symbol)(source), effs + FieldAccess (pot, symbol)(source))
171
+ Summary (pots + FieldReturn (pot, symbol)(source), effs + FieldAccess (pot, symbol)(source))
180
172
}
181
173
182
174
extension (ps : Potentials ) def promote (source : Tree ): Effects = ps.map(Promote (_)(source))
@@ -205,13 +197,11 @@ object Potentials {
205
197
206
198
case Warm (cls, outer2) =>
207
199
// widening to terminate
208
- val thisValue2 = thisValue match {
209
- case Warm (cls, outer) if outer.level > 2 =>
210
- Warm (cls, Cold ()(outer2.source))(thisValue.source)
211
-
212
- case _ =>
200
+ val thisValue2 =
201
+ if thisValue.level + outer2.level > 4 then
202
+ Cold ()(outer2.source)
203
+ else
213
204
thisValue
214
- }
215
205
216
206
val outer3 = asSeenFrom(outer2, thisValue2)
217
207
Warm (cls, outer3)(pot.source)
0 commit comments