Skip to content

Commit 4163fd3

Browse files
committed
Fix crash: make sure object fresh before calling init
The problem is that all warm objects are populated with outers and class parameters in Heap.prepare(). If we call `init` on those objects, the only-set-once invariant will be violated and crash.
1 parent 99df102 commit 4163fd3

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ object Semantic {
8080
this
8181
}
8282

83+
def ensureFresh()(using Heap): this.type =
84+
val obj = Objekt(this.klass, fields = Map.empty, outers = Map(this.klass -> this.outer))
85+
heap.update(this, obj)
86+
this
8387

8488
/** Update field value of the abstract object
8589
*
@@ -681,7 +685,7 @@ object Semantic {
681685
Result(Hot, Errors.empty)
682686
else
683687
val outer = Hot
684-
val warm = Warm(klass, outer, ctor, args2)
688+
val warm = Warm(klass, outer, ctor, args2).ensureFresh()
685689
val argInfos2 = args.zip(args2).map { (argInfo, v) => argInfo.copy(value = v) }
686690
val res = warm.callConstructor(ctor, argInfos2, source)
687691
Result(warm, res.errors)
@@ -701,7 +705,7 @@ object Semantic {
701705

702706
val argsWidened = args.map(_.value).widenArgs
703707
val argInfos2 = args.zip(argsWidened).map { (argInfo, v) => argInfo.copy(value = v) }
704-
val warm = Warm(klass, outer, ctor, argsWidened)
708+
val warm = Warm(klass, outer, ctor, argsWidened).ensureFresh()
705709
val res = warm.callConstructor(ctor, argInfos2, source)
706710
Result(warm, res.errors)
707711

@@ -916,8 +920,7 @@ object Semantic {
916920
case task :: rest =>
917921
checkedTasks = checkedTasks + task
918922

919-
// must be before heap snapshot
920-
task.value.ensureObjectExists()
923+
task.value.ensureFresh()
921924
val heapBefore = heap.snapshot()
922925
val res = doTask(task)
923926
res.errors.foreach(_.issue)

0 commit comments

Comments
 (0)