Skip to content

Commit f21a26d

Browse files
committed
Deprecate nested class shadowing
Fixes scala/bug#8353 Fixes scala/bug#11360 This deprecates nested class shadowing for Dotty compatibilty.
1 parent fb0c104 commit f21a26d

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,23 @@ trait TypeDiagnostics {
769769
|If applicable, you may wish to try moving some members into another object.""".stripMargin
770770
}
771771

772+
def warnNestedClassShadow(sym: Symbol): Unit =
773+
if (!isPastTyper && !sym.isSynthetic && sym.isNestedClass) {
774+
def enclClass(c: Context): Context =
775+
if (!c.owner.exists || c.owner.isClass) c
776+
else enclClass(c.outer)
777+
val outerOwner = enclClass(context).outer.owner
778+
outerOwner.info.baseClasses
779+
.filter(x => x != outerOwner && x != AnyRefClass && x != AnyClass && x != ObjectClass)
780+
.foreach(base => {
781+
val sym2 = base.info.decl(sym.name)
782+
if (sym2 != NoSymbol && sym2 != sym) {
783+
val msg = s"class shadowing is deprecated but class ${sym.name} shadows $sym2 defined in ${sym2.owner}; rename the class to something else"
784+
context.deprecationWarning(sym.pos, sym, msg, "2.13.2")
785+
}
786+
})
787+
}
788+
772789
// warn about class/method/type-members' type parameters that shadow types already in scope
773790
def warnTypeParameterShadow(tparams: List[TypeDef], sym: Symbol): Unit =
774791
if (settings.warnTypeParameterShadow && !isPastTyper && !sym.isSynthetic) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
19001900
checkEphemeral(clazz, impl2.body)
19011901

19021902
warnTypeParameterShadow(tparams1, clazz)
1903+
warnNestedClassShadow(clazz)
19031904

19041905
if (!isPastTyper) {
19051906
for (ann <- clazz.getAnnotation(DeprecatedAttr)) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
nested-class-shadowing.scala:9: warning: class shadowing is deprecated but class Status shadows class Status defined in trait Core; rename the class to something else
2+
class Status extends super.Status
3+
^
4+
error: No warnings can be incurred under -Werror.
5+
1 warning
6+
1 error
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// scalac: -Werror -Xlint:deprecation
2+
//
3+
4+
trait Core {
5+
class Status
6+
}
7+
8+
trait Ext extends Core {
9+
class Status extends super.Status
10+
}

0 commit comments

Comments
 (0)