From 590e15920eecf93a1a15ae328cb94e0be666ec2a Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 22 Nov 2023 10:31:26 +0100 Subject: [PATCH] Make sure that patches for 3.0 are also applied in later versions --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 11 ++++++----- 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, 18 insertions(+), 12 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index a2cc6499e843..4a855e20fb16 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -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 @@ -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) } @@ -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) @@ -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 { @@ -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 diff --git a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala index 44b0c43e545b..f1e9e646bf43 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 3d61ac0a51f3..5ab5e7415c7b 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -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.") @@ -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() 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.