Skip to content

Commit ff32921

Browse files
committed
Restore previous CheckShadowing with tweaks
1 parent e424d7d commit ff32921

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

compiler/src/dotty/tools/dotc/transform/CheckShadowing.scala

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ class CheckShadowing extends MiniPhase:
8787
)
8888

8989
override def prepareForTypeDef(tree: tpd.TypeDef)(using Context): Context =
90+
val sym = tree.symbol
9091
if tree.symbol.isAliasType then // if alias, the parent is the current symbol
9192
nestedTypeTraverser(tree.symbol).traverse(tree.rhs)
9293
if tree.symbol.is(Param) then // if param, the parent is up
93-
val enclosing =
94-
val owner = tree.symbol.ownersIterator.dropWhile(_.is(Param)).next()
95-
if owner.isConstructor then owner.owner else owner
96-
nestedTypeTraverser(enclosing).traverse(tree.rhs)(using ctx.outer)
97-
shadowingDataApply(_.registerCandidate(enclosing, tree))
98-
else
99-
ctx
94+
val owner = tree.symbol.owner
95+
val parent = if (owner.isConstructor) then owner.owner else owner
96+
nestedTypeTraverser(parent).traverse(tree.rhs)(using ctx.outer)
97+
if isValidTypeParamOwner(tree.symbol.owner) then
98+
shadowingDataApply(sd => sd.registerCandidate(parent, tree))
99+
ctx
100100

101101
override def transformPackageDef(tree: tpd.PackageDef)(using Context): tpd.Tree =
102102
shadowingDataApply(sd => sd.outOfScope())
@@ -112,10 +112,8 @@ class CheckShadowing extends MiniPhase:
112112

113113
override def transformTypeDef(tree: tpd.TypeDef)(using Context): tpd.Tree =
114114
// Do not register for constructors the work is done for the Class owned equivalent TypeDef
115-
if tree.symbol.is(Param) then
116-
val owner = tree.symbol.ownersIterator.dropWhile(_.is(Param)).next()
117-
if isValidTypeParamOwner(owner) then
118-
shadowingDataApply(_.computeTypeParamShadowsFor(owner)(using ctx.outer))
115+
if tree.symbol.is(Param) && isValidTypeParamOwner(tree.symbol.owner) then
116+
shadowingDataApply(sd => sd.computeTypeParamShadowsFor(tree.symbol.owner)(using ctx.outer))
119117
// No need to start outer here, because the TypeDef reached here it's already the parent
120118
if tree.symbol.isAliasType then
121119
shadowingDataApply(sd => sd.computeTypeParamShadowsFor(tree.symbol)(using ctx))
@@ -140,7 +138,7 @@ class CheckShadowing extends MiniPhase:
140138

141139
override def traverse(tree: tpd.Tree)(using Context): Unit =
142140
tree match
143-
case t:tpd.TypeDef =>
141+
case t: tpd.TypeDef =>
144142
val newCtx = shadowingDataApply(sd =>
145143
sd.registerCandidate(parent, t)
146144
)
@@ -156,7 +154,7 @@ class CheckShadowing extends MiniPhase:
156154

157155
override def traverse(tree: tpd.Tree)(using Context): Unit =
158156
tree match
159-
case t:tpd.Import =>
157+
case t: tpd.Import =>
160158
val newCtx = shadowingDataApply(sd => sd.registerImport(t))
161159
traverseChildren(tree)(using newCtx)
162160
case _ =>
@@ -239,7 +237,7 @@ object CheckShadowing:
239237
val declarationScope = ctx.effectiveScope
240238
val res = declarationScope.lookup(symbol.name)
241239
res match
242-
case s: Symbol if s.isType => Some(s)
240+
case s: Symbol if s.isType && s != symbol => Some(s)
243241
case _ => lookForUnitShadowedType(symbol)(using ctx.outer)
244242

245243
/** Register if the valDef is a private declaration that shadows an inherited field */

tests/warn/i19657-mega.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
//> using options -Wshadow:type-parameter-shadow -Wunused:all
22

3-
class F[X, M[N[X]]]:
3+
class F[X, M[N[X]]]: // warn
44
private def x[X] = toString // warn // warn
5-
6-
// the first is spurious
7-
// at 3: Type parameter X for type M shadows the type defined by type X in class F
8-
// at 4: Type parameter X for method x shadows the type defined by type X in class F
9-
// at 4: unused private member

0 commit comments

Comments
 (0)