Skip to content

Add missing substitution for pattern-bound variables #11543

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 2 commits into from
Feb 26, 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
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ object Inferencing {
def maximizeType(tp: Type, span: Span, fromScala2x: Boolean)(using Context): List[Symbol] = {
Stats.record("maximizeType")
val vs = variances(tp)
val patternBound = new mutable.ListBuffer[Symbol]
val patternBindings = new mutable.ListBuffer[(Symbol, TypeParamRef)]
vs foreachBinding { (tvar, v) =>
if (v == 1) tvar.instantiate(fromBelow = false)
else if (v == -1) tvar.instantiate(fromBelow = true)
Expand All @@ -406,11 +406,17 @@ object Inferencing {
// Instead, we simultaneously add them later on.
val wildCard = newPatternBoundSymbol(UniqueName.fresh(tvar.origin.paramName), bounds, span, addToGadt = false)
tvar.instantiateWith(wildCard.typeRef)
patternBound += wildCard
patternBindings += ((wildCard, tvar.origin))
}
}
}
val res = patternBound.toList
val res = patternBindings.toList.map { (boundSym, _) =>
// substitute bounds of pattern bound variables to deal with possible F-bounds
for (wildCard, param) <- patternBindings do
boundSym.info = boundSym.info.substParam(param, wildCard.typeRef)
boundSym
}

// We add the created symbols to GADT constraint here.
if (res.nonEmpty) ctx.gadt.addToConstraint(res)
res
Expand Down
16 changes: 16 additions & 0 deletions tests/pos-deep-subtype/i9759.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sealed trait Expr[A <: Expr[A]]

case class Equals[A <: Value[A]](lhs: Expr[A], rhs: Expr[A]) extends Expr[Value.Boolean]

sealed trait Value[A <: Value[A]] extends Expr[A]
object Value {
case class String(value: java.lang.String) extends Value[String]
case class Int(value: Long) extends Value[Int]
case class Boolean(value: java.lang.Boolean) extends Value[Boolean]
}

object Main extends App {
def evaluate[A <: Value[A]](expr: Expr[A]): A = expr match {
case Equals(Value.String(lhs), Value.String(rhs)) => Value.Boolean(lhs == rhs)
}
}