Skip to content

Commit 6a2663b

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 7183d75 commit 6a2663b

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
@@ -667,18 +667,29 @@ trait Checking {
667667
i"Use of implicit conversion ${conv.showLocated}", NoSymbol, posd.sourcePos)
668668
}
669669

670+
private def infixOKSinceFollowedBy(tree: untpd.Tree): Boolean = tree match {
671+
case _: untpd.Block | _: untpd.Match => true
672+
case _ => false
673+
}
674+
670675
/** Check that `tree` is a valid infix operation. That is, if the
671676
* operator is alphanumeric, it must be declared `@infix`.
672677
*/
673678
def checkValidInfix(tree: untpd.InfixOp, app: Tree)(implicit ctx: Context): Unit =
674679
tree.op match {
680+
case _: untpd.BackquotedIdent =>
681+
()
675682
case Ident(name: SimpleName)
676-
if !name.exists(isOperatorPart) && !app.symbol.hasAnnotation(defn.InfixAnnot) && false =>
683+
if !name.exists(isOperatorPart) &&
684+
!app.symbol.hasAnnotation(defn.InfixAnnot) &&
685+
!app.symbol.maybeOwner.is(Scala2x) &&
686+
!infixOKSinceFollowedBy(tree.right) &&
687+
ctx.settings.strict.value =>
677688
ctx.deprecationWarning(
678689
i"""alphanumeric method $name is not declared @infix; should not be used as infix operator.
679-
|The operation can be rewritten automatically under -migration -rewrite""",
690+
|The operation can be rewritten automatically to `$name` under -deprecation -rewrite""",
680691
tree.op.sourcePos)
681-
if (ctx.scala2Mode) {
692+
if (ctx.settings.deprecation.value) {
682693
patch(Span(tree.op.span.start, tree.op.span.start), "`")
683694
patch(Span(tree.op.span.end, tree.op.span.end), "`")
684695
}

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)