Skip to content

Commit b2e7a55

Browse files
committed
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
1 parent 80eb6e1 commit b2e7a55

File tree

10 files changed

+66
-6
lines changed

10 files changed

+66
-6
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,15 +3111,20 @@ object Parsers {
31113111
if (in.token == LBRACKET) {
31123112
if (mods.is(Local) || mods.hasPrivateWithin)
31133113
syntaxError(DuplicatePrivateProtectedQualifier())
3114-
inBrackets {
3114+
val startOffset = in.offset
3115+
val mods1 = inBrackets {
31153116
if in.token == THIS then
3116-
if sourceVersion.isAtLeast(future) then
3117-
deprecationWarning(
3118-
em"The [this] qualifier will be deprecated in the future; it should be dropped.")
31193117
in.nextToken()
31203118
mods | Local
31213119
else mods.withPrivateWithin(ident().toTypeName)
31223120
}
3121+
if mods1.is(Local) then
3122+
report.errorOrMigrationWarning(
3123+
em"The [this] qualifier will be deprecated in the future; it should be dropped.${rewriteNotice(`future-migration`)}",
3124+
in.sourcePos(), from = future)
3125+
if sourceVersion == `future-migration` then
3126+
patch(source, Span(startOffset, in.lastOffset), "")
3127+
mods1
31233128
}
31243129
else mods
31253130

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class CompilationTests {
6161
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6262
compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")),
6363
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
64+
compileFile("tests/rewrites/private-this.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6465
compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6566
compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6667
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),

sbt-test/compilerReporter/i14576/build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lazy val resetMessages = taskKey[Unit]("empties the messages list")
1010

1111
lazy val root = (project in file("."))
1212
.settings(
13-
scalacOptions += "-source:future",
13+
scalacOptions += "-source:future-migration",
1414
extraAppenders := { s => Seq(ConsoleAppender(FakePrintWriter)) },
1515
assertFeatureSummary := {
1616
assert {
@@ -24,7 +24,7 @@ lazy val root = (project in file("."))
2424
},
2525
assertDeprecationSummary := {
2626
assert {
27-
FakePrintWriter.messages.exists(_.contains("there were 3 deprecation warnings; re-run with -deprecation for details"))
27+
FakePrintWriter.messages.exists(_.contains("there were 2 deprecation warnings; re-run with -deprecation for details"))
2828
}
2929
},
3030
assertNoDeprecationSummary := {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Error: tests/neg/private-this-future-migration.scala:6:16 -----------------------------------------------------------
2+
6 | private[this] def foo: Int = ??? // error
3+
| ^
4+
| The [this] qualifier will be deprecated in the future; it should be dropped.
5+
| This construct can be rewritten automatically under -rewrite -source future-migration.
6+
-- Error: tests/neg/private-this-future-migration.scala:7:18 -----------------------------------------------------------
7+
7 | protected[this] def bar: Int = ??? // error
8+
| ^
9+
| The [this] qualifier will be deprecated in the future; it should be dropped.
10+
| This construct can be rewritten automatically under -rewrite -source future-migration.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//> using options -Werror
2+
3+
import scala.language.`future-migration`
4+
5+
class Foo:
6+
private[this] def foo: Int = ??? // error
7+
protected[this] def bar: Int = ??? // error

tests/neg/private-this-future.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.language.future
2+
3+
class Foo:
4+
private[this] def foo: Int = ??? // error
5+
protected[this] def bar: Int = ??? // error
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.language.`future-migration`
2+
3+
class Foo:
4+
private[this] def foo: Int = ??? // warn
5+
protected[this] def bar: Int = ??? // warn

tests/pos/private-this.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo:
2+
private[this] def foo: Int = ???
3+
protected[this] def bar: Int = ???

tests/rewrites/private-this.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Foo:
2+
private def foo1: Int = ???
3+
private def foo2: Int = ???
4+
private def foo3: Int = ???
5+
private def foo4: Int = ???
6+
private def foo5: Int = ???
7+
8+
protected def bar1: Int = ???
9+
protected def bar2: Int = ???
10+
protected def bar3: Int = ???
11+
protected def bar4: Int = ???
12+
protected def bar5: Int = ???

tests/rewrites/private-this.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Foo:
2+
private[this] def foo1: Int = ???
3+
private[ this] def foo2: Int = ???
4+
private[this ] def foo3: Int = ???
5+
private[ this ] def foo4: Int = ???
6+
private [this] def foo5: Int = ???
7+
8+
protected[this] def bar1: Int = ???
9+
protected[ this] def bar2: Int = ???
10+
protected[this ] def bar3: Int = ???
11+
protected[ this ] def bar4: Int = ???
12+
protected [this] def bar5: Int = ???

0 commit comments

Comments
 (0)