@@ -652,6 +652,15 @@ object Semantic {
652652 }
653653
654654 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+ }
655664 value match {
656665 case Hot | Cold | _ : RefSet | _ : Fun =>
657666 report.error(" unexpected constructor call, meth = " + ctor + " , value = " + value, source)
@@ -668,6 +677,7 @@ object Semantic {
668677 val tpl = cls.defTree.asInstanceOf [TypeDef ].rhs.asInstanceOf [Template ]
669678 init(tpl, ref, cls)
670679 else
680+ addParamsAsFields(env, ref, ddef)
671681 val initCall = ddef.rhs match
672682 case Block (call :: _, _) => call
673683 case call => call
@@ -682,12 +692,13 @@ object Semantic {
682692 given Trace = trace1
683693 val cls = ctor.owner.enclosingClass.asClass
684694 val ddef = ctor.defTree.asInstanceOf [DefDef ]
685- given Env = Env (ddef, args.map(_.value).widenArgs)
695+ given Env = Env (ddef, args.map(_.value).widenArgs)
686696 if ctor.isPrimaryConstructor then
687697 val tpl = cls.defTree.asInstanceOf [TypeDef ].rhs.asInstanceOf [Template ]
688698 val res = withTrace(trace.add(cls.defTree)) { eval(tpl, ref, cls, cacheResult = true ) }
689699 Result (ref, res.errors)
690700 else
701+ addParamsAsFields(env, ref, ddef)
691702 eval(ddef.rhs, ref, cls, cacheResult = true )
692703 else if ref.canIgnoreMethodCall(ctor) then
693704 Result (Hot , Nil )
@@ -752,19 +763,25 @@ object Semantic {
752763 Result (value2, errors)
753764 }
754765 }
766+ end extension
755767
768+ extension (ref : Ref )
756769 def accessLocal (tmref : TermRef , klass : ClassSymbol , source : Tree ): Contextual [Result ] =
757770 val sym = tmref.symbol
758771
759772 def default () = Result (Hot , Nil )
760773
761774 if sym.is(Flags .Param ) && sym.owner.isConstructor then
775+ // if we can get the field from the Ref (which can only possibly be
776+ // a secondary constructor parameter), then use it.
777+ if (ref.objekt.hasField(sym))
778+ Result (ref.objekt.field(sym), Errors .empty)
762779 // instances of local classes inside secondary constructors cannot
763780 // reach here, as those values are abstracted by Cold instead of Warm.
764781 // This enables us to simplify the domain without sacrificing
765782 // expressiveness nor soundess, as local classes inside secondary
766783 // constructors are uncommon.
767- if sym.isContainedIn(klass) then
784+ else if sym.isContainedIn(klass) then
768785 Result (env.lookup(sym), Nil )
769786 else
770787 // We don't know much about secondary constructor parameters in outer scope.
@@ -777,7 +794,7 @@ object Semantic {
777794 case vdef : ValDef =>
778795 // resolve this for local variable
779796 val enclosingClass = sym.owner.enclosingClass.asClass
780- val thisValue2 = resolveThis(enclosingClass, value , klass, source)
797+ val thisValue2 = resolveThis(enclosingClass, ref , klass, source)
781798 thisValue2 match {
782799 case Hot => Result (Hot , Errors .empty)
783800
0 commit comments