Skip to content

Fix #3168: Don't insert apply if tree has method type #3198

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 4 commits into from
Sep 29, 2017
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
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
*/
def tryInsertApplyOrImplicit(tree: Tree, pt: ProtoType)(fallBack: => Tree)(implicit ctx: Context): Tree = {

def isMethod(tree: Tree) = tree.tpe match {
case ref: TermRef => ref.denot.alternatives.forall(_.info.widen.isInstanceOf[MethodicType])
case _ => false
}

def isSyntheticApply(tree: Tree): Boolean = tree match {
case tree: Select => tree.getAttachment(InsertedApply).isDefined
case Apply(fn, _) => fn.getAttachment(InsertedApply).isDefined
Expand All @@ -1821,7 +1826,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
pt.markAsDropped()
tree
case _ =>
if (isApplyProto(pt) || isSyntheticApply(tree)) tryImplicit
if (isApplyProto(pt) || isMethod(tree) || isSyntheticApply(tree)) tryImplicit
else tryEither(tryApply(_))((_, _) => tryImplicit)
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/insertapply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class C {
def apply: C
}
object Test {
(new C)(22) // error: does not take parameters
}
14 changes: 14 additions & 0 deletions tests/pos/i3168.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Test {
class C {
def foo(x: Int) = 1
def foo(x: Double) = 2
}

implicit class COps(val x: C) {
def foo(x: String) = 3
}

def test: Unit = {
(new C).foo("Hello")
}
}
3 changes: 0 additions & 3 deletions tests/pos/i3189.scala

This file was deleted.

9 changes: 9 additions & 0 deletions tests/run/i3189.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Test[A](action: A => A) {
def this() = this(a => a)
def go(x: A) = action(x)
}

object Test extends App {
assert(new Test[Int]().go(3) == 3)
}