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