Skip to content

Commit

Permalink
Awesome warning for named arg clash with named tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Oct 22, 2024
1 parent 1e8f606 commit 85ef7c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
15 changes: 9 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1054,14 +1054,17 @@ object Parsers {
else {
val opInfo = opStack.head
val opPrec = precedence(opInfo.operator.name)
if (prec < opPrec || leftAssoc && prec == opPrec) {
if prec < opPrec || leftAssoc && prec == opPrec then
opStack = opStack.tail
recur {
atSpan(opInfo.operator.span union opInfo.operand.span union top.span) {
recur:
atSpan(opInfo.operator.span union opInfo.operand.span union top.span):
def deprecateInfixNamedArg(t: Tree): Unit = t match
case Tuple(ts) => ts.foreach(deprecateInfixNamedArg)
case Parens(t) => deprecateInfixNamedArg(t)
case t: Assign => report.deprecationWarning(InfixNamedArgDeprecation(), t.srcPos)
case _ =>
deprecateInfixNamedArg(top)
InfixOp(opInfo.operand, opInfo.operator, top)
}
}
}
else top
}
recur(top)
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case UnusedSymbolID // errorNumber: 198
case TailrecNestedCallID //errorNumber: 199 - unused in LTS
case FinalLocalDefID // errorNumber: 200
case NonNamedArgumentInJavaAnnotationID // errorNumber: 201 - unused in LTS
case QuotedTypeMissingID // errorNumber: 202 - unused in LTS
case AmbiguousNamedTupleAssignmentID // errorNumber: 203 - unused in LTS
case DeprecatedNamedInfixArgID // errorNumber: 204 - used ONLY in LTS

def errorNumber = ordinal - 1

Expand Down
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3150,3 +3150,12 @@ object UnusedSymbol {
def privateMembers(using Context): UnusedSymbol = new UnusedSymbol(i"unused private member")
def patVars(using Context): UnusedSymbol = new UnusedSymbol(i"unused pattern variable")
}

class InfixNamedArgDeprecation()(using Context)
extends Message(DeprecatedNamedInfixArgID):
def kind = MessageKind.PotentialIssue
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."""
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

class C {
def f = 42 + (x = 1) // warn
def multi(x: Int, y: Int): Int = x + y
def g = new C() `multi` (x = 42, y = 27) // warn // warn
}

0 comments on commit 85ef7c0

Please sign in to comment.