Skip to content

Commit 97a20c7

Browse files
committed
Standardize -rewrite advertisements and suppress them in REPL
1 parent 8c9a021 commit 97a20c7

File tree

9 files changed

+70
-19
lines changed

9 files changed

+70
-19
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()
@@ -3186,7 +3184,7 @@ object Parsers {
31863184
def wildcardSelector() =
31873185
if in.token == USCORE && sourceVersion.isAtLeast(future) then
31883186
report.errorOrMigrationWarning(
3189-
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice("future")}",
3187+
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice(`future-migration`)}",
31903188
in.sourcePos(),
31913189
from = future)
31923190
patch(source, Span(in.offset, in.offset + 1), "*")
@@ -3205,7 +3203,7 @@ object Parsers {
32053203
if in.token == ARROW || isIdent(nme.as) then
32063204
if in.token == ARROW && sourceVersion.isAtLeast(future) then
32073205
report.errorOrMigrationWarning(
3208-
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice("future")}",
3206+
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
32093207
in.sourcePos(),
32103208
from = future)
32113209
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import transform.patmat.SpaceEngine.isIrrefutable
3737
import config.Feature
3838
import config.Feature.sourceVersion
3939
import config.SourceVersion._
40+
import printing.Formatting.hl
4041
import transform.TypeUtils.*
4142

4243
import collection.mutable
@@ -834,11 +835,12 @@ trait Checking {
834835
case NonConforming => sel.srcPos
835836
case RefutableExtractor => pat.source.atSpan(pat.span union sel.span)
836837
else pat.srcPos
838+
def rewriteMsg = Message.rewriteNotice("This patch", `future-migration`)
837839
report.warning(
838840
em"""$message
839841
|
840842
|If $usage is intentional, this can be communicated by $fix,
841-
|which $addendum.${err.rewriteNotice}""",
843+
|which $addendum.$rewriteMsg""",
842844
pos)
843845
false
844846
}
@@ -992,10 +994,10 @@ trait Checking {
992994
("extractor", (n: Name) => s"prefix syntax $n(...)")
993995
else
994996
("method", (n: Name) => s"method syntax .$n(...)")
997+
def rewriteMsg = Message.rewriteNotice("The latter", options = "-deprecation")
995998
report.deprecationWarning(
996-
i"""Alphanumeric $kind $name is not declared `infix`; it should not be used as infix operator.
997-
|The operation can be rewritten automatically to `$name` under -deprecation -rewrite.
998-
|Or rewrite to ${alternative(name)} manually.""",
999+
i"""Alphanumeric $kind $name is not declared ${hl("infix")}; it should not be used as infix operator.
1000+
|Instead, use ${alternative(name)} or backticked identifier `$name`.$rewriteMsg""",
9991001
tree.op.srcPos)
10001002
if (ctx.settings.deprecation.value) {
10011003
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
@@ -3452,10 +3452,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34523452
if sourceVersion == `future-migration` && isContextBoundParams && pt.args.nonEmpty
34533453
then // Under future-migration, don't infer implicit arguments yet for parameters
34543454
// coming from context bounds. Issue a warning instead and offer a patch.
3455+
def rewriteMsg = Message.rewriteNotice("This code", `future-migration`)
34553456
report.migrationWarning(
34563457
em"""Context bounds will map to context parameters.
3457-
|A `using` clause is needed to pass explicit arguments to them.
3458-
|This code can be rewritten automatically using -rewrite""", tree.srcPos)
3458+
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""", tree.srcPos)
34593459
patch(Span(pt.args.head.span.start), "using ")
34603460
tree
34613461
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 backticked identifier `foo`.
17+
1 error found

tests/neg/refutable-pattern-binding-messages.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,44 @@
55
|
66
| If this usage is intentional, this can be communicated by adding the `case` keyword before the full pattern,
77
| which will result in a filtering for expression (using `withFilter`).
8+
| This patch can be rewritten automatically under -rewrite -source future-migration.
89
-- Error: tests/neg/refutable-pattern-binding-messages.scala:11:11 -----------------------------------------------------
910
11 | for ((x: String) <- xs) do () // error: pattern type more specialized
1011
| ^^^^^^
1112
| pattern's type String is more specialized than the right hand side expression's type AnyRef
1213
|
1314
| If the narrowing is intentional, this can be communicated by adding the `case` keyword before the full pattern,
1415
| which will result in a filtering for expression (using `withFilter`).
16+
| This patch can be rewritten automatically under -rewrite -source future-migration.
1517
-- Error: tests/neg/refutable-pattern-binding-messages.scala:15:13 -----------------------------------------------------
1618
15 | for none @ None <- ys do () // error: pattern type does not match
1719
| ^^^^
1820
| pattern's type None.type does not match the right hand side expression's type (x$1 : Option[?])
1921
|
2022
| If the narrowing is intentional, this can be communicated by adding the `case` keyword before the full pattern,
2123
| which will result in a filtering for expression (using `withFilter`).
24+
| This patch can be rewritten automatically under -rewrite -source future-migration.
2225
-- Error: tests/neg/refutable-pattern-binding-messages.scala:5:14 ------------------------------------------------------
2326
5 | val Positive(p) = 5 // error: refutable extractor
2427
| ^^^^^^^^^^^^^^^
2528
| pattern binding uses refutable extractor `Test.Positive`
2629
|
2730
| If this usage is intentional, this can be communicated by adding `: @unchecked` after the expression,
2831
| which may result in a MatchError at runtime.
32+
| This patch can be rewritten automatically under -rewrite -source future-migration.
2933
-- Error: tests/neg/refutable-pattern-binding-messages.scala:10:20 -----------------------------------------------------
3034
10 | val i :: is = List(1, 2, 3) // error: pattern type more specialized
3135
| ^^^^^^^^^^^^^
3236
| pattern's type ::[Int] is more specialized than the right hand side expression's type List[Int]
3337
|
3438
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
3539
| which may result in a MatchError at runtime.
40+
| This patch can be rewritten automatically under -rewrite -source future-migration.
3641
-- Error: tests/neg/refutable-pattern-binding-messages.scala:16:10 -----------------------------------------------------
3742
16 | val 1 = 2 // error: pattern type does not match
3843
| ^
3944
| pattern's type (1 : Int) does not match the right hand side expression's type (2 : Int)
4045
|
4146
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
4247
| which may result in a MatchError at runtime.
48+
| This patch can be rewritten automatically under -rewrite -source future-migration.

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 backticked identifier `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)