Skip to content

Commit 8a725f3

Browse files
committed
Fix #6705: Improve adaptation of overloaded terms
If a term is overloaded and the expected type is not an application, prefer the parameterless alternative.
1 parent bc81953 commit 8a725f3

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ object ProtoTypes {
222222
class UnapplySelectionProto(name: Name) extends SelectionProto(name, WildcardType, NoViewsAllowed, true)
223223

224224
trait ApplyingProto extends ProtoType // common trait of ViewProto and FunProto
225-
trait FunOrPolyProto extends ProtoType // common trait of PolyProto and FunProto
225+
trait FunOrPolyProto extends ProtoType { // common trait of PolyProto and FunProto
226+
def isGivenApply: Boolean = false
227+
}
226228

227229
class FunProtoState {
228230

@@ -244,7 +246,7 @@ object ProtoTypes {
244246
* [](args): resultType
245247
*/
246248
case class FunProto(args: List[untpd.Tree], resType: Type)(typer: Typer,
247-
val isGivenApply: Boolean, state: FunProtoState = new FunProtoState)(implicit val ctx: Context)
249+
override val isGivenApply: Boolean, state: FunProtoState = new FunProtoState)(implicit val ctx: Context)
248250
extends UncachedGroundType with ApplyingProto with FunOrPolyProto {
249251
override def resultType(implicit ctx: Context): Type = resType
250252

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,18 +2421,26 @@ class Typer extends Namer
24212421
case alt :: Nil =>
24222422
readaptSimplified(tree.withType(alt))
24232423
case Nil =>
2424+
// If alternative matches, there are still two ways to recover:
2425+
// 1. If context is an application, try to insert and apply or implicit
2426+
// 2. If context is not an application, pick a alternative that does
2427+
// not take parameters.
24242428
def noMatches =
24252429
errorTree(tree, NoMatchingOverload(altDenots, pt)(err))
24262430
def hasEmptyParams(denot: SingleDenotation) = denot.info.paramInfoss == ListOfNil
24272431
pt match {
2428-
case pt: FunProto if !pt.isGivenApply =>
2429-
// insert apply or convert qualifier only for a regular application
2432+
case pt: FunOrPolyProto if !pt.isGivenApply =>
2433+
// insert apply or convert qualifier, but only for a regular application
24302434
tryInsertApplyOrImplicit(tree, pt, locked)(noMatches)
24312435
case _ =>
2432-
if (altDenots exists (_.info.paramInfoss == ListOfNil))
2433-
typed(untpd.Apply(untpd.TypedSplice(tree), Nil), pt, locked)
2434-
else
2435-
noMatches
2436+
alts.filter(_.info.isParameterless) match {
2437+
case alt :: Nil => readaptSimplified(tree.withType(alt))
2438+
case _ =>
2439+
if (altDenots exists (_.info.paramInfoss == ListOfNil))
2440+
typed(untpd.Apply(untpd.TypedSplice(tree), Nil), pt, locked)
2441+
else
2442+
noMatches
2443+
}
24362444
}
24372445
case alts =>
24382446
if (tree.tpe.isErroneous || pt.isErroneous) tree.withType(UnspecifiedErrorType)

tests/pos/i6705.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait StringTempl {
2+
def mkString: String
3+
def mkString(x: String): String
4+
}
5+
6+
7+
object Test {
8+
def (x: String) shouldBe(y: String): Boolean = ???
9+
10+
def test(tmpl: StringTempl): Unit = {
11+
tmpl.mkString shouldBe "hello" // error
12+
tmpl.mkString(", world") shouldBe "hello, world"
13+
}
14+
}

0 commit comments

Comments
 (0)