Skip to content

Commit

Permalink
Replace singleton bounds before doing implicit search
Browse files Browse the repository at this point in the history
Before searching for implicit arguments, if a the constraint
contains a type parameter
```
   P >: A <: B
```
where `P` occurs in the searched-for type and `A =:= B`, change the constraint
to
```
   P := B
```
instead. This improves the implicit search by making the searched-for type
have fewer uninstantiated type variables.
  • Loading branch information
odersky committed May 6, 2019
1 parent d78b8c0 commit 6d87bf5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ object Inferencing {
}
}

/** For all type parameters occurring in `tp`:
* If the bounds of `tp` in the current constraint are equal wrt =:=,
* instantiate the type parameter to the lower bound's approximation
* (approximation because of possible F-bounds).
*/
def replaceSingletons(tp: Type)(implicit ctx: Context): Unit = {
val tr = new TypeTraverser {
def traverse(tp: Type): Unit = {
tp match {
case param: TypeParamRef =>
val constraint = ctx.typerState.constraint
if (constraint.contains(param) &&
(constraint.fullUpperBound(param) frozen_<:< constraint.fullLowerBound(param))) {
typr.println(i"replace singleton $param := ${constraint.fullLowerBound(param)}")
ctx.typerState.constraint = constraint.replace(param,
ctx.typeComparer.approximation(param, fromBelow = true))
}
case _ =>
}
traverseChildren(tp)
}
}
tr.traverse(tp)
}

/** If `tree` has a type lambda type, infer its type parameters by comparing with expected type `pt` */
def inferTypeParams(tree: Tree, pt: Type)(implicit ctx: Context): Tree = tree.tpe match {
case tl: TypeLambda =>
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,11 @@ class Typer extends Namer
def adaptNoArgsImplicitMethod(wtp: MethodType): Tree = {
assert(wtp.isImplicitMethod)
val tvarsToInstantiate = tvarsInParams(tree, locked).distinct
wtp.paramInfos.foreach(instantiateSelected(_, tvarsToInstantiate))
def instantiate(tp: Type): Unit = {
instantiateSelected(tp, tvarsToInstantiate)
replaceSingletons(tp)
}
wtp.paramInfos.foreach(instantiate)
val constr = ctx.typerState.constraint

def dummyArg(tp: Type) = untpd.Ident(nme.???).withTypeUnchecked(tp)
Expand Down

0 comments on commit 6d87bf5

Please sign in to comment.