Skip to content

Commit 73f63c8

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 1e95432 commit 73f63c8

9 files changed

+64
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,15 +3113,20 @@ object Parsers {
31133113
if (in.token == LBRACKET) {
31143114
if (mods.is(Local) || mods.hasPrivateWithin)
31153115
syntaxError(DuplicatePrivateProtectedQualifier())
3116-
inBrackets {
3116+
val startOffset = in.offset
3117+
val mods1 = inBrackets {
31173118
if in.token == THIS then
3118-
if sourceVersion.isAtLeast(future) then
3119-
deprecationWarning(
3120-
em"The [this] qualifier will be deprecated in the future; it should be dropped.")
31213119
in.nextToken()
31223120
mods | Local
31233121
else mods.withPrivateWithin(ident().toTypeName)
31243122
}
3123+
if mods1.is(Local) then
3124+
report.errorOrMigrationWarning(
3125+
em"The [this] qualifier will be deprecated in the future; it should be dropped.${rewriteNotice(`future-migration`)}",
3126+
in.sourcePos(), from = future)
3127+
if sourceVersion == `future-migration` then
3128+
patch(source, Span(startOffset, in.lastOffset), "")
3129+
mods1
31253130
}
31263131
else mods
31273132

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CompilationTests {
6060
compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")),
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")),
63+
compileFile("tests/rewrites/private-this.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6364
compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6465
compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6566
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),
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)