@@ -652,6 +652,15 @@ object Semantic {
652
652
}
653
653
654
654
def callConstructor (ctor : Symbol , args : List [ArgInfo ], source : Tree ): Contextual [Result ] = log(" call " + ctor.show + " , args = " + args, printer, (_ : Result ).show) {
655
+ // init "fake" param fields for the secondary constructor
656
+ def addParamsAsFields (env : Env , ref : Ref , ctorDef : DefDef ) = {
657
+ val paramSyms = ctorDef.termParamss.flatten.map(_.symbol)
658
+ paramSyms.map { acc =>
659
+ val value = env.lookup(acc)
660
+ ref.updateField(acc, value)
661
+ printer.println(acc.show + " initialized with " + value)
662
+ }
663
+ }
655
664
value match {
656
665
case Hot | Cold | _ : RefSet | _ : Fun =>
657
666
report.error(" unexpected constructor call, meth = " + ctor + " , value = " + value, source)
@@ -668,6 +677,7 @@ object Semantic {
668
677
val tpl = cls.defTree.asInstanceOf [TypeDef ].rhs.asInstanceOf [Template ]
669
678
init(tpl, ref, cls)
670
679
else
680
+ addParamsAsFields(env, ref, ddef)
671
681
val initCall = ddef.rhs match
672
682
case Block (call :: _, _) => call
673
683
case call => call
@@ -682,12 +692,13 @@ object Semantic {
682
692
given Trace = trace1
683
693
val cls = ctor.owner.enclosingClass.asClass
684
694
val ddef = ctor.defTree.asInstanceOf [DefDef ]
685
- given Env = Env (ddef, args.map(_.value).widenArgs)
695
+ given Env = Env (ddef, args.map(_.value).widenArgs)
686
696
if ctor.isPrimaryConstructor then
687
697
val tpl = cls.defTree.asInstanceOf [TypeDef ].rhs.asInstanceOf [Template ]
688
698
val res = withTrace(trace.add(cls.defTree)) { eval(tpl, ref, cls, cacheResult = true ) }
689
699
Result (ref, res.errors)
690
700
else
701
+ addParamsAsFields(env, ref, ddef)
691
702
eval(ddef.rhs, ref, cls, cacheResult = true )
692
703
else if ref.canIgnoreMethodCall(ctor) then
693
704
Result (Hot , Nil )
@@ -752,19 +763,26 @@ object Semantic {
752
763
Result (value2, errors)
753
764
}
754
765
}
766
+ end extension
767
+
768
+ extension (ref : Ref )
755
769
756
770
def accessLocal (tmref : TermRef , klass : ClassSymbol , source : Tree ): Contextual [Result ] =
757
771
val sym = tmref.symbol
758
772
759
773
def default () = Result (Hot , Nil )
760
774
761
775
if sym.is(Flags .Param ) && sym.owner.isConstructor then
776
+ // if we can get the field from the Ref (which can only possibly be
777
+ // a secondary constructor parameter), then use it.
778
+ if (ref.objekt.hasField(sym))
779
+ Result (ref.objekt.field(sym), Errors .empty)
762
780
// instances of local classes inside secondary constructors cannot
763
781
// reach here, as those values are abstracted by Cold instead of Warm.
764
782
// This enables us to simplify the domain without sacrificing
765
783
// expressiveness nor soundess, as local classes inside secondary
766
784
// constructors are uncommon.
767
- if sym.isContainedIn(klass) then
785
+ else if sym.isContainedIn(klass) then
768
786
Result (env.lookup(sym), Nil )
769
787
else
770
788
// We don't know much about secondary constructor parameters in outer scope.
@@ -777,7 +795,7 @@ object Semantic {
777
795
case vdef : ValDef =>
778
796
// resolve this for local variable
779
797
val enclosingClass = sym.owner.enclosingClass.asClass
780
- val thisValue2 = resolveThis(enclosingClass, value , klass, source)
798
+ val thisValue2 = resolveThis(enclosingClass, ref , klass, source)
781
799
thisValue2 match {
782
800
case Hot => Result (Hot , Errors .empty)
783
801
@@ -792,10 +810,7 @@ object Semantic {
792
810
793
811
case _ => default()
794
812
}
795
- end extension
796
-
797
- // ----- Promotion ----------------------------------------------------
798
- extension (ref : Ref )
813
+ // ----- Promotion ----------------------------------------------------
799
814
/** Whether the object is fully assigned
800
815
*
801
816
* It means all fields and outers are set. For performance, we don't check
0 commit comments