Skip to content

Commit 0c45807

Browse files
committed
Heap may serve as global cache for warm objects
This is safe because heap is monotonistic and fields abstractions are immutable
1 parent c17bbfe commit 0c45807

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

compiler/src/dotty/tools/dotc/transform/init/Checker.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class Checker extends MiniPhase {
5050
val tpl = tree.rhs.asInstanceOf[Template]
5151
val thisRef = ThisRef(cls)
5252
val obj = Objekt(cls, fields = mutable.Map.empty, outers = mutable.Map.empty)
53-
given Heap = Heap.empty
5453
given Promoted = Promoted.empty
5554
given Trace = Trace.empty
5655
heap.update(thisRef, obj)

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Semantic {
129129
type Heap = Heap.Heap
130130

131131
import Heap._
132-
def heap(using h: Heap): Heap = h
132+
val heap: Heap = Heap.empty
133133

134134
object Promoted {
135135
/** Values that have been safely promoted */
@@ -205,7 +205,7 @@ class Semantic {
205205
}
206206

207207
/** The state that threads through the interpreter */
208-
type Contextual[T] = (Heap, Context, Trace, Promoted) ?=> T
208+
type Contextual[T] = (Context, Trace, Promoted) ?=> T
209209

210210
// ----- Error Handling -----------------------------------
211211

@@ -268,7 +268,13 @@ class Semantic {
268268
if obj.fields.contains(target) then
269269
Result(obj.fields(target), Nil)
270270
else if addr.isInstanceOf[Warm] then
271-
if target.hasSource then
271+
if target.is(Flags.ParamAccessor) then
272+
// possible for trait parameters
273+
// see tests/init/neg/trait2.scala
274+
//
275+
// return `Hot` here, errors are reported in checking `ThisRef`
276+
Result(Hot, Nil)
277+
else if target.hasSource then
272278
val rhs = target.defTree.asInstanceOf[ValOrDefDef].rhs
273279
eval(rhs, addr, target.owner.asClass, cacheResult = true)
274280
else
@@ -310,7 +316,7 @@ class Semantic {
310316
val cls = target.owner.enclosingClass.asClass
311317
if target.isPrimaryConstructor then
312318
val tpl = cls.defTree.asInstanceOf[TypeDef].rhs.asInstanceOf[Template]
313-
eval(tpl, addr, cls, cacheResult = true)(using heap, ctx, trace.add(tpl), promoted)
319+
eval(tpl, addr, cls, cacheResult = true)(using ctx, trace.add(tpl), promoted)
314320
else
315321
val rhs = target.defTree.asInstanceOf[ValOrDefDef].rhs
316322
eval(rhs, addr, cls, cacheResult = true)
@@ -608,11 +614,11 @@ class Semantic {
608614
case Select(supert: Super, _) =>
609615
val SuperType(thisTp, superTp) = supert.tpe
610616
val thisValue2 = resolveThis(thisTp.classSymbol.asClass, thisV, klass, ref)
611-
Result(thisValue2, errors).call(ref.symbol, superTp, expr)(using heap, ctx, trace2)
617+
Result(thisValue2, errors).call(ref.symbol, superTp, expr)(using ctx, trace2)
612618

613619
case Select(qual, _) =>
614620
val res = eval(qual, thisV, klass) ++ errors
615-
res.call(ref.symbol, superType = NoType, source = expr)(using heap, ctx, trace2)
621+
res.call(ref.symbol, superType = NoType, source = expr)(using ctx, trace2)
616622

617623
case id: Ident =>
618624
id.tpe match
@@ -624,7 +630,7 @@ class Semantic {
624630
thisValue2.call(id.symbol, superType = NoType, expr, needResolve = false)
625631
case TermRef(prefix, _) =>
626632
val res = cases(prefix, thisV, klass, id) ++ errors
627-
res.call(id.symbol, superType = NoType, source = expr)(using heap, ctx, trace2)
633+
res.call(id.symbol, superType = NoType, source = expr)(using ctx, trace2)
628634

629635
case Select(qualifier, name) =>
630636
eval(qualifier, thisV, klass).select(expr.symbol, expr)
@@ -816,7 +822,7 @@ class Semantic {
816822

817823
// follow constructor
818824
if cls.hasSource then
819-
val res2 = thisV.call(ctor, superType = NoType, source)(using heap, ctx, trace.add(source))
825+
val res2 = thisV.call(ctor, superType = NoType, source)(using ctx, trace.add(source))
820826
errorBuffer ++= res2.errors
821827

822828
// parents

tests/init/neg/trait1.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
abstract class A(x: Int) {
2+
def foo(): Unit
3+
foo()
4+
}
5+
6+
trait B(val y: Int) // error
7+
8+
class C extends A(10) with B(20) {
9+
def foo(): Unit = println(y)
10+
}

tests/init/neg/trait2.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
abstract class A(x: Int) {
2+
def foo(): Unit
3+
foo()
4+
}
5+
6+
trait B(val y: Int) // error
7+
8+
class D {
9+
class C extends A(10) with B(20) {
10+
def foo(): Unit = println(y)
11+
}
12+
13+
val c = new C
14+
}

0 commit comments

Comments
 (0)