Skip to content

Commit

Permalink
wip move the implementation to RefChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Feb 8, 2020
1 parent 396557f commit eb0f13b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
21 changes: 20 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ abstract class RefChecks extends Transform {
* that are not implemented in a subclass.
* 4. Check that every member with an `override` modifier
* overrides some other member.
* 5. Check that the nested class do not shadow other nested classes from outer class's parent.
*/
private def checkAllOverrides(clazz: Symbol, typesOnly: Boolean = false): Unit = {
val self = clazz.thisType
Expand Down Expand Up @@ -832,7 +833,25 @@ abstract class RefChecks extends Transform {
}
member resetFlag (OVERRIDE | ABSOVERRIDE) // Any Override
}
}

// 5. Check that the nested class do not shadow other nested classes from outer class's parent
def checkNestedClassShadow(): Unit =
if (clazz.isNestedClass) {
val outers = clazz.enclClassChain.filterNot(_ == clazz)
outers foreach { outer =>
outer.baseClasses
.filter(x => x != outer && x != AnyRefClass && x != AnyClass && x != ObjectClass)
.foreach(base => {
val sym2 = base.info.decl(clazz.name)
if (sym2 != NoSymbol && sym2 != clazz && sym2.isClass) {
val msg = s"class shadowing is deprecated but class ${clazz.name} shadows $sym2 defined in ${sym2.owner}; rename the class to something else"
currentRun.reporting.deprecationWarning(clazz.pos, clazz, msg, "2.13.2")
}
})
}
}
checkNestedClassShadow()
} // end checkAllOverrides

// Basetype Checking --------------------------------------------------------

Expand Down
17 changes: 0 additions & 17 deletions src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -769,23 +769,6 @@ 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 && sym2.isClass) {
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: 0 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,6 @@ 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

0 comments on commit eb0f13b

Please sign in to comment.