Skip to content

Commit 46f2db6

Browse files
committed
Adjust export and synth. case class constructor false-positive cases & Add Tests
1 parent 297183c commit 46f2db6

File tree

8 files changed

+127
-19
lines changed

8 files changed

+127
-19
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ class CheckShadowing extends MiniPhase:
116116
tree
117117

118118
override def transformTypeDef(tree: tpd.TypeDef)(using Context): tpd.Tree =
119-
if tree.symbol.is(Param) && !tree.symbol.owner.isConstructor then // Do not register for constructors the work is done for the Class owned equivalent TypeDef
119+
if tree.symbol.is(Param) && isValidTypeParamOwner(tree.symbol.owner) then // Do not register for constructors the work is done for the Class owned equivalent TypeDef
120120
shadowingDataApply(sd => sd.computeTypeParamShadowsFor(tree.symbol.owner)(using ctx.outer))
121121
if tree.symbol.isAliasType then // No need to start outer here, because the TypeDef reached here it's already the parent
122122
shadowingDataApply(sd => sd.computeTypeParamShadowsFor(tree.symbol)(using ctx))
123123
tree
124124

125125
// Helpers :
126+
private def isValidTypeParamOwner(owner: Symbol)(using Context): Boolean =
127+
!owner.isConstructor && !owner.is(Synthetic) && !owner.is(Exported)
126128

127129
private def reportShadowing(res: ShadowingData.ShadowResult)(using Context): Unit =
128130
res.warnings.sortBy(w => (w.pos.line, w.pos.startPos.column))(using Ordering[(Int, Int)]).foreach { s =>
@@ -211,7 +213,7 @@ object CheckShadowing:
211213
val actual = typeParamCandidates.getOrElseUpdate(parent, Seq())
212214
typeParamCandidates.update(parent, actual.+:(typeDef))
213215

214-
/** Compute if there is some TypeParam shadowing and register if it is the case*/
216+
/** Compute if there is some TypeParam shadowing and register if it is the case */
215217
def computeTypeParamShadowsFor(parent: Symbol)(using Context): Unit =
216218
typeParamCandidates(parent).foreach(typeDef => {
217219
val sym = typeDef.symbol
@@ -230,7 +232,7 @@ object CheckShadowing:
230232

231233
private def lookForImportedShadowedType(symbol: Symbol)(using Context): Option[Symbol] =
232234
explicitsImports
233-
.flatMap(_.flatMap(imp => symbol.isInImport(imp)))
235+
.flatMap(_.flatMap(imp => symbol.isAnImportedType(imp)))
234236
.headOption
235237

236238
private def lookForUnitShadowedType(symbol: Symbol)(using Context): Option[Symbol] =
@@ -278,8 +280,8 @@ object CheckShadowing:
278280
ShadowResult(privateWarnings ++ typeParamWarnings)
279281

280282
extension (sym: Symbol)
281-
/** Given an import and accessibility, return the import's symbol that matches import<->this symbol */
282-
private def isInImport(imp: tpd.Import)(using Context): Option[Symbol] =
283+
/** Looks after any type import symbol in the given import that matches this symbol */
284+
private def isAnImportedType(imp: tpd.Import)(using Context): Option[Symbol] =
283285
val tpd.Import(qual, sels) = imp
284286
val simpleSelections = qual.tpe.member(sym.name).alternatives
285287
val typeSelections = sels.flatMap(n => qual.tpe.member(n.name.toTypeName).alternatives)

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CompilationTests {
3434
compileFile("tests/pos-special/utf8encoded.scala", explicitUTF8),
3535
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
3636
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
37+
compileFilesInDir("tests/pos", defaultOptions.and("-Xlint:private-shadow", "-Xlint:type-parameter-shadow")),
3738
compileFile("tests/pos-special/sourcepath/outer/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
3839
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-deprecation", "-feature")),
3940
compileFilesInDir("tests/pos-special/spec-t5545", defaultOptions),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:21:15 ---------------------------------------------
2+
21 | class Derived(x : Int, x3: Int, y: Int, z2: Int) extends BaseB, BaseC(x3), Base(x, y + 1, z2): // error // error / for x, y translated to private[this] x field & shadowing var Base.x, Base.y
3+
| ^
4+
| value x in class Derived shadows field x inherited from trait Base
5+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:21:33 ---------------------------------------------
6+
21 | class Derived(x : Int, x3: Int, y: Int, z2: Int) extends BaseB, BaseC(x3), Base(x, y + 1, z2): // error // error / for x, y translated to private[this] x field & shadowing var Base.x, Base.y
7+
| ^
8+
| value y in class Derived shadows field y inherited from trait Base
9+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:23:2 ----------------------------------------------
10+
23 | private val shadowed2 = 2 + 2 // error (In Scala 2 we cannot do that got the warning)
11+
| ^
12+
| value shadowed2 in class Derived shadows field shadowed2 inherited from trait Base
13+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:24:2 ----------------------------------------------
14+
24 | private[this] val shadowed3 = 3 + 3 // error
15+
| ^
16+
| value shadowed3 in class Derived shadows field shadowed3 inherited from trait Base
17+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:26:2 ----------------------------------------------
18+
26 | private val shadowed5 = 5 + 5 // error
19+
| ^
20+
| value shadowed5 in class Derived shadows field shadowed5 inherited from trait Base
21+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:41:20 ---------------------------------------------
22+
41 | class UnderDerived(x: Int, y: Int, z: Int) extends Derived(x, 1, y, z) // error // error // error
23+
| ^
24+
| value x in class UnderDerived shadows field x inherited from trait Base
25+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:41:28 ---------------------------------------------
26+
41 | class UnderDerived(x: Int, y: Int, z: Int) extends Derived(x, 1, y, z) // error // error // error
27+
| ^
28+
| value y in class UnderDerived shadows field y inherited from trait Base
29+
-- Error: tests/neg-custom-args/fatal-warnings/i17612b/i17612b.scala:41:36 ---------------------------------------------
30+
41 | class UnderDerived(x: Int, y: Int, z: Int) extends Derived(x, 1, y, z) // error // error // error
31+
| ^
32+
| value z in class UnderDerived shadows field z inherited from trait Base
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// scalac: -Xlint:private-shadow
2+
3+
object i17612b:
4+
5+
trait Base(var x: Int, val y: Int, var z: Int):
6+
var shadowed2 = 2
7+
val shadowed3 = 3
8+
val shadowed4 = 4
9+
protected var shadowed5 = 5
10+
11+
val notShadowed = -1
12+
private val notShadowed2 = -2
13+
val notShadowedbyLambda = -2
14+
15+
def increment(): Unit =
16+
x = x + 1
17+
18+
trait BaseB
19+
trait BaseC(var x2: Int)
20+
21+
class Derived(x : Int, x3: Int, y: Int, z2: Int) extends BaseB, BaseC(x3), Base(x, y + 1, z2): // error // error / for x, y translated to private[this] x field & shadowing var Base.x, Base.y
22+
private def hello() = 4
23+
private val shadowed2 = 2 + 2 // error (In Scala 2 we cannot do that got the warning)
24+
private[this] val shadowed3 = 3 + 3 // error
25+
26+
private val shadowed5 = 5 + 5 // error
27+
private val notShadowed2 = -4
28+
29+
val lambda: Int => Int => Int =
30+
notShadowedbyLambda =>
31+
notShadowedbyLambda =>
32+
notShadowedbyLambda * 2
33+
34+
def inFunctionScope() =
35+
val notShadowed = -2 // OK
36+
-2
37+
38+
override def toString =
39+
s"x : ${x.toString}, y : ${y.toString}"
40+
41+
class UnderDerived(x: Int, y: Int, z: Int) extends Derived(x, 1, y, z) // error // error // error
42+
43+
def main(args: Array[String]) =
44+
val derived = new Derived(1, 1, 1, 1)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object importTry:
2+
3+
trait ImTrait
4+
5+
class ImClass

tests/neg-custom-args/fatal-warnings/i17613b.check

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,43 @@
1414
13 | type MySeq2[ImClass] = Seq[D] // error
1515
| ^^^^^^^
1616
| Type parameter ImClass for type MySeq2 shadows the type defined by class ImClass in object importTry
17-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:15:12 ---------------------------------------------
18-
15 | class Foo[T](t: T): // error class parameter shadows some other type
17+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:16:24 ---------------------------------------------
18+
16 | type TypeLambda[A] = [ImTrait] =>> Map[ImTrait, B] // error
19+
| ^^^^^^^
20+
| Type parameter ImTrait for type TypeLambda shadows the type defined by trait ImTrait in object importTry
21+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:17:21 ---------------------------------------------
22+
17 | type PolyFun[A] = [ImTrait] => ImTrait => B // error
23+
| ^^^^^^^
24+
| Type parameter ImTrait for type PolyFun shadows the type defined by trait ImTrait in object importTry
25+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:23:12 ---------------------------------------------
26+
23 | class Foo[T](t: T): // error class parameter shadows some other type
1927
| ^
2028
| Type parameter T for class Foo shadows the type defined by type T in class B
21-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:19:15 ---------------------------------------------
22-
19 | def intType[List1](x: T) = x.toString() // error
29+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:27:15 ---------------------------------------------
30+
27 | def intType[List1](x: T) = x.toString() // error
2331
| ^^^^^
2432
| Type parameter List1 for method intType shadows an explicitly renamed type : List1
25-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:23:12 ---------------------------------------------
26-
23 | class C[M[List[_]]] // error List not renamed here
33+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:32:10 ---------------------------------------------
34+
32 | given [Int]: Ordering[Int]() // error
35+
| ^^^
36+
| Type parameter Int for method given_Ordering_Int shadows the type defined by class Int in package scala
37+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:34:12 ---------------------------------------------
38+
34 | class C[M[List[_]]] // error List not renamed here
2739
| ^^^^^^^
2840
| Type parameter List for class C shadows the type defined by type List in package scala
29-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:24:11 ---------------------------------------------
30-
24 | type E[M[Int[_]]] = Int // error
41+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:35:11 ---------------------------------------------
42+
35 | type E[M[Int[_]]] = Int // error
3143
| ^^^^^^
3244
| Type parameter Int for type E shadows the type defined by class Int in package scala
33-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:26:14 ---------------------------------------------
34-
26 | def foo[N[M[List[_]]]] = // error
45+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:37:14 ---------------------------------------------
46+
37 | def foo[N[M[List[_]]]] = // error
3547
| ^^^^^^^
3648
| Type parameter List for method foo shadows the type defined by type List in package scala
37-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:29:11 ---------------------------------------------
38-
29 | type Z[ImClassR] = Int // error
49+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:40:11 ---------------------------------------------
50+
40 | type Z[ImClassR] = Int // error
3951
| ^^^^^^^^
4052
| Type parameter ImClassR for type Z shadows an explicitly renamed type : ImClassR
41-
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:30:18 ---------------------------------------------
42-
30 | class InnerCl[ImClassR] // error
53+
-- Error: tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala:41:18 ---------------------------------------------
54+
41 | class InnerCl[ImClassR] // error
4355
| ^^^^^^^^
4456
| Type parameter ImClassR for class InnerCl shadows an explicitly renamed type : ImClassR

tests/neg-custom-args/fatal-warnings/i17613b/i17613b.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ object i17613b:
1212
def foobar2[ImClass](in: D) = in.toString // error
1313
type MySeq2[ImClass] = Seq[D] // error
1414

15+
given [A]: Ordering[Int]()
16+
type TypeLambda[A] = [ImTrait] =>> Map[ImTrait, B] // error
17+
type PolyFun[A] = [ImTrait] => ImTrait => B // error
18+
type MatchType[A] = A match {
19+
case String => Int
20+
case ImTrait => Boolean
21+
}
22+
1523
class Foo[T](t: T): // error class parameter shadows some other type
1624
import scala.collection.immutable.{List => List1}
1725
def bar[List](w: T) = w.toString // no warning due to the explicit import renaming
@@ -20,6 +28,9 @@ object i17613b:
2028

2129
type Y[List] = Int // no warning
2230

31+
given [A]: Ordering[A]()
32+
given [Int]: Ordering[Int]() // error
33+
2334
class C[M[List[_]]] // error List not renamed here
2435
type E[M[Int[_]]] = Int // error
2536

tests/pos/i16339.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
sealed trait Get[X, +X2 <: X]
33
case class Bar[Y, Y2 <: Y](value: Y2) extends Get[Y, Y2]
44

5+
56
class Test:
67
def t1[Z, Z2 <: Z](get: Get[Z, Z2]) = get match
78
case Bar(_) =>

0 commit comments

Comments
 (0)