Skip to content

Fix ambiguity errors with polymorphic implicits #963

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
Nov 18, 2015
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
5 changes: 5 additions & 0 deletions src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,11 @@ object Types {

protected def computeSignature(implicit ctx: Context) = resultSignature

def isPolymorphicMethodType: Boolean = resType match {
case _: MethodType => true
case _ => false
}

def instantiate(argTypes: List[Type])(implicit ctx: Context): Type =
resultType.substParams(this, argTypes)

Expand Down
39 changes: 28 additions & 11 deletions src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -848,17 +848,22 @@ trait Applications extends Compatibility { self: Typer =>
else (sym1 is Module) && isDerived(sym1.companionClass, sym2)

/** Is alternative `alt1` with type `tp1` as specific as alternative
* `alt2` with type `tp2` ? This is the case if
* `alt2` with type `tp2` ?
*
* 1. `tp2` is a method or poly type but `tp1` isn't, or `tp1` is nullary.
* 2. `tp2` and `tp1` are method or poly types and `tp2` can be applied to the parameters of `tp1`.
* 3. Neither `tp1` nor `tp2` are method or poly types and `tp1` is compatible with `tp2`.
* 1. A method `alt1` of type (p1: T1, ..., pn: Tn)U is as specific as `alt2`
* if `alt2` is applicable to arguments (p1, ..., pn) of types T1,...,Tn
* or if `alt1` is nullary.
* 2. A polymorphic member of type [a1 >: L1 <: U1, ..., an >: Ln <: Un]T is as
* specific as `alt2` of type `tp2` if T is as specific as `tp2` under the
* assumption that for i = 1,...,n each ai is an abstract type name bounded
* from below by Li and from above by Ui.
* 3. A member of any other type `tp1` is:
* a. always as specific as a method or a polymorphic method.
* b. as specific as a member of any other type `tp2` if `tp1` is compatible
* with `tp2`.
*/
def isAsSpecific(alt1: TermRef, tp1: Type, alt2: TermRef, tp2: Type): Boolean = ctx.traceIndented(i"isAsSpecific $tp1 $tp2", overload) { tp1 match {
case tp1: PolyType =>
val tparams = ctx.newTypeParams(alt1.symbol, tp1.paramNames, EmptyFlags, tp1.instantiateBounds)
isAsSpecific(alt1, tp1.instantiate(tparams map (_.typeRef)), alt2, tp2)
case tp1: MethodType =>
case tp1: MethodType => // (1)
def repeatedToSingle(tp: Type): Type = tp match {
case tp @ ExprType(tp1) => tp.derivedExprType(repeatedToSingle(tp1))
case _ => if (tp.isRepeatedParam) tp.argTypesHi.head else tp
Expand All @@ -868,10 +873,22 @@ trait Applications extends Compatibility { self: Typer =>
else tp1.paramTypes
isApplicable(alt2, formals1, WildcardType) ||
tp1.paramTypes.isEmpty && tp2.isInstanceOf[MethodOrPoly]
case _ =>
case tp1: PolyType => // (2)
val tparams = ctx.newTypeParams(alt1.symbol, tp1.paramNames, EmptyFlags, tp1.instantiateBounds)
isAsSpecific(alt1, tp1.instantiate(tparams map (_.typeRef)), alt2, tp2)
case _ => // (3)
tp2 match {
case tp2: MethodOrPoly => true
case _ => isCompatible(tp1, tp2)
case tp2: MethodType => true // (3a)
case tp2: PolyType if tp2.isPolymorphicMethodType => true // (3a)
case tp2: PolyType => // (3b)
val nestedCtx = ctx.fresh.setExploreTyperState

{
implicit val ctx: Context = nestedCtx
isCompatible(tp1, constrained(tp2).resultType)
}
case _ => // (3b)
isCompatible(tp1, tp2)
}
}}

Expand Down
1 change: 1 addition & 0 deletions tests/run/implicits_poly.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barFoo
14 changes: 14 additions & 0 deletions tests/run/implicits_poly.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo[A](val x: String)
class Bar[A](x: String) extends Foo[A](x)

object Test {
implicit def anyRefFoo[A <: AnyRef]: Foo[A] = new Foo("anyRefFoo")
implicit def fooFoo[A]: Foo[Foo[A]] = new Foo("fooFoo")
implicit def barFoo[A]: Bar[Foo[A]] = new Bar("barFoo")

def getFooFoo(implicit ev: Foo[Foo[Int]]) = ev

def main(args: Array[String]): Unit = {
println(getFooFoo.x)
}
}