Skip to content

Commit b85f964

Browse files
committed
Fix #4031: Check arguments of dependent methods for realizability
1 parent cf65da9 commit b85f964

File tree

10 files changed

+121
-15
lines changed

10 files changed

+121
-15
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Periods._
1919
import util.Positions.{Position, NoPosition}
2020
import util.Stats._
2121
import util.{DotClass, SimpleIdentitySet}
22+
import CheckRealizable._
2223
import reporting.diagnostic.Message
2324
import reporting.diagnostic.messages.CyclicReferenceInvolving
2425
import ast.tpd._
@@ -155,6 +156,12 @@ object Types {
155156
case _ => false
156157
}
157158

159+
/** Does this type denote a realizable stable reference? Much more expensive to checl
160+
* than isStable, that's why some of the checks are done later in PostTyper.
161+
*/
162+
final def isRealizable(implicit ctx: Context): Boolean =
163+
isStable && realizability(this) == Realizable
164+
158165
/** Is this type a (possibly refined or applied or aliased) type reference
159166
* to the given type symbol?
160167
* @sym The symbol to compare to. It must be a class symbol or abstract type.
@@ -4069,6 +4076,8 @@ object Types {
40694076
else if (variance < 0) lo
40704077
else Range(lower(lo), upper(hi))
40714078

4079+
protected def emptyRange = range(defn.NothingType, defn.AnyType)
4080+
40724081
protected def isRange(tp: Type) = tp.isInstanceOf[Range]
40734082

40744083
protected def lower(tp: Type) = tp match {
@@ -4136,7 +4145,7 @@ object Types {
41364145
forwarded.orElse(
41374146
range(super.derivedSelect(tp, preLo), super.derivedSelect(tp, preHi)))
41384147
case _ =>
4139-
super.derivedSelect(tp, pre)
4148+
if (pre == defn.AnyType) pre else super.derivedSelect(tp, pre)
41404149
}
41414150

41424151
override protected def derivedRefinedType(tp: RefinedType, parent: Type, info: Type) =
@@ -4184,7 +4193,7 @@ object Types {
41844193
else tp.derivedTypeBounds(lo, hi)
41854194

41864195
override protected def derivedSuperType(tp: SuperType, thistp: Type, supertp: Type) =
4187-
if (isRange(thistp) || isRange(supertp)) range(defn.NothingType, defn.AnyType)
4196+
if (isRange(thistp) || isRange(supertp)) emptyRange
41884197
else tp.derivedSuperType(thistp, supertp)
41894198

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

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
413413
def addTyped(arg: Arg, formal: Type): Type => Type = {
414414
addArg(typedArg(arg, formal), formal)
415415
if (methodType.isParamDependent)
416-
safeSubstParam(_, methodType.paramRefs(n), typeOfArg(arg))
417-
else identity
416+
safeSubstParam(_, methodType.paramRefs(n), typeOfArg(arg), initVariance = -1)
417+
else
418+
identity
418419
}
419420

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

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

+25-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import collection.mutable
1414
import reporting.diagnostic.Message
1515
import reporting.diagnostic.messages._
1616
import Checking.checkNoPrivateLeaks
17+
import CheckRealizable._
1718

1819
trait TypeAssigner {
1920
import tpd._
@@ -106,7 +107,7 @@ trait TypeAssigner {
106107
case info: ClassInfo =>
107108
range(defn.NothingType, apply(classBound(info)))
108109
case _ =>
109-
range(defn.NothingType, defn.AnyType) // should happen only in error cases
110+
emptyRange // should happen only in error cases
110111
}
111112
case tp: ThisType if toAvoid(tp.cls) =>
112113
range(defn.NothingType, apply(classBound(tp.cls.classInfo)))
@@ -337,13 +338,31 @@ trait TypeAssigner {
337338
}
338339
}
339340

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

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

tests/neg/i4031-posttyper.scala

+7
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

+17
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

+11-4
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

+22
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/neg/z1720.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test
2+
3+
class Thing {
4+
def info: Info[this.type] = InfoRepository.getInfo(this)
5+
def info2: Info[this.type] = {
6+
def self: this.type = this
7+
InfoRepository.getInfo(self) // error: not a legal path
8+
}
9+
}
10+
11+
trait Info[T]
12+
case class InfoImpl[T](thing: T) extends Info[T]
13+
14+
object InfoRepository {
15+
def getInfo(t: Thing): Info[t.type] = InfoImpl(t)
16+
}

tests/pos/i4031.scala

+8
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+
}

tests/pos/z1720.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package test
33
class Thing {
44
def info: Info[this.type] = InfoRepository.getInfo(this)
55
def info2: Info[this.type] = {
6-
def self: this.type = this
6+
val self: this.type = this
77
InfoRepository.getInfo(self)
88
}
99
}

0 commit comments

Comments
 (0)