Skip to content

Commit 30dba8c

Browse files
authored
Merge pull request #2896 from dotty-staging/fix#-2300
Fix #2300: Try to heal subtype failures by instantiating type variables
2 parents 611f8ee + 6782df8 commit 30dba8c

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
21912191
case SearchSuccess(inferred, _, _, _) =>
21922192
adapt(inferred, pt)(ctx.retractMode(Mode.ImplicitsEnabled))
21932193
case failure: SearchFailure =>
2194+
val prevConstraint = ctx.typerState.constraint
21942195
if (pt.isInstanceOf[ProtoType] && !failure.isInstanceOf[AmbiguousImplicits]) tree
2196+
else if (isFullyDefined(wtp, force = ForceDegree.all) &&
2197+
ctx.typerState.constraint.ne(prevConstraint)) adapt(tree, pt, original)
21952198
else err.typeMismatch(tree, pt, failure)
21962199
}
21972200
}

tests/neg/i1650.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
test4(test4$default$1) // error
2+
test4(test4$default$1)
33
def test4[T[P]](x: T[T[List[T[X forSome { type X }]]]]) = ??? // error // error
44
def test4$default$1[T[P]]: T[Int] = ???
55
}

tests/pos/i2300.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
object hlists {
2+
sealed abstract class HList {
3+
type Merge[L <: HList] <: HList
4+
5+
def merge[L <: HList](l: L): Merge[L]
6+
}
7+
final case class HCons[H, T <: HList](head : H, tail : T) extends HList {
8+
type Merge[L <: HList] = HCons[H, tail.Merge[L]]
9+
10+
def merge[L <: HList](l: L): Merge[L] = HCons(head, tail.merge(l))
11+
}
12+
sealed trait HNil extends HList {
13+
type Merge[L <: HList] = L
14+
15+
def merge[L <: HList](l: L): Merge[L] = l
16+
}
17+
final val HNil: HNil = { case object HNil extends HNil; HNil }
18+
}
19+
20+
object Test {
21+
import hlists._
22+
23+
val merged: HCons[Int,HCons[String,HNil]] = {
24+
HCons(42, HNil) merge HCons("foo", HNil)
25+
}
26+
}

tests/repl/errmsgs.check

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ scala> val a: Inv[String] = new Inv(new Inv(1))
3131
-- [E007] Type Mismatch Error: <console>:5:33 ----------------------------------
3232
5 |val a: Inv[String] = new Inv(new Inv(1))
3333
| ^^^^^
34-
| found: Inv[T]
35-
| required: String
36-
|
37-
| where: T is a type variable with constraint >: Int(1)
34+
| found: Inv[Int]
35+
| required: String
36+
|
3837
scala> val b: Inv[String] = new Inv(1)
3938
-- [E007] Type Mismatch Error: <console>:5:29 ----------------------------------
4039
5 |val b: Inv[String] = new Inv(1)

0 commit comments

Comments
 (0)