Skip to content

Commit e98ee92

Browse files
vitorsvieiraallanrenucci
authored andcommitted
Ref #1589: Add warning messages for migration annotation (#3562)
1 parent 6ade9be commit e98ee92

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public enum ErrorMessageID {
118118
StaticFieldsOnlyAllowedInObjectsID,
119119
CyclicInheritanceID,
120120
UnableToExtendSealedClassID,
121+
SymbolHasUnparsableVersionNumberID,
122+
SymbolChangedSemanticsInVersionID,
121123
UnableToEmitSwitchID,
122124
MissingCompanionForStaticID,
123125
;

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import ErrorMessageID._
2121
import Denotations.SingleDenotation
2222
import dotty.tools.dotc.ast.Trees
2323
import dotty.tools.dotc.ast.untpd.Modifiers
24+
import dotty.tools.dotc.config.ScalaVersion
2425
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
2526
import dotty.tools.dotc.core.SymDenotations.SymDenotation
2627
import scala.util.control.NonFatal
@@ -140,7 +141,7 @@ object messages {
140141
}
141142

142143
case class EmptyCatchBlock(tryBody: untpd.Tree)(implicit ctx: Context)
143-
extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchBlockID) {
144+
extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchBlockID) {
144145
val kind = "Syntax"
145146
val msg =
146147
hl"""|The ${"catch"} block does not contain a valid expression, try
@@ -1973,6 +1974,31 @@ object messages {
19731974
val explanation = "A sealed class or trait can only be extended in the same file as its declaration"
19741975
}
19751976

1977+
case class SymbolHasUnparsableVersionNumber(symbol: Symbol, migrationMessage: String)(implicit ctx: Context)
1978+
extends Message(SymbolHasUnparsableVersionNumberID) {
1979+
val kind = "Syntax"
1980+
val msg = hl"${symbol.showLocated} has an unparsable version number: $migrationMessage"
1981+
val explanation =
1982+
hl"""$migrationMessage
1983+
|
1984+
|The ${symbol.showLocated} is marked with ${"@migration"} indicating it has changed semantics
1985+
|between versions and the ${"-Xmigration"} settings is used to warn about constructs
1986+
|whose behavior may have changed since version change."""
1987+
}
1988+
1989+
case class SymbolChangedSemanticsInVersion(
1990+
symbol: Symbol,
1991+
migrationVersion: ScalaVersion
1992+
)(implicit ctx: Context) extends Message(SymbolChangedSemanticsInVersionID) {
1993+
val kind = "Syntax"
1994+
val msg = hl"${symbol.showLocated} has changed semantics in version $migrationVersion"
1995+
val explanation = {
1996+
hl"""The ${symbol.showLocated} is marked with ${"@migration"} indicating it has changed semantics
1997+
|between versions and the ${"-Xmigration"} settings is used to warn about constructs
1998+
|whose behavior may have changed since version change."""
1999+
}
2000+
}
2001+
19762002
case class UnableToEmitSwitch()(implicit ctx: Context)
19772003
extends Message(UnableToEmitSwitchID) {
19782004
val kind = "Syntax"

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -685,17 +685,15 @@ object RefChecks {
685685
}
686686
// Similar to deprecation: check if the symbol is marked with @migration
687687
// indicating it has changed semantics between versions.
688-
if (sym.hasAnnotation(defn.MigrationAnnot) && ctx.settings.Xmigration.value != NoScalaVersion) {
689-
val symVersion: scala.util.Try[ScalaVersion] = sym.migrationVersion.get
690-
val changed = symVersion match {
691-
case scala.util.Success(v) =>
692-
ctx.settings.Xmigration.value < v
688+
val xMigrationValue = ctx.settings.Xmigration.value
689+
if (sym.hasAnnotation(defn.MigrationAnnot) && xMigrationValue != NoScalaVersion) {
690+
sym.migrationVersion.get match {
691+
case scala.util.Success(symVersion) if xMigrationValue < symVersion=>
692+
ctx.warning(SymbolChangedSemanticsInVersion(sym, symVersion), pos)
693693
case Failure(ex) =>
694-
ctx.warning(s"${sym.showLocated} has an unparsable version number: ${ex.getMessage()}", pos)
695-
false
694+
ctx.warning(SymbolHasUnparsableVersionNumber(sym, ex.getMessage()), pos)
695+
case _ =>
696696
}
697-
if (changed)
698-
ctx.warning(s"${sym.showLocated} has changed semantics in version $symVersion:\n${sym.migrationMessage.get}")
699697
}
700698
/* (Not enabled yet)
701699
* See an explanation of compileTimeOnly in its scaladoc at scala.annotation.compileTimeOnly.

0 commit comments

Comments
 (0)