Skip to content

Commit 4696622

Browse files
committed
Standardize -rewrite advertisements and suppress them in REPL
1 parent 38bf309 commit 4696622

File tree

8 files changed

+62
-18
lines changed

8 files changed

+62
-18
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,11 @@ object Parsers {
359359
recur(false, false)
360360
end statSepOrEnd
361361

362-
def rewriteNotice(version: String = "3.0", additionalOption: String = "") = {
363-
val optionStr = if (additionalOption.isEmpty) "" else " " ++ additionalOption
364-
i"\nThis construct can be rewritten automatically under$optionStr -rewrite -source $version-migration."
365-
}
362+
def rewriteNotice(version: SourceVersion = `3.0-migration`, additionalOption: String = "") =
363+
Message.rewriteNotice("This construct", version, additionalOption)
366364

367365
def syntaxVersionError(option: String, span: Span) =
368-
syntaxError(em"""This construct is not allowed under $option.${rewriteNotice("3.0", option)}""", span)
366+
syntaxError(em"""This construct is not allowed under $option.${rewriteNotice(`3.0-migration`, option)}""", span)
369367

370368
def rewriteToNewSyntax(span: Span = Span(in.offset)): Boolean = {
371369
if (in.newSyntax) {
@@ -2084,7 +2082,7 @@ object Parsers {
20842082
in.nextToken()
20852083
if isVarargSplice then
20862084
report.errorOrMigrationWarning(
2087-
em"The syntax `x: _*` is no longer supported for vararg splices; use `x*` instead${rewriteNotice("future")}",
2085+
em"The syntax `x: _*` is no longer supported for vararg splices; use `x*` instead${rewriteNotice(`future-migration`)}",
20882086
in.sourcePos(uscoreStart),
20892087
future)
20902088
if sourceVersion == `future-migration` then
@@ -2160,7 +2158,7 @@ object Parsers {
21602158
val t =
21612159
if (in.token == COLON && location == Location.InBlock) {
21622160
report.errorOrMigrationWarning(
2163-
s"This syntax is no longer supported; parameter needs to be enclosed in (...)${rewriteNotice("future")}",
2161+
s"This syntax is no longer supported; parameter needs to be enclosed in (...)${rewriteNotice(`future-migration`)}",
21642162
source.atSpan(Span(start, in.lastOffset)),
21652163
from = future)
21662164
in.nextToken()
@@ -3171,7 +3169,7 @@ object Parsers {
31713169
def wildcardSelector() =
31723170
if in.token == USCORE && sourceVersion.isAtLeast(future) then
31733171
report.errorOrMigrationWarning(
3174-
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice("future")}",
3172+
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice(`future-migration`)}",
31753173
in.sourcePos(),
31763174
from = future)
31773175
patch(source, Span(in.offset, in.offset + 1), "*")
@@ -3190,7 +3188,7 @@ object Parsers {
31903188
if in.token == ARROW || isIdent(nme.as) then
31913189
if in.token == ARROW && sourceVersion.isAtLeast(future) then
31923190
report.errorOrMigrationWarning(
3193-
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice("future")}",
3191+
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
31943192
in.sourcePos(),
31953193
from = future)
31963194
patch(source, Span(in.offset, in.offset + 2),

compiler/src/dotty/tools/dotc/reporting/Message.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package dotty.tools
22
package dotc
33
package reporting
44

5+
import core.Contexts._, core.Decorators._, core.Mode
6+
import config.SourceVersion
7+
58
import scala.language.unsafeNulls
69

710
import scala.annotation.threadUnsafe
@@ -15,6 +18,16 @@ object Message {
1518
* see where old errors still exist
1619
*/
1720
implicit def toNoExplanation(str: => String): Message = NoExplanation(str)
21+
22+
def rewriteNotice(what: String, version: SourceVersion | Null = null, options: String = "")(using Context): String =
23+
if !ctx.mode.is(Mode.Interactive) then
24+
val sourceStr = if version != null then i"-source $version" else ""
25+
val optionStr =
26+
if options.isEmpty then sourceStr
27+
else if sourceStr.isEmpty then options
28+
else i"$sourceStr $options"
29+
i"\n$what can be rewritten automatically under -rewrite $optionStr."
30+
else ""
1831
}
1932

2033
/** A `Message` contains all semantic information necessary to easily

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,11 @@ trait Checking {
805805
val problem = if (pat.tpe <:< reportedPt) "is more specialized than" else "does not match"
806806
val fix = if (isPatDef) "adding `: @unchecked` after the expression" else "writing `case ` before the full pattern"
807807
val pos = if (isPatDef) sel.srcPos else pat.srcPos
808+
def rewriteMsg = Message.rewriteNotice("This patch", `future-migration`)
808809
report.warning(
809810
ex"""pattern's type ${pat.tpe} $problem the right hand side expression's type $reportedPt
810811
|
811-
|If the narrowing is intentional, this can be communicated by $fix.${err.rewriteNotice}""",
812+
|If the narrowing is intentional, this can be communicated by $fix.$rewriteMsg""",
812813
pos)
813814
false
814815
}
@@ -962,10 +963,10 @@ trait Checking {
962963
("extractor", (n: Name) => s"prefix syntax $n(...)")
963964
else
964965
("method", (n: Name) => s"method syntax .$n(...)")
966+
def rewriteMsg = Message.rewriteNotice("The latter", options = "-deprecation")
965967
report.deprecationWarning(
966968
i"""Alphanumeric $kind $name is not declared `infix`; it should not be used as infix operator.
967-
|The operation can be rewritten automatically to `$name` under -deprecation -rewrite.
968-
|Or rewrite to ${alternative(name)} manually.""",
969+
|Instead, use ${alternative(name)} or rewrite as `$name`.$rewriteMsg""",
969970
tree.op.srcPos)
970971
if (ctx.settings.deprecation.value) {
971972
patch(Span(tree.op.span.start, tree.op.span.start), "`")

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ object ErrorReporting {
153153
|
154154
|The tests were made under $constraintText"""
155155

156-
def rewriteNotice: String =
157-
if Feature.migrateTo3 then "\nThis patch can be inserted automatically under -rewrite."
158-
else ""
159-
160156
def whyFailedStr(fail: FailedExtension) =
161157
i""" failed with
162158
|

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,10 +3406,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34063406
if sourceVersion == `future-migration` && isContextBoundParams
34073407
then // Under future-migration, don't infer implicit arguments yet for parameters
34083408
// coming from context bounds. Issue a warning instead and offer a patch.
3409+
def rewriteMsg = Message.rewriteNotice("This code", `future-migration`)
34093410
report.migrationWarning(
34103411
em"""Context bounds will map to context parameters.
3411-
|A `using` clause is needed to pass explicit arguments to them.
3412-
|This code can be rewritten automatically using -rewrite""", tree.srcPos)
3412+
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""", tree.srcPos)
34133413
patch(Span(pt.args.head.span.start), "using ")
34143414
tree
34153415
else
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// scalac: -source:future-migration -deprecation -Werror
2+
scala> import scala.util._
3+
-- Error: ----------------------------------------------------------------------
4+
1 | import scala.util._
5+
| ^
6+
| `_` is no longer supported for a wildcard import; use `*` instead
7+
8+
scala> extension (x: Int) def foo(y: Int) = x + y
9+
def foo(x: Int)(y: Int): Int
10+
11+
scala> 2 foo 4
12+
-- Error: ----------------------------------------------------------------------
13+
1 | 2 foo 4
14+
| ^^^
15+
|Alphanumeric method foo is not declared `infix`; it should not be used as infix operator.
16+
|Instead, use method syntax .foo(...) or rewrite as `foo`.
17+
1 error found

tests/neg/rewrite-messages.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Error: tests/neg/rewrite-messages.scala:3:18 ------------------------------------------------------------------------
2+
3 |import scala.util._ // error
3+
| ^
4+
| `_` is no longer supported for a wildcard import; use `*` instead
5+
| This construct can be rewritten automatically under -rewrite -source future-migration.
6+
-- Error: tests/neg/rewrite-messages.scala:7:4 -------------------------------------------------------------------------
7+
7 | 2 foo 4 // error
8+
| ^^^
9+
| Alphanumeric method foo is not declared `infix`; it should not be used as infix operator.
10+
| Instead, use method syntax .foo(...) or rewrite as `foo`.
11+
| The latter can be rewritten automatically under -rewrite -deprecation.

tests/neg/rewrite-messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// scalac: -source:future-migration -deprecation -Werror
2+
3+
import scala.util._ // error
4+
5+
object Test {
6+
extension (x: Int) def foo(y: Int) = x + y
7+
2 foo 4 // error
8+
}

0 commit comments

Comments
 (0)