Skip to content

Commit e9ba65f

Browse files
committed
Deprecate package objects with inheritance
This deprecates package objects with inheritance, and removes it under -Xsource:2.14. Ref scala/scala-dev#488 Ref scala/scala-dev#441
1 parent 1c56f0a commit e9ba65f

15 files changed

+99
-18
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,16 +1881,31 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
18811881

18821882
if (settings.isScala211 && mdef.symbol == PredefModule)
18831883
ensurePredefParentsAreInSameSourceFile(impl2)
1884+
if (mdef.name == nme.PACKAGEkw)
1885+
ensurePackageObjectDoesNotInherit(impl2, mdef.symbol, mdef.pos)
18841886

18851887
treeCopy.ModuleDef(mdef, typedMods, mdef.name, impl2) setType NoType
18861888
}
18871889

1890+
private def ensurePackageObjectDoesNotInherit(template: Template, sym: Symbol, pos: Position): Unit = {
1891+
def msg(what: String): String = s"package objects with parent classes or traits are $what"
1892+
def okSymbol(symbol: Symbol): Boolean = (symbol == AnyRefClass) || (symbol == AnyValClass)
1893+
template.parents match {
1894+
case Nil => ()
1895+
case parent :: Nil if okSymbol(parent.symbol) => ()
1896+
case _ =>
1897+
if (settings.isScala214) context.error(pos, msg("unsupported"))
1898+
else context.deprecationWarning(pos, sym, msg("deprecated"), "2.13.0")
1899+
}
1900+
}
1901+
18881902
private def ensurePredefParentsAreInSameSourceFile(template: Template) = {
18891903
val parentSyms = template.parents map (_.symbol) filterNot (_ == AnyRefClass)
18901904
val PredefModuleFile = PredefModule.associatedFile
18911905
if (parentSyms exists (_.associatedFile != PredefModuleFile))
18921906
context.error(template.pos, s"All parents of Predef must be defined in ${PredefModuleFile}.")
18931907
}
1908+
18941909
/** In order to override this in the TreeCheckers Typer so synthetics aren't re-added
18951910
* all the time, it is exposed here the module/class typing methods go through it.
18961911
* ...but it turns out it's also the ideal spot for namer/typer coordination for
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package-inheritance-deprecation.scala:39: warning: package objects with parent classes or traits are deprecated
2+
package object bar extends Duh with Duh2 { }
3+
^
4+
error: No warnings can be incurred under -Xfatal-warnings.
5+
one warning found
6+
one error found
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfatal-warnings -deprecation
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package-inheritance-removal.scala:39: error: package objects with parent classes or traits are unsupported
2+
package object bar extends Duh with Duh2 { }
3+
^
4+
one error found
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xsource:2.14
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// a.scala
2+
// Fri Jan 13 11:31:47 PST 2012
3+
4+
package foo {
5+
package object bar {
6+
def duh(n: Long) = println("long")
7+
def duh(n: Double) = println("double")
8+
9+
def duh2(n: Double) = println("double")
10+
def duh2(n: Long) = println("long")
11+
}
12+
package bar {
13+
object Main {
14+
def main(args:Array[String]): Unit = {
15+
duh(33L)
16+
bip.bar.duh(33L)
17+
duh(33d)
18+
bip.bar.duh(33d)
19+
20+
duh2(33L)
21+
bip.bar.duh2(33L)
22+
duh2(33d)
23+
bip.bar.duh2(33d)
24+
}
25+
}
26+
}
27+
}
28+
29+
package bip {
30+
trait Duh {
31+
def duh(n: Long) = println("long")
32+
def duh(n: Double) = println("double")
33+
}
34+
trait Duh2 {
35+
def duh2(n: Double) = println("double")
36+
def duh2(n: Long) = println("long")
37+
}
38+
39+
package object bar extends Duh with Duh2 { }
40+
package bar {
41+
object Main {
42+
def main(args:Array[String]): Unit = {
43+
duh(33L)
44+
bip.bar.duh(33L)
45+
duh(33d)
46+
bip.bar.duh(33d)
47+
48+
duh2(33L)
49+
bip.bar.duh2(33L)
50+
duh2(33d)
51+
bip.bar.duh2(33d)
52+
}
53+
}
54+
}
55+
}
56+
57+
object Test {
58+
def main(args: Array[String]): Unit = {
59+
foo.bar.Main.main(null)
60+
bip.bar.Main.main(null)
61+
}
62+
}

test/files/pos/t5954d.flags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
-Xfatal-warnings -Xdev
1+
-deprecation -Xdev

test/files/run/t1987.check

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/files/run/t1987.flags

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/files/run/t1987b.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
pkg2.scala:3: warning: package objects with parent classes or traits are deprecated
2+
package object xml extends PullIteratees
3+
^
14
ok!

test/files/run/t1987b.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-deprecation

test/files/run/t5604.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
t5604.scala:26: warning: package objects with parent classes or traits are deprecated
2+
package object bar extends Duh {
3+
^
14
long
25
double
36
long

test/files/run/t5604.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-deprecation

test/scaladoc/run/package-object.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
warning: there was one deprecation warning (since 2.13.0); re-run with -deprecation for details
12
List(test.B, test.A, scala.AnyRef, scala.Any)
23
List(B, A, AnyRef, Any)
34
Some((newSource,10))

0 commit comments

Comments
 (0)