From edc59a8810ed1057c05597bee57ca348d35b4622 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Sun, 23 Jun 2024 19:19:25 +0200 Subject: [PATCH] Make sure that patches for 3.0 are also applied in later versions [Cherry-picked 590e15920eecf93a1a15ae328cb94e0be666ec2a][modified] --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 9 +++++---- compiler/src/dotty/tools/dotc/parsing/Scanners.scala | 11 +++++++---- compiler/src/dotty/tools/dotc/typer/Typer.scala | 5 ++--- tests/neg/i13440.check | 3 +++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index bb8454a74b49..c9dd9924eae7 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -455,7 +455,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 @@ -1305,7 +1305,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) } @@ -1403,7 +1403,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) @@ -2222,7 +2223,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 { diff --git a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala index 0339fc0531f4..2c8eb51a79db 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala @@ -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 { @@ -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 diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index a74f6dc32668..64188d94d21b 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -441,7 +441,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.") @@ -2892,13 +2892,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() if sourceVersion.isAtLeast(future) then diff --git a/tests/neg/i13440.check b/tests/neg/i13440.check index c820ee67833d..277d3dce0bf0 100644 --- a/tests/neg/i13440.check +++ b/tests/neg/i13440.check @@ -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.