Skip to content

Commit 6b04f15

Browse files
committed
Tighten condition when to emit a "unary method cannot have parameters" warning
Also, use a more precise term in the error message. Unary method generally means something entirely different! Generally, was it really worth it to go into so much trouble for a warning for an edge case? The previous fix overshot, excluding things that are OK. I still believe it would have been better to do nothing aboit #9142. The time we have already spent on this and the number of code lines we use to address the issue is in no relation to the benefit of the fix.
1 parent 5939849 commit 6b04f15

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ object RefChecks {
10391039
val what =
10401040
if tpe.paramNames.isEmpty then "empty parameter list.\n\nPossible fix: remove the `()` arguments."
10411041
else "parameters"
1042-
report.warning(s"Unary method cannot take $what", sym.sourcePos)
1042+
report.warning(s"unary_<op> method cannot take $what", sym.sourcePos)
10431043
case tpe: PolyType =>
10441044
checkParameters(tpe.resType)
10451045
case _ =>
@@ -1057,7 +1057,13 @@ object RefChecks {
10571057
case tpe: PolyType =>
10581058
checkExtensionParameters(tpe.resType)
10591059

1060-
if sym.name.startsWith(nme.UNARY_PREFIX.toString) then
1060+
def isUnaryPrefixName(name: Name) = name match
1061+
case name: SimpleName =>
1062+
name.startsWith("unary_") && nme.raw.isUnary(name.drop(6))
1063+
case _ =>
1064+
false
1065+
1066+
if isUnaryPrefixName(sym.name) then
10611067
if sym.is(Extension) || sym.name.is(ExtMethName) then
10621068
// if is method from `extension` or value class
10631069
checkExtensionParameters(sym.info)

tests/neg-custom-args/fatal-warnings/i9241.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class Baz private (val x: Int) extends AnyVal {
1616
def unary_- : Baz = ???
1717
def unary_+[T] : Baz = ???
1818
def unary_!() : Baz = ??? // error
19+
def `unary_!_=`() : Baz = ??? // ok
1920
def unary_~(using Int) : Baz = ???
2021
}
2122

0 commit comments

Comments
 (0)