Skip to content

Commit 7784987

Browse files
committed
Deprecation warnings for operators without alpha
Operators without an alpha annotation give a deprecation warning -strict mode.
1 parent 45cc854 commit 7784987

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
111111

112112
private def processMemberDef(tree: Tree)(implicit ctx: Context): tree.type = {
113113
val sym = tree.symbol
114+
Checking.checkValidOperator(sym)
114115
sym.transformAnnotations(transformAnnot)
115116
sym.defTree = tree
116117
tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ object Checking {
274274
}
275275
}
276276

277+
/** If `sym` has an operator name, check that it has an @alpha annotation under -strict */
278+
def checkValidOperator(sym: Symbol)(implicit ctx: Context): Unit =
279+
sym.name.toTermName match {
280+
case name: SimpleName
281+
if name.exists(isOperatorPart) &&
282+
!sym.getAnnotation(defn.AlphaAnnot).isDefined &&
283+
!sym.is(Synthetic) &&
284+
!name.isConstructorName &&
285+
ctx.settings.strict.value =>
286+
ctx.deprecationWarning(
287+
i"$sym has an operator name; it should come with an @alpha annotation", sym.sourcePos)
288+
case _ =>
289+
}
290+
277291
/** Check that `info` of symbol `sym` is not cyclic.
278292
* @pre sym is not yet initialized (i.e. its type is a Completer).
279293
* @return `info` where every legal F-bounded reference is proctected

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ class CompilationTests extends ParallelTesting {
180180
"tests/neg-custom-args/toplevel-samesource/nested/S.scala"),
181181
defaultOptions),
182182
compileFile("tests/neg-custom-args/i6300.scala", allowDeepSubtypes),
183-
compileFile("tests/neg-custom-args/infix.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings"))
183+
compileFile("tests/neg-custom-args/infix.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings")),
184+
compileFile("tests/neg-custom-args/alpha.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings"))
184185
).checkExpectedErrors()
185186
}
186187

tests/neg-custom-args/alpha.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Compile with -strict -Xfatal-warnings -deprecation
2+
import scala.annotation.alpha
3+
class & { // error
4+
5+
@alpha("op") def *(x: Int): Int = ??? // OK
6+
def / (x: Int): Int // error
7+
val frozen_& : Int = ??? // error
8+
object some_??? // error
9+
}

0 commit comments

Comments
 (0)