summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@typesafe.com>2014-08-26 19:56:26 +0200
committerLukas Rytz <lukas.rytz@typesafe.com>2014-08-26 19:56:26 +0200
commitb9048bb8a7d91e032eb57afa3ec12d3987980fe9 (patch)
tree56f24e877becd5ae9bd39825621f4f8db1da6116 /src
parenteb741483c44290f0fcfb0c6abd99584d9fafe4b7 (diff)
parent756e551b30461b548ea59e99562634775b7ebd74 (diff)
downloadscala-b9048bb8a7d91e032eb57afa3ec12d3987980fe9.tar.gz
scala-b9048bb8a7d91e032eb57afa3ec12d3987980fe9.tar.bz2
scala-b9048bb8a7d91e032eb57afa3ec12d3987980fe9.zip
Merge pull request #3905 from gourlaysama/wip/t5691-2
SI-5691 lint warning when a type parameter shadows an existing type.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/settings/Warnings.scala3
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala17
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala6
3 files changed, 26 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/settings/Warnings.scala b/src/compiler/scala/tools/nsc/settings/Warnings.scala
index bec068b56a..4c37633301 100644
--- a/src/compiler/scala/tools/nsc/settings/Warnings.scala
+++ b/src/compiler/scala/tools/nsc/settings/Warnings.scala
@@ -38,6 +38,7 @@ trait Warnings {
warnMissingInterpolator,
warnDocDetached,
warnPrivateShadow,
+ warnTypeParameterShadow,
warnPolyImplicitOverload,
warnOptionImplicit,
warnDelayedInit,
@@ -83,6 +84,8 @@ trait Warnings {
"A ScalaDoc comment appears to be detached from its element.")
val warnPrivateShadow = lintflag("private-shadow",
"A private field (or class parameter) shadows a superclass field.")
+ val warnTypeParameterShadow = lintflag("type-parameter-shadow",
+ "A local type parameter shadows a type already in scope.")
val warnPolyImplicitOverload = lintflag("poly-implicit-overload",
"Parameterized overloaded implicit methods are not visible as view bounds")
val warnOptionImplicit = lintflag("option-implicit",
diff --git a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
index 89b064e7c1..8d29d28908 100644
--- a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
@@ -600,6 +600,23 @@ trait TypeDiagnostics {
)
}
+ // 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) {
+ def enclClassOrMethodOrTypeMember(c: Context): Context =
+ if (!c.owner.exists || c.owner.isClass || c.owner.isMethod || (c.owner.isType && !c.owner.isParameter)) c
+ else enclClassOrMethodOrTypeMember(c.outer)
+
+ val tt = tparams.filter(_.name != typeNames.WILDCARD).foreach { tp =>
+ // we don't care about type params shadowing other type params in the same declaration
+ enclClassOrMethodOrTypeMember(context).outer.lookupSymbol(tp.name, s => s != tp.symbol && s.hasRawInfo && reallyExists(s)) match {
+ case LookupSucceeded(_, sym2) => context.warning(tp.pos,
+ s"type parameter ${tp.name} defined in $sym shadows $sym2 defined in ${sym2.owner}. You may want to rename your type parameter, or possibly remove it.")
+ case _ =>
+ }
+ }
+ }
+
/** Report a type error.
*
* @param pos The position where to report the error
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 0621fbefe4..422b940cd3 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -1726,6 +1726,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
|you want, you must write the annotation class in Java.""".stripMargin)
}
+ warnTypeParameterShadow(tparams1, clazz)
+
if (!isPastTyper) {
for (ann <- clazz.getAnnotation(DeprecatedAttr)) {
val m = companionSymbolOf(clazz, context)
@@ -2128,6 +2130,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
val tparams1 = ddef.tparams mapConserve typedTypeDef
val vparamss1 = ddef.vparamss mapConserve (_ mapConserve typedValDef)
+ warnTypeParameterShadow(tparams1, meth)
+
meth.annotations.map(_.completeInfo())
for (vparams1 <- vparamss1; vparam1 <- vparams1 dropRight 1)
@@ -2204,6 +2208,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
val typedMods = typedModifiers(tdef.mods)
tdef.symbol.annotations.map(_.completeInfo())
+ warnTypeParameterShadow(tparams1, tdef.symbol)
+
// @specialized should not be pickled when compiling with -no-specialize
if (settings.nospecialization && currentRun.compiles(tdef.symbol)) {
tdef.symbol.removeAnnotation(definitions.SpecializedClass)