Skip to content

Take @targetName into account when resolving extension methods #16487

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 1 commit into from
Dec 12, 2022
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
17 changes: 14 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,17 @@ object ExtensionMethods {
val companion = imeth.owner.companionModule
val companionInfo = companion.info
val candidates = companionInfo.decl(extensionName(imeth)).alternatives
val matching =
// See the documentation of `memberSignature` to understand why `.stripPoly.ensureMethodic` is needed here.
candidates filter (c => FullParameterization.memberSignature(c.info) == imeth.info.stripPoly.ensureMethodic.signature)
def matches(candidate: SingleDenotation) =
FullParameterization.memberSignature(candidate.info) == imeth.info.stripPoly.ensureMethodic.signature
// See the documentation of `memberSignature` to understand why `.stripPoly.ensureMethodic` is needed here.
&& (if imeth.targetName == imeth.name then
// imeth does not have a @targetName annotation, candidate should not have one either
candidate.symbol.targetName == candidate.symbol.name
else
// imeth has a @targetName annotation, candidate's target name must match
imeth.targetName == candidate.symbol.targetName
)
val matching = candidates.filter(matches)
assert(matching.nonEmpty,
i"""no extension method found for:
|
Expand All @@ -203,6 +211,9 @@ object ExtensionMethods {
| Candidates (signatures normalized):
|
| ${candidates.map(c => s"${c.name}:${c.info.signature}:${FullParameterization.memberSignature(c.info)}").mkString("\n")}""")
if matching.tail.nonEmpty then
// this case will report a "have the same erasure" error later at erasure pahse
report.log(i"mutiple extension methods match $imeth: ${candidates.map(c => i"${c.name}:${c.info}")}")
matching.head.symbol.asTerm
}
}
6 changes: 6 additions & 0 deletions tests/neg/i16464.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

implicit final class SomeOps(e: Int) extends AnyVal:
def -(other: Seq[Int]) = List(1)
def -(other: Seq[Long]) = List(2) // error: double definition

def main(): Unit = 1 - Seq.empty[Int]
26 changes: 26 additions & 0 deletions tests/pos/i16464.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.annotation.targetName

object test1:
implicit final class SomeOps(e: Int) extends AnyVal:
@targetName("a")
def -(other: Seq[Int]) = List(1)
@targetName("b")
def -(other: Seq[Long]) = List(2)

def main(): Unit = 1 - Seq.empty[Int]

object test2:
implicit final class SomeOps(e: Int) extends AnyVal:
@targetName("a")
def -(other: Seq[Int]) = List(1)
def -(other: Seq[Long]) = List(2)

def main(): Unit = 1 - Seq.empty[Int]

object test3:
implicit final class SomeOps(e: Int) extends AnyVal:
def -(other: Seq[Int]) = List(1)
@targetName("b")
def -(other: Seq[Long]) = List(2)

def main(): Unit = 1 - Seq.empty[Int]
10 changes: 10 additions & 0 deletions tests/run/i16464.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.annotation.targetName

implicit final class SomeOps(e: Int) extends AnyVal:
@targetName("a")
def -(other: Seq[Int]) = List(1)
@targetName("b")
def -(other: Seq[Long]) = List(2)

@main
def Test(): Unit = 1 - Seq.empty[Int]