Skip to content

Fix #7793: Account for context params preceding normal ones in overloading #8395

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

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 40 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ trait Applications extends Compatibility {
else
args

protected def init(): Unit = methType match {
protected def initWith(mt: Type): Unit = mt match {
case methType: MethodType =>
// apply the result type constraint, unless method type is dependent
val resultApprox = resultTypeApprox(methType)
Expand All @@ -358,6 +358,28 @@ trait Applications extends Compatibility {
else fail(s"$methString does not take parameters")
}

protected def reset(): Unit =
ok = true

/**
* This function tests whether a given method is applicable to the
* given arguments. Context parameters that precede the normal parameters
* pose problems for such a test. E.g.:

* def f(using String)(g: Int => String): Int
* f(x => "2")
* f(using summon[String])(x => "2")

* At the first call site, the context parameter is omitted. For the
* applicability test to pass in that case, we should also test
* the function `f` with its context parameters dropped.
*/
protected def init(): Unit =
initWith(methType)
if !success then
reset()
initWith(stripImplicit(methType))

/** The application was successful */
def success: Boolean = ok

Expand Down Expand Up @@ -688,6 +710,12 @@ trait Applications extends Compatibility {
private var typedArgBuf = new mutable.ListBuffer[Tree]
private var liftedDefs: mutable.ListBuffer[Tree] = null
private var myNormalizedFun: Tree = fun
override protected def reset(): Unit =
super.reset()
typedArgBuf = new mutable.ListBuffer[Tree]
liftedDefs = null
myNormalizedFun = fun

init()

def addArg(arg: Tree, formal: Type): Unit =
Expand Down Expand Up @@ -1299,6 +1327,16 @@ trait Applications extends Compatibility {
}
}

/** Drop any implicit parameter section */
def stripImplicit(tp: Type)(using Context): Type = tp match {
case mt: MethodType if mt.isImplicitMethod =>
stripImplicit(resultTypeApprox(mt))
case pt: PolyType =>
pt.derivedLambdaType(pt.paramNames, pt.paramInfos, stripImplicit(pt.resultType))
case _ =>
tp
}

/** Compare owner inheritance level.
* @param sym1 The first owner
* @param sym2 The second owner
Expand Down Expand Up @@ -1466,16 +1504,6 @@ trait Applications extends Compatibility {
else tp
}

/** Drop any implicit parameter section */
def stripImplicit(tp: Type): Type = tp match {
case mt: MethodType if mt.isImplicitMethod =>
stripImplicit(resultTypeApprox(mt))
case pt: PolyType =>
pt.derivedLambdaType(pt.paramNames, pt.paramInfos, stripImplicit(pt.resultType))
case _ =>
tp
}

def compareWithTypes(tp1: Type, tp2: Type) = {
val ownerScore = compareOwner(alt1.symbol.maybeOwner, alt2.symbol.maybeOwner)
def winsType1 = isAsSpecific(alt1, tp1, alt2, tp2)
Expand Down Expand Up @@ -1901,7 +1929,7 @@ trait Applications extends Compatibility {
recur(altFormals.map(_.tail), args1)
case _ =>
}
recur(alts.map(_.widen.firstParamTypes), pt.args)
recur(alts.map(alt => stripImplicit(alt.widen).firstParamTypes), pt.args)
}

private def harmonizeWith[T <: AnyRef](ts: List[T])(tpe: T => Type, adapt: (T, Type) => T)(implicit ctx: Context): List[T] = {
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i7793.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Foo:
def g(f: Int => Int): Int = 1
def g(using String)(f: Int => String): String = "2"

@main def Test =
val m: Foo = ???
given String = "foo"
m.g(x => "2")
m.g(using summon[String])(x => "2")