Skip to content

Allow inline matches to bind type variables #5657

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 4 commits into from
Jan 7, 2019
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
33 changes: 29 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,18 @@ object Contexts {
def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(implicit ctx: Context): Boolean
def bounds(sym: Symbol)(implicit ctx: Context): TypeBounds
def contains(sym: Symbol)(implicit ctx: Context): Boolean
def approximation(sym: Symbol, fromBelow: Boolean)(implicit ctx: Context): Type
def debugBoundsDescription(implicit ctx: Context): String
def fresh: GADTMap
def restore(other: GADTMap): Unit
def isEmpty: Boolean
}

final class SmartGADTMap private (
private[this] var myConstraint: Constraint,
private[this] var mapping: SimpleIdentityMap[Symbol, TypeVar],
private[this] var reverseMapping: SimpleIdentityMap[TypeParamRef, Symbol],
private[this] var boundCache: SimpleIdentityMap[Symbol, TypeBounds]
private var myConstraint: Constraint,
private var mapping: SimpleIdentityMap[Symbol, TypeVar],
private var reverseMapping: SimpleIdentityMap[TypeParamRef, Symbol],
private var boundCache: SimpleIdentityMap[Symbol, TypeBounds]
) extends GADTMap with ConstraintHandling[Context] {
import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr}

Expand Down Expand Up @@ -833,13 +836,30 @@ object Contexts {

override def contains(sym: Symbol)(implicit ctx: Context): Boolean = mapping(sym) ne null

override def approximation(sym: Symbol, fromBelow: Boolean)(implicit ctx: Context): Type = {
val res = removeTypeVars(approximation(tvar(sym).origin, fromBelow = fromBelow))
gadts.println(i"approximating $sym ~> $res")
res
}

override def fresh: GADTMap = new SmartGADTMap(
myConstraint,
mapping,
reverseMapping,
boundCache
)

def restore(other: GADTMap): Unit = other match {
case other: SmartGADTMap =>
this.myConstraint = other.myConstraint
this.mapping = other.mapping
this.reverseMapping = other.reverseMapping
this.boundCache = other.boundCache
case _ => ;
}

override def isEmpty: Boolean = mapping.size == 0

// ---- Private ----------------------------------------------------------

private[this] def tvar(sym: Symbol)(implicit ctx: Context): TypeVar = {
Expand Down Expand Up @@ -916,7 +936,12 @@ object Contexts {
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(implicit ctx: Context): Boolean = unsupported("EmptyGADTMap.addBound")
override def bounds(sym: Symbol)(implicit ctx: Context): TypeBounds = null
override def contains(sym: Symbol)(implicit ctx: Context) = false
override def approximation(sym: Symbol, fromBelow: Boolean)(implicit ctx: Context): Type = unsupported("EmptyGADTMap.approximation")
override def debugBoundsDescription(implicit ctx: Context): String = "EmptyGADTMap"
override def fresh = new SmartGADTMap
override def restore(other: GADTMap): Unit = {
if (!other.isEmpty) sys.error("cannot restore a non-empty GADTMap")
}
override def isEmpty: Boolean = true
}
}
18 changes: 11 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ object Implicits {
* @param level The level where the reference was found
* @param tstate The typer state to be committed if this alternative is chosen
*/
case class SearchSuccess(tree: Tree, ref: TermRef, level: Int)(val tstate: TyperState) extends SearchResult with Showable
case class SearchSuccess(tree: Tree, ref: TermRef, level: Int)(val tstate: TyperState, val gstate: GADTMap) extends SearchResult with Showable

/** A failed search */
case class SearchFailure(tree: Tree) extends SearchResult {
Expand Down Expand Up @@ -880,6 +880,7 @@ trait Implicits { self: Typer =>
result0 match {
case result: SearchSuccess =>
result.tstate.commit()
ctx.gadt.restore(result.gstate)
implicits.println(i"success: $result")
implicits.println(i"committing ${result.tstate.constraint} yielding ${ctx.typerState.constraint} in ${ctx.typerState}")
result
Expand Down Expand Up @@ -1004,7 +1005,7 @@ trait Implicits { self: Typer =>
val generated2 =
if (cand.isExtension) Applications.ExtMethodApply(generated1).withType(generated1.tpe)
else generated1
SearchSuccess(generated2, ref, cand.level)(ctx.typerState)
SearchSuccess(generated2, ref, cand.level)(ctx.typerState, ctx.gadt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to combine typerState and GADT map?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping my eye on it. I tried to do that, but couldn't find a clean approach. The problem is we don't want to set fresh typer state when we set fresh GADT state, and vice versa - it's really easiest if the two are separate objects. If current approach turns out to be too clumsy, I'll find a way to merge the two.

}
}}

Expand All @@ -1016,7 +1017,8 @@ trait Implicits { self: Typer =>
SearchFailure(new DivergingImplicit(cand.ref, pt.widenExpr, argument))
else {
val history = ctx.searchHistory.nest(cand, pt)
val result = typedImplicit(cand, contextual)(nestedContext().setNewTyperState().setSearchHistory(history))
val result =
typedImplicit(cand, contextual)(nestedContext().setNewTyperState().setFreshGADTBounds.setSearchHistory(history))
result match {
case res: SearchSuccess =>
ctx.searchHistory.defineBynameImplicit(pt.widenExpr, res)
Expand Down Expand Up @@ -1118,7 +1120,9 @@ trait Implicits { self: Typer =>
result match {
case _: SearchFailure =>
SearchSuccess(ref(defn.Not_value), defn.Not_value.termRef, 0)(
ctx.typerState.fresh().setCommittable(true))
ctx.typerState.fresh().setCommittable(true),
ctx.gadt
)
case _: SearchSuccess =>
NoMatchingImplicitsFailure
}
Expand Down Expand Up @@ -1188,7 +1192,7 @@ trait Implicits { self: Typer =>
// other candidates need to be considered.
ctx.searchHistory.recursiveRef(pt) match {
case ref: TermRef =>
SearchSuccess(tpd.ref(ref).withPos(pos.startPos), ref, 0)(ctx.typerState)
SearchSuccess(tpd.ref(ref).withPos(pos.startPos), ref, 0)(ctx.typerState, ctx.gadt)
case _ =>
val eligible =
if (contextual) ctx.implicits.eligible(wildProto)
Expand Down Expand Up @@ -1428,7 +1432,7 @@ final class SearchRoot extends SearchHistory {
implicitDictionary.get(tpe) match {
case Some((ref, _)) =>
implicitDictionary.put(tpe, (ref, result.tree))
SearchSuccess(tpd.ref(ref).withPos(result.tree.pos), result.ref, result.level)(result.tstate)
SearchSuccess(tpd.ref(ref).withPos(result.tree.pos), result.ref, result.level)(result.tstate, result.gstate)
case None => result
}
}
Expand Down Expand Up @@ -1529,7 +1533,7 @@ final class SearchRoot extends SearchHistory {

val blk = Block(classDef :: inst :: Nil, res)

success.copy(tree = blk)(success.tstate)
success.copy(tree = blk)(success.tstate, success.gstate)
}
}
}
Expand Down
Loading