Skip to content

Awesome warning for named arg clash with named tuple #21826

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

Closed
Closed
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
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
}
Loading