Skip to content

Don't generate illegal types when clarifying implicit errors #14068

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
Dec 13, 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
22 changes: 14 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,26 @@ object Implicits:
val ctx1 = ctx.fresh.setExploreTyperState()
ctx1.typerState.constraint = constraint
inContext(ctx1) {
val map = new TypeMap {
def apply(t: Type): Type = t match {
val map = new TypeMap:
def apply(t: Type): Type = t match
case t: TypeParamRef =>
constraint.entry(t) match {
case NoType => t
case bounds: TypeBounds => TypeComparer.fullBounds(t)
constraint.entry(t) match
case NoType | _: TypeBounds => t
case t1 => t1
}
case t: TypeVar =>
t.instanceOpt.orElse(apply(t.origin))
case _ =>
mapOver(t)
}
}

override def mapArgs(args: List[Type], tparams: List[ParamInfo]) =
args.mapConserve {
case t: TypeParamRef =>
constraint.entry(t) match
case bounds: TypeBounds => TypeComparer.fullBounds(t)
case _ => this(t)
case t => this(t)
}
end map
map(tp)
}

Expand Down
16 changes: 16 additions & 0 deletions tests/neg/i13987.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sealed trait Xa[T]
sealed trait Mu[T] extends Xa[T]
object Xa {
// bad
implicit def convertMu[X[x] <: Xa[x], A, B](implicit t: X[A] with Xa[A]): X[B] = t.asInstanceOf[X[B]]
// good
// implicit def convertMu[X[x] <: Xa[x], A, B](implicit t: X[A] with Mu[A]): X[B] = t.asInstanceOf[X[B]]
}
object Mu {
implicit def mu: Mu[Int] = new Mu[Int] {}
}

object App extends App {
def constrain(a: Mu[Long]): Unit = println(a)
constrain(Xa.convertMu) // error
}