Skip to content

Commit c39209b

Browse files
committed
Relax infix criterion
- Only checked under -strict - Not checked for symbols coming from Scala-2 - Infix alphanumeric is OK when followed by `{`
1 parent c340b97 commit c39209b

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,18 +660,29 @@ trait Checking {
660660
i"Use of implicit conversion ${conv.showLocated}", NoSymbol, posd.sourcePos)
661661
}
662662

663+
private def infixOKSinceFollowedBy(tree: untpd.Tree): Boolean = tree match {
664+
case _: untpd.Block | _: untpd.Match => true
665+
case _ => false
666+
}
667+
663668
/** Check that `tree` is a valid infix operation. That is, if the
664669
* operator is alphanumeric, it must be declared `@infix`.
665670
*/
666671
def checkValidInfix(tree: untpd.InfixOp, app: Tree)(implicit ctx: Context): Unit =
667672
tree.op match {
673+
case _: untpd.BackquotedIdent =>
674+
()
668675
case Ident(name: SimpleName)
669-
if !name.exists(isOperatorPart) && !app.symbol.hasAnnotation(defn.InfixAnnot) && false =>
676+
if !name.exists(isOperatorPart) &&
677+
!app.symbol.hasAnnotation(defn.InfixAnnot) &&
678+
!app.symbol.maybeOwner.is(Scala2x) &&
679+
!infixOKSinceFollowedBy(tree.right) &&
680+
ctx.settings.strict.value =>
670681
ctx.deprecationWarning(
671682
i"""alphanumeric method $name is not declared @infix; should not be used as infix operator.
672-
|The operation can be rewritten automatically under -migration -rewrite""",
683+
|The operation can be rewritten automatically to `$name` under -deprecation -rewrite""",
673684
tree.op.sourcePos)
674-
if (ctx.scala2Mode) {
685+
if (ctx.settings.deprecation.value) {
675686
patch(Span(tree.op.span.start, tree.op.span.start), "`")
676687
patch(Span(tree.op.span.end, tree.op.span.end), "`")
677688
}

docs/docs/reference/changed-features/operators.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,17 @@ s1 difference s2 // gives a deprecation warning
7878
s1 * s2 // OK
7979
s1.*(s2) // also OK, but unusual
8080
```
81-
Infix operations involving alphanumeric operators that do not carry @infix annotations are deprecated. Infix operations involving symbolic operators are always allowed, so `@infix` is redundant for methods with symbolic names. Infix operations are also allowed
82-
if an alphanumeric operator name is given in backticks (as in the third call of `difference` above).
81+
Infix operations involving alphanumeric operators are deprecated, unless
82+
one of the following conditions holds:
83+
84+
- the operator definition carries an `@infix` annotation, or
85+
- the operator was compiled with Scala 2, or
86+
- the operator is followed by an opening brace.
87+
88+
An alphanumeric operator is an operator consisting entirely of letters, digits, the `$` and `_` characters, or
89+
any unicode character `c` for which `java.lang.Character.isIdentifierPart(c)` returns `true`.
90+
91+
Infix operations involving symbolic operators are always allowed, so `@infix` is redundant for methods with symbolic names.
8392

8493
The @infix annotation can also be given to a type:
8594
```
@@ -116,3 +125,5 @@ The purpose of the `@infix` annotation is to achieve consistency across a code b
116125

117126
can be applied using infix syntax, i.e. `A op B`.
118127

128+
5. To smooth migration to Scala 3.0, alphanumeric operations will only be deprecated from Scala 3.1 onwards,
129+
or if the `-strict` option is given in Dotty/Scala 3.

0 commit comments

Comments
 (0)