Skip to content

Commit b3ed0f8

Browse files
committed
Don't generate illegal types when clarifying implicit errors
Fixes #13987
1 parent 80ec523 commit b3ed0f8

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,26 @@ object Implicits:
462462
val ctx1 = ctx.fresh.setExploreTyperState()
463463
ctx1.typerState.constraint = constraint
464464
inContext(ctx1) {
465-
val map = new TypeMap {
466-
def apply(t: Type): Type = t match {
465+
val map = new TypeMap:
466+
def apply(t: Type): Type = t match
467467
case t: TypeParamRef =>
468-
constraint.entry(t) match {
469-
case NoType => t
470-
case bounds: TypeBounds => TypeComparer.fullBounds(t)
468+
constraint.entry(t) match
469+
case NoType | _: TypeBounds => t
471470
case t1 => t1
472-
}
473471
case t: TypeVar =>
474472
t.instanceOpt.orElse(apply(t.origin))
475473
case _ =>
476474
mapOver(t)
477-
}
478-
}
475+
476+
override def mapArgs(args: List[Type], tparams: List[ParamInfo]) =
477+
args.mapConserve {
478+
case t: TypeParamRef =>
479+
constraint.entry(t) match
480+
case bounds: TypeBounds => TypeComparer.fullBounds(t)
481+
case _ => this(t)
482+
case t => this(t)
483+
}
484+
end map
479485
map(tp)
480486
}
481487

tests/neg/i13987.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sealed trait Xa[T]
2+
sealed trait Mu[T] extends Xa[T]
3+
object Xa {
4+
// bad
5+
implicit def convertMu[X[x] <: Xa[x], A, B](implicit t: X[A] with Xa[A]): X[B] = t.asInstanceOf[X[B]]
6+
// good
7+
// implicit def convertMu[X[x] <: Xa[x], A, B](implicit t: X[A] with Mu[A]): X[B] = t.asInstanceOf[X[B]]
8+
}
9+
object Mu {
10+
implicit def mu: Mu[Int] = new Mu[Int] {}
11+
}
12+
13+
object App extends App {
14+
def constrain(a: Mu[Long]): Unit = println(a)
15+
constrain(Xa.convertMu) // error
16+
}

0 commit comments

Comments
 (0)