Skip to content

Rely on variance to typecheck more uses of wildcards #6719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
* If the original left-hand type `leftRoot` is a path `p.type`,
* and the current widened left type is an application with wildcard arguments
* such as `C[_]`, where `X` is `C`'s type parameter corresponding to the `_` argument,
* compare with `C[p.X]` instead. Otherwise return `false`.
* compare with `C[p.X]` instead. Otherwise approximate based on variance.
* Also do a capture conversion in either of the following cases:
*
* - If we are after typer. We generally relax soundness requirements then.
Expand All @@ -1203,10 +1203,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
* paths is less intrusive than skolemization.
*/
def compareCaptured(arg1: TypeBounds, arg2: Type) = tparam match {
case tparam: Symbol
if leftRoot.isStable || (ctx.isAfterTyper || ctx.mode.is(Mode.TypevarsMissContext)) && leftRoot.member(tparam.name).exists =>
val captured = TypeRef(leftRoot, tparam)
isSubArg(captured, arg2)
case tparam: Symbol =>
if (leftRoot.isStable || (ctx.isAfterTyper || ctx.mode.is(Mode.TypevarsMissContext))
&& leftRoot.member(tparam.name).exists) {
val captured = TypeRef(leftRoot, tparam)
isSubArg(captured, arg2)
}
else if (v > 0)
isSubType(paramBounds(tparam).hi, arg2)
else if (v < 0)
isSubType(arg2, paramBounds(tparam).lo)
else
false
case _ =>
false
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class ExpandSAMs extends MiniPhase {
anon.rhs match {
case PartialFunctionRHS(pf) =>
val anonSym = anon.symbol

val parents = List(defn.AbstractPartialFunctionType.appliedTo(tpe.argInfos), defn.SerializableType)
val anonTpe = anon.tpe.widen
val parents = List(defn.AbstractPartialFunctionType.appliedTo(anonTpe.firstParamTypes.head, anonTpe.resultType), defn.SerializableType)
val pfSym = ctx.newNormalizedClassSymbol(anonSym.owner, tpnme.ANON_CLASS, Synthetic | Final, parents, coord = tree.span)

def overrideSym(sym: Symbol) = sym.copy(
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/t5729.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object C {
def join(in: Seq[List[_]]): Int = 1
def join[S](in: Seq[List[S]]): String = "x"

val x= join(Seq[List[Int]]()) // error: ambiguous overload
assert(x == "x")
}
13 changes: 13 additions & 0 deletions tests/pos/capture-variance.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Hi
class Lo extends Hi
class Cov1[+T]
class Cov2[+T >: Lo <: Hi]
class Contra1[-T]
class Contra2[-T >: Lo <: Hi]

object Test {
val a: List[Cov1[Any]] = ??? : List[Cov1[_]]
val b: List[Cov2[Hi]] = ??? : List[Cov2[_]]
val c: List[Contra1[Nothing]] = ??? : List[Contra1[_]]
val d: List[Contra2[Lo]] = ??? : List[Contra2[_]]
}
9 changes: 0 additions & 9 deletions tests/run/t5729.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@ object Test extends App {
def join[S](in: Seq[T[S]]): String = "x"
val x = join(null: Seq[T[_]])
assert(x == 1) // first alt chosen, since second requires a capture conversion in adapt
C
}

object C {
def join(in: Seq[List[_]]): Int = 1
def join[S](in: Seq[List[S]]): String = "x"

val x= join(Seq[List[Int]]()) // second alt chosen, since it is more specific
assert(x == "x")
}