diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 217643eae0ba..65fe07de83cc 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -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 diff --git a/test/files/neg/package-inheritance-deprecation.check b/test/files/neg/package-inheritance-deprecation.check new file mode 100644 index 000000000000..778d79577376 --- /dev/null +++ b/test/files/neg/package-inheritance-deprecation.check @@ -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 diff --git a/test/files/neg/package-inheritance-deprecation.flags b/test/files/neg/package-inheritance-deprecation.flags new file mode 100644 index 000000000000..7de3c0f3eea0 --- /dev/null +++ b/test/files/neg/package-inheritance-deprecation.flags @@ -0,0 +1 @@ +-Xfatal-warnings -deprecation diff --git a/test/files/run/t1987.scala b/test/files/neg/package-inheritance-deprecation.scala similarity index 100% rename from test/files/run/t1987.scala rename to test/files/neg/package-inheritance-deprecation.scala diff --git a/test/files/neg/package-inheritance-removal.check b/test/files/neg/package-inheritance-removal.check new file mode 100644 index 000000000000..a7b06371e76e --- /dev/null +++ b/test/files/neg/package-inheritance-removal.check @@ -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 diff --git a/test/files/neg/package-inheritance-removal.flags b/test/files/neg/package-inheritance-removal.flags new file mode 100644 index 000000000000..7a269652ae4b --- /dev/null +++ b/test/files/neg/package-inheritance-removal.flags @@ -0,0 +1 @@ +-Xsource:2.14 diff --git a/test/files/neg/package-inheritance-removal.scala b/test/files/neg/package-inheritance-removal.scala new file mode 100644 index 000000000000..f5abc262cc9f --- /dev/null +++ b/test/files/neg/package-inheritance-removal.scala @@ -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) + } +} diff --git a/test/files/pos/t5954d.flags b/test/files/pos/t5954d.flags index 6ced0e709065..62d932e4f4fd 100644 --- a/test/files/pos/t5954d.flags +++ b/test/files/pos/t5954d.flags @@ -1 +1 @@ --Xfatal-warnings -Xdev +-deprecation -Xdev diff --git a/test/files/run/t1987.check b/test/files/run/t1987.check deleted file mode 100644 index d2102a4a183f..000000000000 --- a/test/files/run/t1987.check +++ /dev/null @@ -1,16 +0,0 @@ -long -long -double -double -long -long -double -double -long -long -double -double -long -long -double -double diff --git a/test/files/run/t1987.flags b/test/files/run/t1987.flags deleted file mode 100644 index e8fb65d50c20..000000000000 --- a/test/files/run/t1987.flags +++ /dev/null @@ -1 +0,0 @@ --Xfatal-warnings \ No newline at end of file diff --git a/test/files/run/t1987b.check b/test/files/run/t1987b.check index 68d4b10e1204..fc023247e77b 100644 --- a/test/files/run/t1987b.check +++ b/test/files/run/t1987b.check @@ -1 +1,4 @@ +pkg2.scala:3: warning: package objects with parent classes or traits are deprecated +package object xml extends PullIteratees + ^ ok! diff --git a/test/files/run/t1987b.flags b/test/files/run/t1987b.flags new file mode 100644 index 000000000000..dcc59ebe32ef --- /dev/null +++ b/test/files/run/t1987b.flags @@ -0,0 +1 @@ +-deprecation diff --git a/test/files/run/t5604.check b/test/files/run/t5604.check index 53a2fc889446..394f46331afe 100644 --- a/test/files/run/t5604.check +++ b/test/files/run/t5604.check @@ -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 diff --git a/test/files/run/t5604.flags b/test/files/run/t5604.flags new file mode 100644 index 000000000000..dcc59ebe32ef --- /dev/null +++ b/test/files/run/t5604.flags @@ -0,0 +1 @@ +-deprecation diff --git a/test/scaladoc/run/package-object.check b/test/scaladoc/run/package-object.check index 7da897a4f2c1..c04bb10ab316 100644 --- a/test/scaladoc/run/package-object.check +++ b/test/scaladoc/run/package-object.check @@ -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))