Skip to content

Ref #1589: Add warning messages for migration annotation #3562

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public enum ErrorMessageID {
StaticFieldsOnlyAllowedInObjectsID,
CyclicInheritanceID,
UnableToExtendSealedClassID,
SymbolHasUnparsableVersionNumberID,
SymbolChangedSemanticsInVersionID,
UnableToEmitSwitchID,
MissingCompanionForStaticID,
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ErrorMessageID._
import Denotations.SingleDenotation
import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.ast.untpd.Modifiers
import dotty.tools.dotc.config.ScalaVersion
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import scala.util.control.NonFatal
Expand Down Expand Up @@ -140,7 +141,7 @@ object messages {
}

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

case class SymbolHasUnparsableVersionNumber(symbol: Symbol, migrationMessage: String)(implicit ctx: Context)
extends Message(SymbolHasUnparsableVersionNumberID) {
val kind = "Syntax"
val msg = hl"${symbol.showLocated} has an unparsable version number: $migrationMessage"
val explanation =
hl"""$migrationMessage
|
|The ${symbol.showLocated} is marked with ${"@migration"} indicating it has changed semantics
|between versions and the ${"-Xmigration"} settings is used to warn about constructs
|whose behavior may have changed since version change."""
}

case class SymbolChangedSemanticsInVersion(
symbol: Symbol,
migrationVersion: ScalaVersion
)(implicit ctx: Context) extends Message(SymbolChangedSemanticsInVersionID) {
val kind = "Syntax"
val msg = hl"${symbol.showLocated} has changed semantics in version $migrationVersion"
val explanation = {
hl"""The ${symbol.showLocated} is marked with ${"@migration"} indicating it has changed semantics
|between versions and the ${"-Xmigration"} settings is used to warn about constructs
|whose behavior may have changed since version change."""
}
}

case class UnableToEmitSwitch()(implicit ctx: Context)
extends Message(UnableToEmitSwitchID) {
val kind = "Syntax"
Expand Down
16 changes: 7 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,15 @@ object RefChecks {
}
// Similar to deprecation: check if the symbol is marked with @migration
// indicating it has changed semantics between versions.
if (sym.hasAnnotation(defn.MigrationAnnot) && ctx.settings.Xmigration.value != NoScalaVersion) {
val symVersion: scala.util.Try[ScalaVersion] = sym.migrationVersion.get
val changed = symVersion match {
case scala.util.Success(v) =>
ctx.settings.Xmigration.value < v
val xMigrationValue = ctx.settings.Xmigration.value
if (sym.hasAnnotation(defn.MigrationAnnot) && xMigrationValue != NoScalaVersion) {
sym.migrationVersion.get match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your match is not exhaustive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

case scala.util.Success(symVersion) if xMigrationValue < symVersion=>
ctx.warning(SymbolChangedSemanticsInVersion(sym, symVersion), pos)
case Failure(ex) =>
ctx.warning(s"${sym.showLocated} has an unparsable version number: ${ex.getMessage()}", pos)
false
ctx.warning(SymbolHasUnparsableVersionNumber(sym, ex.getMessage()), pos)
case _ =>
}
if (changed)
ctx.warning(s"${sym.showLocated} has changed semantics in version $symVersion:\n${sym.migrationMessage.get}")
}
/* (Not enabled yet)
* See an explanation of compileTimeOnly in its scaladoc at scala.annotation.compileTimeOnly.
Expand Down