From d7f876d5d3d018b89c2255789463640fe8f3ca14 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 6 Apr 2021 10:19:54 +0200 Subject: [PATCH 1/2] Revert "Recursively check nonvariant arguments of base types for realizability" This reverts commit 60e39653d1a73044056c5c257d02565c5ee3899f. --- .../tools/dotc/core/CheckRealizable.scala | 33 +++++------------ tests/neg/i11545.scala | 37 ------------------- 2 files changed, 10 insertions(+), 60 deletions(-) delete mode 100644 tests/neg/i11545.scala diff --git a/compiler/src/dotty/tools/dotc/core/CheckRealizable.scala b/compiler/src/dotty/tools/dotc/core/CheckRealizable.scala index 7f15bd6271f1..460647cdaf7c 100644 --- a/compiler/src/dotty/tools/dotc/core/CheckRealizable.scala +++ b/compiler/src/dotty/tools/dotc/core/CheckRealizable.scala @@ -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, argStr: String)(using Context) - extends Realizability(i" has conflicting base type${argStr}s $base1 and $base2") + class HasProblemBase(base1: Type, base2: Type)(using Context) + extends Realizability(i" has conflicting base types $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}") @@ -167,30 +167,17 @@ class CheckRealizable(using Context) { new HasProblemBounds(name, mbr.info) } - 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 + 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) } - case _ => Nil } val baseProblems = - tp.baseClasses.map(_.baseTypeOf(tp)).flatMap(baseTypeProblems(_, "")) + tp.baseClasses.map(_.baseTypeOf(tp)).flatMap(baseTypeProblems) baseProblems.foldLeft( refinementProblems.foldLeft( diff --git a/tests/neg/i11545.scala b/tests/neg/i11545.scala deleted file mode 100644 index 380027b7db41..000000000000 --- a/tests/neg/i11545.scala +++ /dev/null @@ -1,37 +0,0 @@ -@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 - } -} \ No newline at end of file From c1e1808d843dd731eae589b81cb5a3fec4cd2871 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 6 Apr 2021 10:21:57 +0200 Subject: [PATCH 2/2] Add test --- tests/pos/i11995.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/pos/i11995.scala diff --git a/tests/pos/i11995.scala b/tests/pos/i11995.scala new file mode 100644 index 000000000000..d5a089c1bb35 --- /dev/null +++ b/tests/pos/i11995.scala @@ -0,0 +1,18 @@ +trait MyBase[A]{ + def foo: String +} + +case class BothThing[L, R]() extends MyBase[L & R]: + def foo: String = "blather" + +trait Has[A] + +trait Console +trait Clock + +type ConsoleWithClock = Has[Console] with Has[Clock] + +class Spec[R <: Has[_]] + +object MySpec1 extends Spec[Has[Console] with Has[Clock]] // does not compile +object MySpec2 extends Spec[ConsoleWithClock] // okay