Skip to content

Commit 450f8ac

Browse files
authored
Merge pull request #12495 from dotty-staging/new-init
A simpler implementation of init checker
2 parents a956774 + 12bfbd2 commit 450f8ac

17 files changed

+1115
-20
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Checker extends MiniPhase {
2626
// cache of class summary
2727
private val cache = new Cache
2828

29+
private val semantic = new Semantic
30+
2931
override val runsAfter = Set(Pickler.name)
3032

3133
override def isEnabled(using Context): Boolean =
@@ -57,7 +59,14 @@ class Checker extends MiniPhase {
5759
env = Env(ctx.withOwner(cls), cache)
5860
)
5961

60-
Checking.checkClassBody(tree)
62+
// Checking.checkClassBody(tree)
63+
64+
import semantic._
65+
val tpl = tree.rhs.asInstanceOf[Template]
66+
val thisRef = ThisRef(cls)
67+
val heap = Objekt(cls, fields = mutable.Map.empty)
68+
val res = eval(tpl, thisRef, cls)(using heap, ctx, Vector.empty)
69+
res.errors.foreach(_.issue)
6170
}
6271

6372
tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ object Checking {
321321
val f = denot.symbol
322322
if !f.isOneOf(excludedFlags) && f.hasSource then
323323
buffer += Promote(FieldReturn(warm, f)(source))(source)
324-
buffer += FieldAccess(warm, f)(source)
325324
}
326325

327326
classRef.membersBasedOnFlags(Flags.Method, Flags.Deferred).foreach { denot =>
@@ -342,7 +341,7 @@ object Checking {
342341
for (eff <- buffer.toList) {
343342
val errs = check(eff)
344343
if !errs.isEmpty then
345-
return UnsafePromotion(warm, eff.source, state.path, errs.toList).toErrors
344+
return UnsafePromotion(eff.source, state.path, errs.toList).toErrors
346345
}
347346
Errors.empty
348347

@@ -355,7 +354,7 @@ object Checking {
355354
Errors.empty
356355
else pot match {
357356
case pot: ThisRef =>
358-
PromoteThis(pot, eff.source, state.path).toErrors
357+
PromoteThis(eff.source, state.path).toErrors
359358

360359
case _: Cold =>
361360
PromoteCold(eff.source, state.path).toErrors
@@ -374,7 +373,7 @@ object Checking {
374373
}
375374

376375
if (errs1.nonEmpty || errs2.nonEmpty)
377-
UnsafePromotion(pot, eff.source, state.path, errs1 ++ errs2).toErrors
376+
UnsafePromotion(eff.source, state.path, errs1 ++ errs2).toErrors
378377
else
379378
Errors.empty
380379

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Types._, Symbols._, Contexts._
1212
import Effects._, Potentials._
1313

1414
object Errors {
15-
type Errors = List[Error]
15+
type Errors = Seq[Error]
1616
val empty: Errors = Nil
1717

1818
def show(errs: Errors)(using Context): String =
@@ -70,12 +70,12 @@ object Errors {
7070
}
7171

7272
/** Promote `this` under initialization to fully-initialized */
73-
case class PromoteThis(pot: ThisRef, source: Tree, trace: Vector[Tree]) extends Error {
73+
case class PromoteThis(source: Tree, trace: Vector[Tree]) extends Error {
7474
def show(using Context): String = "Promote the value under initialization to fully-initialized."
7575
}
7676

7777
/** Promote `this` under initialization to fully-initialized */
78-
case class PromoteWarm(pot: Warm, source: Tree, trace: Vector[Tree]) extends Error {
78+
case class PromoteWarm(source: Tree, trace: Vector[Tree]) extends Error {
7979
def show(using Context): String =
8080
"Promoting the value under initialization to fully-initialized."
8181
}
@@ -98,11 +98,12 @@ object Errors {
9898

9999
case class CallUnknown(meth: Symbol, source: Tree, trace: Vector[Tree]) extends Error {
100100
def show(using Context): String =
101-
"Calling the external method " + meth.show + " may cause initialization errors" + "."
101+
val prefix = if meth.is(Flags.Method) then "Calling the external method " else "Accessing the external field"
102+
prefix + meth.show + " may cause initialization errors" + "."
102103
}
103104

104105
/** Promote a value under initialization to fully-initialized */
105-
case class UnsafePromotion(pot: Potential, source: Tree, trace: Vector[Tree], errors: Errors) extends Error {
106+
case class UnsafePromotion(source: Tree, trace: Vector[Tree], errors: Errors) extends Error {
106107
assert(errors.nonEmpty)
107108

108109
override def issue(using Context): Unit =

0 commit comments

Comments
 (0)