Skip to content

Commit

Permalink
Tweak ExtensionNullifiedByMember
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Dec 29, 2024
1 parent 4d3f757 commit 6bd8f60
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2504,12 +2504,15 @@ class ExtensionNullifiedByMember(method: Symbol, target: Symbol)(using Context)
extends Message(ExtensionNullifiedByMemberID):
def kind = MessageKind.PotentialIssue
def msg(using Context) =
i"""Extension method ${hl(method.name.toString)} will never be selected
|because ${hl(target.name.toString)} already has a member with the same name and compatible parameter types."""
val targetName = hl(target.name.toString)
i"""Extension method ${hl(method.name.toString)} will never be selected from type $targetName
|because $targetName already has a member with the same name and compatible parameter types."""
def explain(using Context) =
i"""An extension method can be invoked as a regular method, but if that is intended,
i"""Although extensions can be overloaded, they do not overload existing member methods.
|An extension method can be invoked as a regular method, but if that is the intended usage,
|it should not be defined as an extension.
|Although extensions can be overloaded, they do not overload existing member methods."""
|
|The extension may be invoked as though selected from an arbitrary type if conversions are in play."""

class TraitCompanionWithMutableStatic()(using Context)
extends SyntaxMsg(TraitCompanionWithMutableStaticID) {
Expand Down
14 changes: 14 additions & 0 deletions tests/warn/i22267.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- [E194] Potential Issue Warning: tests/warn/i22267.scala:13:26 -------------------------------------------------------
13 | extension (self: C) def m(n: Double): Unit = println(2->n) // warn
| ^
| Extension method m will never be selected from type C
| because C already has a member with the same name and compatible parameter types.
|--------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Although extensions can be overloaded, they do not overload existing member methods.
| An extension method can be invoked as a regular method, but if that is the intended usage,
| it should not be defined as an extension.
|
| The extension may be invoked as though selected from an arbitrary type if conversions are in play.
--------------------------------------------------------------------------------------------------------------------
28 changes: 28 additions & 0 deletions tests/warn/i22267.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//> using options -explain

import language.implicitConversions

case class M[T](value: T)
given [T]: Conversion[M[T], T] = _.value
class C:
def m(n: Double): Unit = println(0->n)
object C:
given Ops = Ops()
class Ops:
extension (self: C) def m(n: Int): Unit = println(1->n)
extension (self: C) def m(n: Double): Unit = println(2->n) // warn
extension (self: C) def m(s: String): Unit = println(3->s)

@main def test() =
val c = M(C())
def i = 42
def pi = 3.14
c.value.m(i)
c.value.m(pi)
c.m(i) // conversion
c.m(pi) // conversion
c.m("hello, world") // extension
//m(c)(pi)
val c0 = C()
c0.m(pi)
c0.m("hello, world")

0 comments on commit 6bd8f60

Please sign in to comment.