Skip to content

Commit 61629e3

Browse files
oderskyBlaisorblade
authored andcommitted
Fix #4031: Check arguments of dependent methods for realizability
The first attempt required changing z1720.scala; but that became unnecessary after aligning isRealizable with isStable. A TermRef is stable if its underlying type is stable. Realizability should behave the same way.
1 parent 72ea704 commit 61629e3

File tree

8 files changed

+97
-14
lines changed

8 files changed

+97
-14
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,6 +4513,8 @@ object Types {
45134513
else if (lo `eq` hi) lo
45144514
else Range(lower(lo), upper(hi))
45154515

4516+
protected def emptyRange: Type = range(defn.NothingType, defn.AnyType)
4517+
45164518
protected def isRange(tp: Type): Boolean = tp.isInstanceOf[Range]
45174519

45184520
protected def lower(tp: Type): Type = tp match {
@@ -4586,7 +4588,7 @@ object Types {
45864588
forwarded.orElse(
45874589
range(super.derivedSelect(tp, preLo).loBound, super.derivedSelect(tp, preHi).hiBound))
45884590
case _ =>
4589-
super.derivedSelect(tp, pre) match {
4591+
if (pre == defn.AnyType) pre else super.derivedSelect(tp, pre) match {
45904592
case TypeBounds(lo, hi) => range(lo, hi)
45914593
case tp => tp
45924594
}
@@ -4637,7 +4639,7 @@ object Types {
46374639
else tp.derivedTypeBounds(lo, hi)
46384640

46394641
override protected def derivedSuperType(tp: SuperType, thistp: Type, supertp: Type): Type =
4640-
if (isRange(thistp) || isRange(supertp)) range(defn.NothingType, defn.AnyType)
4642+
if (isRange(thistp) || isRange(supertp)) emptyRange
46414643
else tp.derivedSuperType(thistp, supertp)
46424644

46434645
override protected def derivedAppliedType(tp: AppliedType, tycon: Type, args: List[Type]): Type =

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,9 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
478478
addArg(typedArg(arg, formal), formal)
479479
if (methodType.isParamDependent && typeOfArg(arg).exists)
480480
// `typeOfArg(arg)` could be missing because the evaluation of `arg` produced type errors
481-
safeSubstParam(_, methodType.paramRefs(n), typeOfArg(arg))
482-
else identity
481+
safeSubstParam(_, methodType.paramRefs(n), typeOfArg(arg), initVariance = -1)
482+
else
483+
identity
483484
}
484485

485486
def missingArg(n: Int): Unit = {

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import NameOps._
1313
import collection.mutable
1414
import reporting.diagnostic.messages._
1515
import Checking.checkNoPrivateLeaks
16+
import CheckRealizable._
1617

1718
trait TypeAssigner {
1819
import tpd._
@@ -105,7 +106,7 @@ trait TypeAssigner {
105106
case info: ClassInfo =>
106107
range(defn.NothingType, apply(classBound(info)))
107108
case _ =>
108-
range(defn.NothingType, defn.AnyType) // should happen only in error cases
109+
emptyRange // should happen only in error cases
109110
}
110111
case tp: ThisType if toAvoid(tp.cls) =>
111112
range(defn.NothingType, apply(classBound(tp.cls.classInfo)))
@@ -340,13 +341,31 @@ trait TypeAssigner {
340341
}
341342
}
342343

343-
/** Substitute argument type `argType` for parameter `pref` in type `tp`,
344-
* skolemizing the argument type if it is not stable and `pref` occurs in `tp`.
344+
/** Substitute argument type `argType` for parameter `pref` in type `tp`, but
345+
* take special measures if the argument is not realizable:
346+
* 1. If the widened argument type is known to have good bounds,
347+
* substitute the skolemized argument type.
348+
* 2. If the widened argument type is not known to have good bounds, eliminate all references
349+
* to the parameter in `tp`.
350+
* (2) is necessary since even with a skolemized type we might break subtyping if
351+
* bounds are bad. This could lead to errors not being detected. A test case is the second
352+
* failure in neg/i4031.scala
345353
*/
346-
def safeSubstParam(tp: Type, pref: ParamRef, argType: Type)(implicit ctx: Context): Type = {
354+
def safeSubstParam(tp: Type, pref: ParamRef, argType: Type, initVariance: Int = 1)(implicit ctx: Context): Type = {
347355
val tp1 = tp.substParam(pref, argType)
348-
if ((tp1 eq tp) || argType.isStable) tp1
349-
else tp.substParam(pref, SkolemType(argType.widen))
356+
if ((tp1 eq tp) || argType.isStableRealizable) tp1
357+
else {
358+
val widenedArgType = argType.widen
359+
if (realizability(widenedArgType) == Realizable)
360+
tp.substParam(pref, SkolemType(widenedArgType))
361+
else {
362+
val avoidParam = new ApproximatingTypeMap {
363+
variance = initVariance
364+
def apply(t: Type) = if (t `eq` pref) emptyRange else mapOver(t)
365+
}
366+
avoidParam(tp)
367+
}
368+
}
350369
}
351370

352371
/** Substitute types of all arguments `args` for corresponding `params` in `tp`.

tests/neg/i4031-posttyper.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object App {
2+
trait A { type L >: Any}
3+
def upcast(a: A, x: Any): a.L = x
4+
lazy val p: A { type L <: Nothing } = p
5+
val q = new A { type L = Any }
6+
def coerce2(x: Any): Int = upcast(p, x): p.L // error: not a legal path
7+
}

tests/neg/i4031.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object App {
2+
trait A { type L >: Any}
3+
def upcast(a: A, x: Any): a.L = x
4+
lazy val p: A { type L <: Nothing } = p
5+
val q = new A { type L = Any }
6+
def coerce(x: Any): Int = upcast(p, x) // error: not a legal path
7+
8+
def compare(x: A, y: x.L) = assert(x == y)
9+
def compare2(x: A)(y: x.type) = assert(x == y)
10+
11+
12+
def main(args: Array[String]): Unit = {
13+
println(coerce("Uh oh!"))
14+
compare(p, p) // error: not a legal path
15+
compare2(p)(p) // error: not a legal path
16+
}
17+
}

tests/neg/i50-volatile.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class Test {
1212

1313
class Client extends o.Inner // old-error // old-error
1414

15-
def xToString(x: o.X): String = x // old-error
15+
def xToString(x: o.X): String = x // error
1616

1717
def intToString(i: Int): String = xToString(i)
1818
}
19-
object Test2 {
2019

21-
import Test.o._ // error
20+
object Test2 {
21+
trait A {
22+
type X = String
23+
}
24+
trait B {
25+
type X = Int
26+
}
27+
lazy val o: A & B = ???
2228

23-
def xToString(x: X): String = x
29+
def xToString(x: o.X): String = x // error
2430

31+
def intToString(i: Int): String = xToString(i)
2532
}

tests/neg/realizability.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class C { type T }
2+
3+
class Test {
4+
5+
type D <: C
6+
7+
lazy val a: C = ???
8+
final lazy val b: C = ???
9+
val c: D = ???
10+
final lazy val d: D = ???
11+
12+
val x1: a.T = ??? // error: not a legal path, since a is lazy & non-final
13+
val x2: b.T = ??? // OK, b is lazy but concrete
14+
val x3: c.T = ??? // OK, c is abstract but strict
15+
val x4: d.T = ??? // error: not a legal path since d is abstract and lazy
16+
17+
val y1: Singleton = a
18+
val y2: Singleton = a
19+
val y3: Singleton = a
20+
val y4: Singleton = a
21+
22+
}

tests/pos/i4031.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object App {
2+
trait A { type L >: Any}
3+
def upcast(a: A, x: Any): a.L = x
4+
lazy val p: A { type L <: Nothing } = p
5+
val q = new A { type L = Any }
6+
def coerce1(x: Any): Any = upcast(q, x) // ok
7+
def coerce3(x: Any): Any = upcast(p, x) // ok, since dependent result type is not needed
8+
}

0 commit comments

Comments
 (0)