Skip to content

Recursively check nonvariant arguments of base types for realizability #11549

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 1 commit into from
Feb 28, 2021
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
33 changes: 23 additions & 10 deletions compiler/src/dotty/tools/dotc/core/CheckRealizable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object CheckRealizable {
class HasProblemBaseArg(typ: Type, argBounds: TypeBounds)(using Context)
extends Realizability(i" has a base type $typ with possibly conflicting parameter bounds ${argBounds.lo} <: ... <: ${argBounds.hi}")

class HasProblemBase(base1: Type, base2: Type)(using Context)
extends Realizability(i" has conflicting base types $base1 and $base2")
class HasProblemBase(base1: Type, base2: Type, argStr: String)(using Context)
extends Realizability(i" has conflicting base type${argStr}s $base1 and $base2")

class HasProblemField(fld: SingleDenotation, problem: Realizability)(using Context)
extends Realizability(i" has a member $fld which is not a legal path\nsince ${fld.symbol.name}: ${fld.info}${problem.msg}")
Expand Down Expand Up @@ -167,17 +167,30 @@ class CheckRealizable(using Context) {
new HasProblemBounds(name, mbr.info)
}

def baseTypeProblems(base: Type) = base match {
case AndType(base1, base2) =>
new HasProblemBase(base1, base2) :: Nil
case base =>
base.argInfos.collect {
case bounds @ TypeBounds(lo, hi) if !(lo <:< hi) =>
new HasProblemBaseArg(base, bounds)
def baseTypeProblems(base: Type, argStr: String): List[Realizability] = base match {
case base: AndType =>
def factors(tp: Type): List[Type] = tp match
case AndType(tp1, tp2) => factors(tp1) ++ factors(tp2)
case _ => tp :: Nil
for case AndType(base1, base2) <-
factors(base).groupBy(_.classSymbol).values.map(_.reduce(_ & _)).toList
// try to merge factors with common class symbols
// if we cannot, it's a conflict
yield HasProblemBase(base1, base2, argStr)
case base: AppliedType =>
base.argInfos.lazyZip(base.tycon.typeParams).flatMap { (arg, tparam) =>
arg match
case bounds @ TypeBounds(lo, hi) if !(lo <:< hi) =>
new HasProblemBaseArg(base, bounds) :: Nil
case arg if tparam.paramVarianceSign == 0 =>
baseTypeProblems(arg, " argument")
case _ =>
Nil
}
case _ => Nil
}
val baseProblems =
tp.baseClasses.map(_.baseTypeOf(tp)).flatMap(baseTypeProblems)
tp.baseClasses.map(_.baseTypeOf(tp)).flatMap(baseTypeProblems(_, ""))

baseProblems.foldLeft(
refinementProblems.foldLeft(
Expand Down
37 changes: 37 additions & 0 deletions tests/neg/i11545.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@main def test: Unit = {
trait S[A]
trait Inv[A]

locally {
class P[X] extends S[Inv[X] & Inv[String]] // error: cannot be instantiated

def patmat[A, Y](s: S[Inv[A] & Y]): A = s match {
case p: P[x] =>
"Hello"
}

val got: Int = patmat[Int, Inv[String]](new P)
}

locally {
class P[X] extends S[S[Inv[X] & Inv[String]]] // error: cannot be instantiated

def patmat[A, Y](s: S[S[Inv[A] & Y]]): A = s match {
case p: P[x] =>
"Hello"
}

val got: Int = patmat[Int, Inv[String]](new P)
}

locally {
abstract class P[X] extends S[Inv[X] & Inv[String]]

def patmat[A, Y](s: S[Inv[A] & Y]): A = s match {
case p: P[x] =>
"Hello"
}

val got: Int = patmat[Int, Inv[String]](new P {}) // error: cannot be instantiated
}
}