Skip to content

Commit 5f6294f

Browse files
committed
Constant final vals need to have right hand type.
Previously, a constant right hand side was not propagated to the type of a final val that implemented another val with a given type. The inherited type was used instead. This means final vals implementing abstract vals get evaluated too late.
1 parent 94bcf7c commit 5f6294f

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,19 @@ class Namer { typer: Typer =>
707707
// println(s"final inherited for $sym: ${inherited.toString}") !!!
708708
// println(s"owner = ${sym.owner}, decls = ${sym.owner.info.decls.show}")
709709
def isInline = sym.is(Final, butNot = Method)
710-
def widenRhs(tp: Type): Type = tp match {
711-
case tp: TermRef => widenRhs(tp.underlying)
712-
case tp: ExprType => widenRhs(tp.resultType)
710+
def widenRhs(tp: Type): Type = tp.widenTermRefExpr match {
713711
case tp: ConstantType if isInline => tp
714712
case _ => tp.widen.approximateUnion
715713
}
716714
val rhsCtx = ctx.addMode(Mode.InferringReturnType)
717-
def rhsType = typedAheadExpr(mdef.rhs, rhsProto)(rhsCtx).tpe
715+
def rhsType = typedAheadExpr(mdef.rhs, inherited orElse rhsProto)(rhsCtx).tpe
718716
def cookedRhsType = ctx.deskolemize(widenRhs(rhsType))
719-
def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.pos)
717+
lazy val lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.pos)
720718
//if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType")
721-
if (inherited.exists) inherited
719+
if (inherited.exists)
720+
if (sym.is(Final, butNot = Method) && lhsType.isInstanceOf[ConstantType])
721+
lhsType // keep constant types that fill in for a non-constant (to be revised when inline has landed).
722+
else inherited
722723
else {
723724
if (sym is Implicit) {
724725
val resStr = if (mdef.isInstanceOf[DefDef]) "result " else ""

0 commit comments

Comments
 (0)