Skip to content

Commit 38e47df

Browse files
committed
Specialize types of inline values
Specialize types of ibline values even if a wider type was gven in an inherited definition. This generalizes a previous scheme for final vals.
1 parent e52ac88 commit 38e47df

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,15 +1320,8 @@ class Namer { typer: Typer =>
13201320
def cookedRhsType = deskolemize(dealiasIfUnit(widenRhs(rhsType)))
13211321
def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.span)
13221322
//if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType")
1323-
if (inherited.exists) {
1324-
if (sym.is(Final, butNot = Method)) {
1325-
val tp = lhsType
1326-
if (tp.isInstanceOf[ConstantType])
1327-
tp // keep constant types that fill in for a non-constant (to be revised when inline has landed).
1328-
else inherited
1329-
}
1330-
else inherited
1331-
}
1323+
if (inherited.exists)
1324+
if (isInlineVal) lhsType else inherited
13321325
else {
13331326
if (sym.is(Implicit))
13341327
mdef match {

tests/pos/givenFallback.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait TC[T] { def x: Int; def y: Int = 0 }
2+
3+
given [T] as TC[T] {
4+
inline val x = 1
5+
}
6+
7+
given as TC[Int] {
8+
inline val x = 2
9+
inline override val y = 3
10+
}
11+
12+
object Test extends App {
13+
val z: 2 = the[TC[Int]].x
14+
val _: 3 = the[TC[Int]].y
15+
}

0 commit comments

Comments
 (0)