Skip to content

Commit

Permalink
Fix named arg deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Oct 22, 2024
1 parent 81291f1 commit 67ba634
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ object Parsers {
def deprecateInfixNamedArg(t: Tree): Unit = t match
case Tuple(ts) => ts.foreach(deprecateInfixNamedArg)
case Parens(t) => deprecateInfixNamedArg(t)
case t: Assign => report.deprecationWarning(em"named argument is deprecated for infix syntax", t.srcPos)
case t: NamedArg => report.deprecationWarning(InfixNamedArgDeprecation(), t.srcPos)
case _ =>
deprecateInfixNamedArg(top)
InfixOp(opInfo.operand, opInfo.operator, top)
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3352,3 +3352,10 @@ final class AmbiguousNamedTupleAssignment(key: Name, value: untpd.Tree)(using Co
|To assign a value, use curly braces: `{${key} = ${value}}`."""

override protected def explain(using Context): String = ""

class InfixNamedArgDeprecation()(using Context) extends SyntaxMsg(DeprecatedNamedInfixArgID):
def msg(using Context) = "Named argument syntax is deprecated for infix application"
def explain(using Context) =
i"""The argument will be parsed as a named tuple in future.
|
|To avoid this warning, either remove the argument names or use dotted selection."""
8 changes: 8 additions & 0 deletions tests/neg/infix-named-args.check
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@
| (x: Byte): Int
| (x: String): String
| match arguments ((x : Int)) (a named tuple)
-- [E007] Type Mismatch Error: tests/neg/infix-named-args.scala:13:18 --------------------------------------------------
13 | def g = this ** 2 // error
| ^
| Found: (2 : Int)
| Required: X
|
| longer explanation available when compiling with `-explain`
there were 6 deprecation warnings; re-run with -deprecation for details
10 changes: 8 additions & 2 deletions tests/neg/infix-named-args.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class C {
class C:
def f = 42 + (x = 1) // error // a named tuple!
def multi(x: Int, y: Int): Int = x + y
def **(x: Int, y: Int): Int = x + y
def g = new C() `multi` (x = 42, y = 27) // werror // werror // not actually a tuple! appearances to the contrary
def h = new C() ** (x = 42, y = 27) // werror // werror
}

type X = (x: Int)

class D(d: Int):
def **(x: X): Int = d * x.x
def f = this ** (x = 2)
def g = this ** 2 // error
7 changes: 7 additions & 0 deletions tests/warn/infix-named-args.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//> using options -deprecation
type X = (x: Int)

class E(e: Int):
def **(x: Int): Int = e * x
def **(x: X): Int = e * x.x
def f = this ** (x = 2) // warn

0 comments on commit 67ba634

Please sign in to comment.