Skip to content

Fix problem in unapply typing. #177

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class TypeComparer(initctx: Context) extends DotClass {

private def traceInfo(tp1: Type, tp2: Type) =
s"${tp1.show} <:< ${tp2.show}" +
(if (ctx.settings.verbose.value) s"${tp1.getClass} ${tp2.getClass}" else "")
(if (ctx.settings.verbose.value) s" ${tp1.getClass} ${tp2.getClass}${if (frozenConstraint) " frozen" else ""}" else "")

def isSubType(tp1: Type, tp2: Type): Boolean = /*>|>*/ ctx.traceIndented(s"isSubType ${traceInfo(tp1, tp2)}", subtyping) /*<|<*/ {
if (tp2 eq NoType) false
Expand Down Expand Up @@ -589,7 +589,8 @@ class TypeComparer(initctx: Context) extends DotClass {
NamedType(tp1.prefix, tp1.name, sd.derivedSingleDenotation(sd.symbol, tp))
secondTry(OrType.make(derivedRef(tp11), derivedRef(tp12)), tp2)
case TypeBounds(lo1, hi1) =>
if ((tp1.symbol is GADTFlexType) && !isSubTypeWhenFrozen(hi1, tp2))
if ((ctx.mode is Mode.GADTflexible) && (tp1.symbol is GADTFlexType) &&
!isSubTypeWhenFrozen(hi1, tp2))
trySetType(tp1, TypeBounds(lo1, hi1 & tp2))
else if (lo1 eq hi1) isSubType(hi1, tp2)
else tryRebase2nd
Expand All @@ -607,7 +608,8 @@ class TypeComparer(initctx: Context) extends DotClass {
}
def compareNamed: Boolean = tp2.info match {
case TypeBounds(lo2, hi2) =>
if ((tp2.symbol is GADTFlexType) && !isSubTypeWhenFrozen(tp1, lo2))
if ((ctx.mode is Mode.GADTflexible) && (tp2.symbol is GADTFlexType) &&
!isSubTypeWhenFrozen(tp1, lo2))
trySetType(tp2, TypeBounds(lo2 | tp1, hi2))
else
((frozenConstraint || !isCappable(tp1)) && isSubType(tp1, lo2)
Expand Down
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ trait Applications extends Compatibility { self: Typer =>
/** Can `subtp` be made to be a subtype of `tp`, possibly by dropping some
* refinements in `tp`?
*/
def isSubTypeOfParent(subtp: Type, tp: Type): Boolean =
def isSubTypeOfParent(subtp: Type, tp: Type)(implicit ctx: Context): Boolean =
if (subtp <:< tp) true
else tp match {
case RefinedType(parent, _) => isSubTypeOfParent(subtp, parent)
Expand All @@ -640,7 +640,7 @@ trait Applications extends Compatibility { self: Typer =>
fullyDefinedType(unapplyArgType, "extractor argument", tree.pos)
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
pt
} else if (isSubTypeOfParent(unapplyArgType, wpt)) {
} else if (isSubTypeOfParent(unapplyArgType, wpt)(ctx.addMode(Mode.GADTflexible))) {
maximizeType(unapplyArgType) match {
case Some(tvar) =>
def msg =
Expand Down
3 changes: 3 additions & 0 deletions src/dotty/tools/dotc/typer/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ object Mode {
val TypevarsMissContext = newMode(4, "TypevarsMissContext")
val InSuperCall = newMode(5, "InSuperCall")

/** Allow GADTFlexType labelled types to have their bounds adjusted */
val GADTflexible = newMode(8, "GADTflexible")

val PatternOrType = Pattern | Type
}
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class tests extends CompilerTest {
@Test def neg_tailcall = compileFile(negDir, "tailcall/tailrec", xerrors = 7)
@Test def neg_tailcall2 = compileFile(negDir, "tailcall/tailrec-2", xerrors = 2)
@Test def neg_tailcall3 = compileFile(negDir, "tailcall/tailrec-3", xerrors = 2)
@Test def neg_t1048 = compileFile(negDir, "t1048", xerrors = 1)
@Test def neg_t1843 = compileFile(negDir, "t1843", xerrors = 1)
@Test def neg_t1843_variances = compileFile(negDir, "t1843-variances", xerrors = 1)
@Test def neg_t2994 = compileFile(negDir, "t2994", xerrors = 2)
Expand Down
21 changes: 21 additions & 0 deletions tests/neg/t1048.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
trait T[U] {
def x: T[_ <: U]
}

object T {
def unapply[U](t: T[U]): Option[T[_ <: U]] = Some(t.x)
}

object Test {
def f[W](t: T[W]) = t match {
case T(T(_)) => ()
// Gives:
// t1048.scala:11: error: There is no best instantiation of pattern type T[Any']
// that makes it a subtype of selector type T[_ <: W].
// Non-variant type variable U cannot be uniquely instantiated.
// case T(T(_)) => ()
// ^
// one error found
}
}

14 changes: 0 additions & 14 deletions tests/pos/t1048.scala

This file was deleted.