Skip to content

Commit

Permalink
Make private[this] a migration warning
Browse files Browse the repository at this point in the history
* In `future-migration` we emit the deprecation warning and enable the patch with -rewrite.
* In `future` we emit we make this syntax an error
  • Loading branch information
nicolasstucki committed Nov 2, 2023
1 parent 1e95432 commit 96f88a0
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 6 deletions.
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
4 changes: 2 additions & 2 deletions sbt-test/compilerReporter/i14576/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 := {
Expand Down
10 changes: 10 additions & 0 deletions tests/neg/private-this-future-migration.check
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions tests/neg/private-this-future-migration.scala
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/neg/private-this-future.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.language.future

class Foo:
private[this] def foo: Int = ??? // error
protected[this] def bar: Int = ??? // error
5 changes: 5 additions & 0 deletions tests/pos/private-this-future-migration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.language.`future-migration`

class Foo:
private[this] def foo: Int = ??? // warn
protected[this] def bar: Int = ??? // warn
3 changes: 3 additions & 0 deletions tests/pos/private-this.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo:
private[this] def foo: Int = ???
protected[this] def bar: Int = ???
12 changes: 12 additions & 0 deletions tests/rewrites/private-this.check
Original file line number Diff line number Diff line change
@@ -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 = ???
12 changes: 12 additions & 0 deletions tests/rewrites/private-this.scala
Original file line number Diff line number Diff line change
@@ -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 = ???

0 comments on commit 96f88a0

Please sign in to comment.