Skip to content

Commit

Permalink
Make sure that patches for 3.0 are also applied in later versions (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki authored Nov 22, 2023
2 parents f831830 + 590e159 commit c233061
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ object Parsers {
report.errorOrMigrationWarning(
em"parentheses are required around the parameter of a lambda${rewriteNotice()}",
in.sourcePos(), from = `3.0`)
if migrateTo3 then
if sourceVersion.isMigrating then
patch(source, t.span.startPos, "(")
patch(source, t.span.endPos, ")")
convertToParam(t, mods) :: Nil
Expand Down Expand Up @@ -1315,7 +1315,7 @@ object Parsers {
|For now, you can also `import language.deprecated.symbolLiterals` to accept
|the idiom, but this possibility might no longer be available in the future.""",
in.sourcePos(), from = `3.0`)
if migrateTo3 then
if sourceVersion.isMigrating then
patch(source, Span(in.offset, in.offset + 1), "Symbol(\"")
patch(source, Span(in.charOffset - 1), "\")")
atSpan(in.skipToken()) { SymbolLit(in.strVal) }
Expand Down Expand Up @@ -1413,7 +1413,8 @@ object Parsers {
|It needs to be indented to the right to keep being treated as
|an argument to the previous expression.${rewriteNotice()}""",
in.sourcePos(), from = `3.0`)
patch(source, Span(in.offset), " ")
if sourceVersion.isMigrating then
patch(source, Span(in.offset), " ")

def possibleTemplateStart(isNew: Boolean = false): Unit =
in.observeColonEOL(inTemplate = true)
Expand Down Expand Up @@ -2267,7 +2268,7 @@ object Parsers {
val whileStart = in.offset
accept(WHILE)
val cond = expr()
if migrateTo3 then
if sourceVersion.isMigrating then
patch(source, Span(start, start + 2), "while ({")
patch(source, Span(whileStart, whileStart + 5), ";")
cond match {
Expand Down Expand Up @@ -3153,7 +3154,7 @@ object Parsers {
warnFrom = `3.4`,
errorFrom = future)
if sourceVersion.isMigrating && sourceVersion.isAtLeast(`3.4-migration`) then
patch(source, Span(startOffset, in.lastOffset), "")
patch(source, Span(startOffset, in.lastOffset), "")
mods1
}
else mods
Expand Down
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import scala.collection.immutable.SortedMap
import rewrites.Rewrites.patch
import config.Feature
import config.Feature.{migrateTo3, fewerBracesEnabled}
import config.SourceVersion.`3.0`
import config.SourceVersion.{`3.0`, `3.0-migration`}
import reporting.{NoProfile, Profile, Message}

import java.util.Objects
import dotty.tools.dotc.reporting.Message.rewriteNotice
import dotty.tools.dotc.config.Feature.sourceVersion

object Scanners {

Expand Down Expand Up @@ -253,11 +255,12 @@ object Scanners {
if scala3keywords.contains(keyword) && migrateTo3 then
val what = tokenString(keyword)
report.errorOrMigrationWarning(
em"$what is now a keyword, write `$what` instead of $what to keep it as an identifier",
em"$what is now a keyword, write `$what` instead of $what to keep it as an identifier${rewriteNotice("This", `3.0-migration`)}",
sourcePos(),
from = `3.0`)
patch(source, Span(offset), "`")
patch(source, Span(offset + identifier.length), "`")
if sourceVersion.isMigrating then
patch(source, Span(offset), "`")
patch(source, Span(offset + identifier.length), "`")
IDENTIFIER
else keyword
val idx = identifier.start
Expand Down
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
report.errorOrMigrationWarning(
AmbiguousReference(name, Definition, Inheritance, prevCtx)(using outer),
pos, from = `3.0`)
if migrateTo3 then
if sourceVersion.isMigrating then
patch(Span(pos.span.start),
if prevCtx.owner == refctx.owner.enclosingClass then "this."
else s"${prevCtx.owner.name}.this.")
Expand Down Expand Up @@ -2984,13 +2984,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val recovered = typed(qual)(using ctx.fresh.setExploreTyperState())
val msg = OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen, tree)
report.errorOrMigrationWarning(msg, tree.srcPos, from = `3.0`)
if (migrateTo3) {
if sourceVersion.isMigrating then
// Under -rewrite, patch `x _` to `(() => x)`
msg.actions
.headOption
.foreach(Rewrites.applyAction)
return typed(untpd.Function(Nil, qual), pt)
}
}
nestedCtx.typerState.commit()

Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i13440.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
5 |def given = 42 // error
| ^
| given is now a keyword, write `given` instead of given to keep it as an identifier
| This can be rewritten automatically under -rewrite -source 3.0-migration.
-- Error: tests/neg/i13440.scala:7:13 ----------------------------------------------------------------------------------
7 |case class C(enum: List[Int] = Nil) { // error
| ^
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
| This can be rewritten automatically under -rewrite -source 3.0-migration.
-- Error: tests/neg/i13440.scala:8:11 ----------------------------------------------------------------------------------
8 | val s = s"$enum" // error
| ^
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
| This can be rewritten automatically under -rewrite -source 3.0-migration.

0 comments on commit c233061

Please sign in to comment.