Skip to content

Commit 4f298c0

Browse files
committed
First example works
1 parent 0a39a8f commit 4f298c0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

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

Lines changed: 9 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,13 @@ 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 addr = Addr(cls, outer = Hot)
66+
heap(addr) = Objekt(cls, Map.empty, Map(cls -> Hot))
67+
val res = addr.call(cls.primaryConstructor, superType = NoType, tree)
68+
res.errors.foreach(_.issue)
6169
}
6270

6371
tree

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Semantic {
112112

113113
/** Result of abstract interpretation */
114114
case class Result(value: Value, errors: Seq[Error]) {
115-
def show(using Context) = ???
115+
def show(using Context) = value.show + ", errors = " + errors.map(_.toString)
116116

117117
def ++(errors: Seq[Error]): Result = this.copy(errors = this.errors ++ errors)
118118

@@ -202,7 +202,7 @@ class Semantic {
202202
else
203203
resolve(obj.klass, meth)
204204
if target.isPrimaryConstructor then
205-
init(addr.klass, addr)
205+
init(target.owner.asClass, addr)
206206
else if target.isOneOf(Flags.Method | Flags.Lazy) then
207207
if target.hasSource then
208208
val rhs = target.defTree.asInstanceOf[DefDef].rhs
@@ -518,7 +518,7 @@ class Semantic {
518518
cases(tref.prefix, thisV, klass, source)
519519

520520
/** Initialize part of an abstract object in `klass` of the inheritance chain */
521-
def init(klass: ClassSymbol, thisV: Addr)(using Context): Result =
521+
def init(klass: ClassSymbol, thisV: Addr)(using Context): Result = trace("init " + klass.show, printer, res => res.asInstanceOf[Result].show) {
522522
val errorBuffer = new mutable.ArrayBuffer[Error]
523523

524524
val tpl = klass.defTree.asInstanceOf[TypeDef].rhs.asInstanceOf[Template]
@@ -541,8 +541,9 @@ class Semantic {
541541
thisV.updateOuter(cls, res.value)
542542

543543
// follow constructor
544-
val res2 = thisV.call(ctor, superType = NoType, source)
545-
errorBuffer ++= res2.errors
544+
if !cls.defTree.isEmpty then
545+
val res2 = thisV.call(ctor, superType = NoType, source)
546+
errorBuffer ++= res2.errors
546547

547548
// parents
548549
def initParent(parent: Tree) = parent match {
@@ -551,15 +552,15 @@ class Semantic {
551552
argss.flatten.foreach { arg =>
552553
val res = eval(arg, thisV, klass)
553554
res.ensureHot("Argument must be an initialized value", arg)
554-
errorBuffer ++ res.errors
555+
errorBuffer ++= res.errors
555556
}
556557
superCall(tref, ctor, tree)
557558

558559
case tree @ NewExpr(tref, New(tpt), ctor, argss) =>
559560
argss.flatten.foreach { arg =>
560561
val res = eval(arg, thisV, klass)
561562
res.ensureHot("Argument must be an initialized value", arg)
562-
errorBuffer ++ res.errors
563+
errorBuffer ++= res.errors
563564
}
564565
superCall(tref, ctor, tree)
565566

@@ -584,15 +585,16 @@ class Semantic {
584585
case Some(parent) => initParent(parent)
585586
case None =>
586587
val tref = typeRefOf(klass.typeRef.baseType(mixin).typeConstructor)
587-
superCall(tref, tref.classSymbol.primaryConstructor, superParent)
588+
val ctor = tref.classSymbol.primaryConstructor
589+
if ctor.exists then superCall(tref, ctor, superParent)
588590
}
589591

590592

591593
// class body
592594
tpl.body.foreach {
593595
case vdef : ValDef =>
594596
val res = eval(vdef.rhs, thisV, klass)
595-
errorBuffer ++ res.errors
597+
errorBuffer ++= res.errors
596598
thisV.updateField(vdef.symbol, res.value)
597599

598600
case _: MemberDef =>
@@ -602,6 +604,7 @@ class Semantic {
602604
}
603605

604606
Result(thisV, errorBuffer.toList)
607+
}
605608

606609
// ----- Utility methods and extractors --------------------------------
607610

@@ -623,6 +626,8 @@ class Semantic {
623626

624627
case ref: RefTree if ref.symbol.is(Flags.Method) =>
625628
Some((ref, Nil))
629+
630+
case _ => None
626631
}
627632

628633
object NewExpr {

0 commit comments

Comments
 (0)