Skip to content

Commit 5a82985

Browse files
committed
Refactor trace
1 parent 2a47d89 commit 5a82985

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,11 @@ class Semantic {
306306

307307
case addr: Addr =>
308308
val target = if needResolve then resolve(addr.klass, field) else field
309+
val trace1 = trace.add(source)
309310
if target.is(Flags.Lazy) then
310-
value.call(target, Nil, superType = NoType, source, needResolve = false)
311+
given Trace = trace1
312+
val rhs = target.defTree.asInstanceOf[ValDef].rhs
313+
eval(rhs, addr, target.owner.asClass, cacheResult = true)
311314
else
312315
val obj = heap(addr)
313316
if obj.fields.contains(target) then
@@ -357,12 +360,15 @@ class Semantic {
357360
resolveSuper(addr.klass, superType, meth)
358361
else
359362
resolve(addr.klass, meth)
360-
if target.isOneOf(Flags.Method | Flags.Lazy) then
363+
364+
if target.isOneOf(Flags.Method) then
365+
val trace1 = trace.add(source)
361366
if target.hasSource then
367+
given Trace = trace1
362368
val cls = target.owner.enclosingClass.asClass
363369
if target.isPrimaryConstructor then
364370
val tpl = cls.defTree.asInstanceOf[TypeDef].rhs.asInstanceOf[Template]
365-
eval(tpl, addr, cls, cacheResult = true)(using env, ctx, trace.add(cls.defTree), promoted)
371+
eval(tpl, addr, cls, cacheResult = true)
366372
else
367373
val rhs = target.defTree.asInstanceOf[ValOrDefDef].rhs
368374
eval(rhs, addr, cls, cacheResult = true)
@@ -392,15 +398,18 @@ class Semantic {
392398

393399
/** Handle a new expression `new p.C` where `p` is abstracted by `value` */
394400
def instantiate(klass: ClassSymbol, ctor: Symbol, args: List[Value], source: Tree): Contextual[Result] =
401+
val trace1 = trace.add(source)
395402
value match {
396403
case Hot =>
397404
Result(Hot, Errors.empty)
398405

399406
case Cold =>
400-
val error = CallCold(ctor, source, trace.toVector)
407+
val error = CallCold(ctor, source, trace1.toVector)
401408
Result(Hot, error :: Nil)
402409

403410
case addr: Addr =>
411+
given Trace = trace1
412+
404413
// widen the outer to finitize addresses
405414
val outer = addr match
406415
case Warm(_, _: Warm, _, _) => Cold
@@ -653,17 +662,15 @@ class Semantic {
653662
// check args
654663
val (errors, args) = evalArgs(argss.flatten, thisV, klass)
655664

656-
val trace2: Trace = trace.add(expr)
657-
658665
ref match
659666
case Select(supert: Super, _) =>
660667
val SuperType(thisTp, superTp) = supert.tpe
661668
val thisValue2 = resolveThis(thisTp.classSymbol.asClass, thisV, klass, ref)
662-
Result(thisValue2, errors).call(ref.symbol, args, superTp, expr)(using env, ctx, trace2)
669+
Result(thisValue2, errors).call(ref.symbol, args, superTp, expr)
663670

664671
case Select(qual, _) =>
665672
val res = eval(qual, thisV, klass) ++ errors
666-
res.call(ref.symbol, args, superType = NoType, source = expr)(using env, ctx, trace2)
673+
res.call(ref.symbol, args, superType = NoType, source = expr)
667674

668675
case id: Ident =>
669676
id.tpe match
@@ -675,7 +682,7 @@ class Semantic {
675682
thisValue2.call(id.symbol, args, superType = NoType, expr, needResolve = false)
676683
case TermRef(prefix, _) =>
677684
val res = cases(prefix, thisV, klass, id) ++ errors
678-
res.call(id.symbol, args, superType = NoType, source = expr)(using env, ctx, trace2)
685+
res.call(id.symbol, args, superType = NoType, source = expr)
679686

680687
case Select(qualifier, name) =>
681688
eval(qualifier, thisV, klass).select(expr.symbol, expr)
@@ -870,7 +877,7 @@ class Semantic {
870877
// follow constructor
871878
if cls.hasSource then
872879
printer.println("init super class " + cls.show)
873-
val res2 = thisV.call(ctor, args, superType = NoType, source)(using env, ctx, trace.add(source))
880+
val res2 = thisV.call(ctor, args, superType = NoType, source)
874881
errorBuffer ++= res2.errors
875882

876883
// parents
@@ -957,13 +964,14 @@ class Semantic {
957964
object Semantic {
958965

959966
// ----- Utility methods and extractors --------------------------------
967+
inline def use[T, R](v: T)(inline op: T ?=> R): R = op(using v)
960968

961969
def typeRefOf(tp: Type)(using Context): TypeRef = tp.dealias.typeConstructor match {
962970
case tref: TypeRef => tref
963971
case hklambda: HKTypeLambda => typeRefOf(hklambda.resType)
964972
}
965973

966-
opaque type Arg = Tree | ByNameArg
974+
type Arg = Tree | ByNameArg
967975
case class ByNameArg(tree: Tree)
968976

969977
extension (arg: Arg)

0 commit comments

Comments
 (0)