Skip to content

Commit 626ebf2

Browse files
authored
Merge pull request #3198 from dotty-staging/fix-#3168
Fix #3168: Don't insert apply if tree has method type
2 parents 3c45d43 + c6b65cd commit 626ebf2

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
17991799
*/
18001800
def tryInsertApplyOrImplicit(tree: Tree, pt: ProtoType)(fallBack: => Tree)(implicit ctx: Context): Tree = {
18011801

1802+
def isMethod(tree: Tree) = tree.tpe match {
1803+
case ref: TermRef => ref.denot.alternatives.forall(_.info.widen.isInstanceOf[MethodicType])
1804+
case _ => false
1805+
}
1806+
18021807
def isSyntheticApply(tree: Tree): Boolean = tree match {
18031808
case tree: Select => tree.getAttachment(InsertedApply).isDefined
18041809
case Apply(fn, _) => fn.getAttachment(InsertedApply).isDefined
@@ -1821,7 +1826,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
18211826
pt.markAsDropped()
18221827
tree
18231828
case _ =>
1824-
if (isApplyProto(pt) || isSyntheticApply(tree)) tryImplicit
1829+
if (isApplyProto(pt) || isMethod(tree) || isSyntheticApply(tree)) tryImplicit
18251830
else tryEither(tryApply(_))((_, _) => tryImplicit)
18261831
}
18271832
}

tests/neg/insertapply.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class C {
2+
def apply: C
3+
}
4+
object Test {
5+
(new C)(22) // error: does not take parameters
6+
}

tests/pos/i3168.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Test {
2+
class C {
3+
def foo(x: Int) = 1
4+
def foo(x: Double) = 2
5+
}
6+
7+
implicit class COps(val x: C) {
8+
def foo(x: String) = 3
9+
}
10+
11+
def test: Unit = {
12+
(new C).foo("Hello")
13+
}
14+
}

tests/pos/i3189.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/run/i3189.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Test[A](action: A => A) {
2+
def this() = this(a => a)
3+
def go(x: A) = action(x)
4+
}
5+
6+
object Test extends App {
7+
assert(new Test[Int]().go(3) == 3)
8+
}
9+

0 commit comments

Comments
 (0)