Skip to content

Commit cd914f3

Browse files
committed
Handle secondary constructor
1 parent 6b028ec commit cd914f3

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import util.Property
1212

1313
import ast.tpd.*
1414
import config.Printers.init as printer
15-
import reporting.trace.force as log
15+
import reporting.trace as log
1616

1717
import Semantic.Arg
1818
import Semantic.NewExpr
@@ -131,7 +131,7 @@ object ParamOverridingCheck:
131131
else
132132
ddef.rhs match
133133
case Block(Call(ref, argss) :: _, _) =>
134-
val args = evalArgs(argss.flatten, thisV, thisV.klass)
134+
val args = evalArgs(argss.flatten, thisV, cls)
135135
thisV.callConstructor(ref.symbol, args)
136136

137137
}
@@ -215,7 +215,16 @@ object ParamOverridingCheck:
215215
Skolem(source)
216216

217217
case tmref: TermRef if tmref.prefix == NoPrefix =>
218-
Unknown(source)
218+
val sym = tmref.symbol
219+
220+
if sym.is(Flags.Param) && sym.owner.isConstructor then
221+
val enclosingClass = sym.owner.enclosingClass.asClass
222+
if enclosingClass == klass then
223+
thisV.field(sym)
224+
else
225+
Unknown(source)
226+
else
227+
Unknown(source)
219228

220229
case tmref: TermRef =>
221230
val cls = tmref.widenSingleton.classSymbol

tests/init/pos/i15764b.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class A(val y: Int):
2+
println(y) // should not issue a warning here
3+
foo()
4+
def foo() = println(y)
5+
6+
class B(z: Int) extends A(z)
7+
8+
class C(override val y: Int) extends B(y)

tests/init/pos/i15764c.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class A(val y: Int):
2+
println(y) // should not issue a warning here
3+
foo()
4+
def foo() = println(y)
5+
6+
class B(m: Int, n: Int) extends A(m):
7+
def this(z: Int) = this(z, z)
8+
9+
class C(override val y: Int) extends B(y)

0 commit comments

Comments
 (0)