Skip to content

Commit

Permalink
Deprecate package objects with inheritance
Browse files Browse the repository at this point in the history
This deprecates package objects with inheritance, and removes it under -Xsource:2.14.

Ref scala/scala-dev#488
Ref scala/scala-dev#441
  • Loading branch information
eed3si9n committed May 2, 2018
1 parent 1c56f0a commit e9ba65f
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1881,16 +1881,31 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper

if (settings.isScala211 && mdef.symbol == PredefModule)
ensurePredefParentsAreInSameSourceFile(impl2)
if (mdef.name == nme.PACKAGEkw)
ensurePackageObjectDoesNotInherit(impl2, mdef.symbol, mdef.pos)

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

private def ensurePackageObjectDoesNotInherit(template: Template, sym: Symbol, pos: Position): Unit = {
def msg(what: String): String = s"package objects with parent classes or traits are $what"
def okSymbol(symbol: Symbol): Boolean = (symbol == AnyRefClass) || (symbol == AnyValClass)
template.parents match {
case Nil => ()
case parent :: Nil if okSymbol(parent.symbol) => ()
case _ =>
if (settings.isScala214) context.error(pos, msg("unsupported"))
else context.deprecationWarning(pos, sym, msg("deprecated"), "2.13.0")
}
}

private def ensurePredefParentsAreInSameSourceFile(template: Template) = {
val parentSyms = template.parents map (_.symbol) filterNot (_ == AnyRefClass)
val PredefModuleFile = PredefModule.associatedFile
if (parentSyms exists (_.associatedFile != PredefModuleFile))
context.error(template.pos, s"All parents of Predef must be defined in ${PredefModuleFile}.")
}

/** In order to override this in the TreeCheckers Typer so synthetics aren't re-added
* all the time, it is exposed here the module/class typing methods go through it.
* ...but it turns out it's also the ideal spot for namer/typer coordination for
Expand Down
6 changes: 6 additions & 0 deletions test/files/neg/package-inheritance-deprecation.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package-inheritance-deprecation.scala:39: warning: package objects with parent classes or traits are deprecated
package object bar extends Duh with Duh2 { }
^
error: No warnings can be incurred under -Xfatal-warnings.
one warning found
one error found
1 change: 1 addition & 0 deletions test/files/neg/package-inheritance-deprecation.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Xfatal-warnings -deprecation
File renamed without changes.
4 changes: 4 additions & 0 deletions test/files/neg/package-inheritance-removal.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package-inheritance-removal.scala:39: error: package objects with parent classes or traits are unsupported
package object bar extends Duh with Duh2 { }
^
one error found
1 change: 1 addition & 0 deletions test/files/neg/package-inheritance-removal.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Xsource:2.14
62 changes: 62 additions & 0 deletions test/files/neg/package-inheritance-removal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// a.scala
// Fri Jan 13 11:31:47 PST 2012

package foo {
package object bar {
def duh(n: Long) = println("long")
def duh(n: Double) = println("double")

def duh2(n: Double) = println("double")
def duh2(n: Long) = println("long")
}
package bar {
object Main {
def main(args:Array[String]): Unit = {
duh(33L)
bip.bar.duh(33L)
duh(33d)
bip.bar.duh(33d)

duh2(33L)
bip.bar.duh2(33L)
duh2(33d)
bip.bar.duh2(33d)
}
}
}
}

package bip {
trait Duh {
def duh(n: Long) = println("long")
def duh(n: Double) = println("double")
}
trait Duh2 {
def duh2(n: Double) = println("double")
def duh2(n: Long) = println("long")
}

package object bar extends Duh with Duh2 { }
package bar {
object Main {
def main(args:Array[String]): Unit = {
duh(33L)
bip.bar.duh(33L)
duh(33d)
bip.bar.duh(33d)

duh2(33L)
bip.bar.duh2(33L)
duh2(33d)
bip.bar.duh2(33d)
}
}
}
}

object Test {
def main(args: Array[String]): Unit = {
foo.bar.Main.main(null)
bip.bar.Main.main(null)
}
}
2 changes: 1 addition & 1 deletion test/files/pos/t5954d.flags
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-Xfatal-warnings -Xdev
-deprecation -Xdev
16 changes: 0 additions & 16 deletions test/files/run/t1987.check

This file was deleted.

1 change: 0 additions & 1 deletion test/files/run/t1987.flags

This file was deleted.

3 changes: 3 additions & 0 deletions test/files/run/t1987b.check
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pkg2.scala:3: warning: package objects with parent classes or traits are deprecated
package object xml extends PullIteratees
^
ok!
1 change: 1 addition & 0 deletions test/files/run/t1987b.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-deprecation
3 changes: 3 additions & 0 deletions test/files/run/t5604.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
t5604.scala:26: warning: package objects with parent classes or traits are deprecated
package object bar extends Duh {
^
long
double
long
Expand Down
1 change: 1 addition & 0 deletions test/files/run/t5604.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-deprecation
1 change: 1 addition & 0 deletions test/scaladoc/run/package-object.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
warning: there was one deprecation warning (since 2.13.0); re-run with -deprecation for details
List(test.B, test.A, scala.AnyRef, scala.Any)
List(B, A, AnyRef, Any)
Some((newSource,10))
Expand Down

0 comments on commit e9ba65f

Please sign in to comment.