From 96f88a03d3712abed4c1242de08beec7c7611ae3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 1 Nov 2023 17:19:46 +0100 Subject: [PATCH] Make `private[this]` a migration warning * In `future-migration` we emit the deprecation warning and enable the patch with -rewrite. * In `future` we emit we make this syntax an error --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 13 +++++++++---- .../test/dotty/tools/dotc/CompilationTests.scala | 1 + sbt-test/compilerReporter/i14576/build.sbt | 4 ++-- tests/neg/private-this-future-migration.check | 10 ++++++++++ tests/neg/private-this-future-migration.scala | 7 +++++++ tests/neg/private-this-future.scala | 5 +++++ tests/pos/private-this-future-migration.scala | 5 +++++ tests/pos/private-this.scala | 3 +++ tests/rewrites/private-this.check | 12 ++++++++++++ tests/rewrites/private-this.scala | 12 ++++++++++++ 10 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 tests/neg/private-this-future-migration.check create mode 100644 tests/neg/private-this-future-migration.scala create mode 100644 tests/neg/private-this-future.scala create mode 100644 tests/pos/private-this-future-migration.scala create mode 100644 tests/pos/private-this.scala create mode 100644 tests/rewrites/private-this.check create mode 100644 tests/rewrites/private-this.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 48653ad96197..7f9c6dac89ea 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -3113,15 +3113,20 @@ object Parsers { if (in.token == LBRACKET) { if (mods.is(Local) || mods.hasPrivateWithin) syntaxError(DuplicatePrivateProtectedQualifier()) - inBrackets { + val startOffset = in.offset + val mods1 = inBrackets { if in.token == THIS then - if sourceVersion.isAtLeast(future) then - deprecationWarning( - em"The [this] qualifier will be deprecated in the future; it should be dropped.") in.nextToken() mods | Local else mods.withPrivateWithin(ident().toTypeName) } + if mods1.is(Local) then + report.errorOrMigrationWarning( + em"The [this] qualifier will be deprecated in the future; it should be dropped.${rewriteNotice(`future-migration`)}", + in.sourcePos(), from = future) + if sourceVersion == `future-migration` then + patch(source, Span(startOffset, in.lastOffset), "") + mods1 } else mods diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 2acb71ce62a8..7087ab7c5358 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -60,6 +60,7 @@ class CompilationTests { compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")), compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")), compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")), + compileFile("tests/rewrites/private-this.scala", defaultOptions.and("-rewrite", "-source", "future-migration")), compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")), compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")), compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")), diff --git a/sbt-test/compilerReporter/i14576/build.sbt b/sbt-test/compilerReporter/i14576/build.sbt index 9831c23c103e..f9f211b24977 100644 --- a/sbt-test/compilerReporter/i14576/build.sbt +++ b/sbt-test/compilerReporter/i14576/build.sbt @@ -10,7 +10,7 @@ lazy val resetMessages = taskKey[Unit]("empties the messages list") lazy val root = (project in file(".")) .settings( - scalacOptions += "-source:future", + scalacOptions += "-source:future-migration", extraAppenders := { s => Seq(ConsoleAppender(FakePrintWriter)) }, assertFeatureSummary := { assert { @@ -24,7 +24,7 @@ lazy val root = (project in file(".")) }, assertDeprecationSummary := { assert { - FakePrintWriter.messages.exists(_.contains("there were 3 deprecation warnings; re-run with -deprecation for details")) + FakePrintWriter.messages.exists(_.contains("there were 2 deprecation warnings; re-run with -deprecation for details")) } }, assertNoDeprecationSummary := { diff --git a/tests/neg/private-this-future-migration.check b/tests/neg/private-this-future-migration.check new file mode 100644 index 000000000000..99fe798118a8 --- /dev/null +++ b/tests/neg/private-this-future-migration.check @@ -0,0 +1,10 @@ +-- Error: tests/neg/private-this-future-migration.scala:6:16 ----------------------------------------------------------- +6 | private[this] def foo: Int = ??? // error + | ^ + | The [this] qualifier will be deprecated in the future; it should be dropped. + | This construct can be rewritten automatically under -rewrite -source future-migration. +-- Error: tests/neg/private-this-future-migration.scala:7:18 ----------------------------------------------------------- +7 | protected[this] def bar: Int = ??? // error + | ^ + | The [this] qualifier will be deprecated in the future; it should be dropped. + | This construct can be rewritten automatically under -rewrite -source future-migration. diff --git a/tests/neg/private-this-future-migration.scala b/tests/neg/private-this-future-migration.scala new file mode 100644 index 000000000000..fd93f00a43f3 --- /dev/null +++ b/tests/neg/private-this-future-migration.scala @@ -0,0 +1,7 @@ +//> using options -Werror + +import scala.language.`future-migration` + +class Foo: + private[this] def foo: Int = ??? // error + protected[this] def bar: Int = ??? // error diff --git a/tests/neg/private-this-future.scala b/tests/neg/private-this-future.scala new file mode 100644 index 000000000000..d94cbe16abad --- /dev/null +++ b/tests/neg/private-this-future.scala @@ -0,0 +1,5 @@ +import scala.language.future + +class Foo: + private[this] def foo: Int = ??? // error + protected[this] def bar: Int = ??? // error diff --git a/tests/pos/private-this-future-migration.scala b/tests/pos/private-this-future-migration.scala new file mode 100644 index 000000000000..cdcc6a2c0321 --- /dev/null +++ b/tests/pos/private-this-future-migration.scala @@ -0,0 +1,5 @@ +import scala.language.`future-migration` + +class Foo: + private[this] def foo: Int = ??? // warn + protected[this] def bar: Int = ??? // warn diff --git a/tests/pos/private-this.scala b/tests/pos/private-this.scala new file mode 100644 index 000000000000..18de91df72cb --- /dev/null +++ b/tests/pos/private-this.scala @@ -0,0 +1,3 @@ +class Foo: + private[this] def foo: Int = ??? + protected[this] def bar: Int = ??? diff --git a/tests/rewrites/private-this.check b/tests/rewrites/private-this.check new file mode 100644 index 000000000000..1a6443cdf152 --- /dev/null +++ b/tests/rewrites/private-this.check @@ -0,0 +1,12 @@ +class Foo: + private def foo1: Int = ??? + private def foo2: Int = ??? + private def foo3: Int = ??? + private def foo4: Int = ??? + private def foo5: Int = ??? + + protected def bar1: Int = ??? + protected def bar2: Int = ??? + protected def bar3: Int = ??? + protected def bar4: Int = ??? + protected def bar5: Int = ??? diff --git a/tests/rewrites/private-this.scala b/tests/rewrites/private-this.scala new file mode 100644 index 000000000000..5f5b71f26abe --- /dev/null +++ b/tests/rewrites/private-this.scala @@ -0,0 +1,12 @@ +class Foo: + private[this] def foo1: Int = ??? + private[ this] def foo2: Int = ??? + private[this ] def foo3: Int = ??? + private[ this ] def foo4: Int = ??? + private [this] def foo5: Int = ??? + + protected[this] def bar1: Int = ??? + protected[ this] def bar2: Int = ??? + protected[this ] def bar3: Int = ??? + protected[ this ] def bar4: Int = ??? + protected [this] def bar5: Int = ???