Skip to content

Commit

Permalink
Deprecate nested class shadowing
Browse files Browse the repository at this point in the history
Fixes scala/bug#8353
Fixes scala/bug#11360

This deprecates nested class shadowing for Dotty compatibilty.
  • Loading branch information
eed3si9n committed Feb 8, 2020
1 parent d1b3235 commit 0fd836c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
Expand Up @@ -769,6 +769,23 @@ trait TypeDiagnostics {
|If applicable, you may wish to try moving some members into another object.""".stripMargin
}

def warnNestedClassShadow(sym: Symbol): Unit =
if (!isPastTyper && !sym.isSynthetic && sym.isNestedClass) {
def enclClass(c: Context): Context =
if (!c.owner.exists || c.owner.isClass) c
else enclClass(c.outer)
val outerOwner = enclClass(context).outer.owner
outerOwner.info.baseClasses
.filter(x => x != outerOwner && x != AnyRefClass && x != AnyClass && x != ObjectClass)
.foreach(base => {
val sym2 = base.info.decl(sym.name)
if (sym2 != NoSymbol && sym2 != sym) {
val msg = s"class shadowing is deprecated but class ${sym.name} shadows $sym2 defined in ${sym2.owner}; rename the class to something else"
context.deprecationWarning(sym.pos, sym, msg, "2.13.2")
}
})
}

// warn about class/method/type-members' type parameters that shadow types already in scope
def warnTypeParameterShadow(tparams: List[TypeDef], sym: Symbol): Unit =
if (settings.warnTypeParameterShadow && !isPastTyper && !sym.isSynthetic) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Expand Up @@ -1900,6 +1900,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
checkEphemeral(clazz, impl2.body)

warnTypeParameterShadow(tparams1, clazz)
warnNestedClassShadow(clazz)

if (!isPastTyper) {
for (ann <- clazz.getAnnotation(DeprecatedAttr)) {
Expand Down
6 changes: 6 additions & 0 deletions test/files/neg/nested-class-shadowing.check
@@ -0,0 +1,6 @@
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
class Status extends super.Status
^
error: No warnings can be incurred under -Werror.
1 warning
1 error
10 changes: 10 additions & 0 deletions test/files/neg/nested-class-shadowing.scala
@@ -0,0 +1,10 @@
// scalac: -Werror -Xlint:deprecation
//

trait Core {
class Status
}

trait Ext extends Core {
class Status extends super.Status
}

0 comments on commit 0fd836c

Please sign in to comment.