Skip to content

Fix init checker on secondary constructor parameter access #13776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ object Semantic {
}

def callConstructor(ctor: Symbol, args: List[ArgInfo], source: Tree): Contextual[Result] = log("call " + ctor.show + ", args = " + args, printer, (_: Result).show) {
// init "fake" param fields for the secondary constructor
def addParamsAsFields(env: Env, ref: Ref, ctorDef: DefDef) = {
val paramSyms = ctorDef.termParamss.flatten.map(_.symbol)
paramSyms.map { acc =>
val value = env.lookup(acc)
ref.updateField(acc, value)
printer.println(acc.show + " initialized with " + value)
}
}
value match {
case Hot | Cold | _: RefSet | _: Fun =>
report.error("unexpected constructor call, meth = " + ctor + ", value = " + value, source)
Expand All @@ -668,6 +677,7 @@ object Semantic {
val tpl = cls.defTree.asInstanceOf[TypeDef].rhs.asInstanceOf[Template]
init(tpl, ref, cls)
else
addParamsAsFields(env, ref, ddef)
val initCall = ddef.rhs match
case Block(call :: _, _) => call
case call => call
Expand All @@ -682,12 +692,13 @@ object Semantic {
given Trace = trace1
val cls = ctor.owner.enclosingClass.asClass
val ddef = ctor.defTree.asInstanceOf[DefDef]
given Env= Env(ddef, args.map(_.value).widenArgs)
given Env = Env(ddef, args.map(_.value).widenArgs)
if ctor.isPrimaryConstructor then
val tpl = cls.defTree.asInstanceOf[TypeDef].rhs.asInstanceOf[Template]
val res = withTrace(trace.add(cls.defTree)) { eval(tpl, ref, cls, cacheResult = true) }
Result(ref, res.errors)
else
addParamsAsFields(env, ref, ddef)
eval(ddef.rhs, ref, cls, cacheResult = true)
else if ref.canIgnoreMethodCall(ctor) then
Result(Hot, Nil)
Expand Down Expand Up @@ -752,19 +763,25 @@ object Semantic {
Result(value2, errors)
}
}
end extension

extension (ref: Ref)
def accessLocal(tmref: TermRef, klass: ClassSymbol, source: Tree): Contextual[Result] =
val sym = tmref.symbol

def default() = Result(Hot, Nil)

if sym.is(Flags.Param) && sym.owner.isConstructor then
// if we can get the field from the Ref (which can only possibly be
// a secondary constructor parameter), then use it.
if (ref.objekt.hasField(sym))
Result(ref.objekt.field(sym), Errors.empty)
// instances of local classes inside secondary constructors cannot
// reach here, as those values are abstracted by Cold instead of Warm.
// This enables us to simplify the domain without sacrificing
// expressiveness nor soundess, as local classes inside secondary
// constructors are uncommon.
if sym.isContainedIn(klass) then
else if sym.isContainedIn(klass) then
Result(env.lookup(sym), Nil)
else
// We don't know much about secondary constructor parameters in outer scope.
Expand All @@ -777,7 +794,7 @@ object Semantic {
case vdef: ValDef =>
// resolve this for local variable
val enclosingClass = sym.owner.enclosingClass.asClass
val thisValue2 = resolveThis(enclosingClass, value, klass, source)
val thisValue2 = resolveThis(enclosingClass, ref, klass, source)
thisValue2 match {
case Hot => Result(Hot, Errors.empty)

Expand Down
17 changes: 17 additions & 0 deletions tests/init/pos/second-ctor-fields.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

class A(b: B) {
def this(b: B, m: Int) = {
this(b)
def foo = m // resolved to parameter `m`
class C { foo } // resolved to parameter `m`, as hidden field of `A`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this comment belong on the previous line? I don't see how it applies to class C.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to foo here will cause the interpreter to look up m (and from the PR's changes, find m as a hidden field of A)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it, thanks.

new C
}
}

class D(b: B) extends A(b, 10) {
val n = 10
}

class B {
val a = new D(this)
}