Skip to content

Commit

Permalink
scalac: warn when type parameters are shadowed (#4937)
Browse files Browse the repository at this point in the history
as brought up in #4936 it can
lead to confusing situations if type parameters are shadowed, so for
the sake of readability and disambiguity alone we should enable this
compiler warning IMO.

That being said, I'd like to stress that it's not something
fundamentally complicated, afaics. Simple example copied from #4936:
```scala
class Example[NodeType <: Object](node: NodeType) extends AnyVal{
  def foo[NodeType](x: List[NodeType]): List[NodeType] = x
}
```

These two type parameters in your Example class are not related at all. They happen to have the same name, which only means that within `def foo` we cannot reference the `NodeType` type from the `class Example`, but apart from that there's no connection at all between them.

Semantically it's similar to something like this on the value level: the two `bar` variables are not related at all, they just happen to have the same name, and therefor we cannot reference the class-level `bar` variable from within `def baz`.
```scala
class Foo {
    val bar = 42

    def baz: Unit = {
      val bar = "123"
      ()
  }
}
```
  • Loading branch information
mpollmeier authored Sep 19, 2024
1 parent 17df32d commit 351c619
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ ThisBuild / compile / javacOptions ++= Seq(
ThisBuild / scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"--release",
"11"
"11",
"-Wshadow:type-parameter-shadow",
)

lazy val createDistribution = taskKey[File]("Create a complete Joern distribution")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ class Scope {

def popNamespaceScope(): NamespaceScope = popScope[NamespaceScope]()

private def popScope[ScopeType <: JavaScopeElement](): ScopeType = {
private def popScope[ScopeType0 <: JavaScopeElement](): ScopeType0 = {
val scope = scopeStack.head
scopeStack = scopeStack.tail
scope.asInstanceOf[ScopeType]
scope.asInstanceOf[ScopeType0]
}

def addTopLevelType(name: String, typeFullName: String): Unit = {
Expand Down

0 comments on commit 351c619

Please sign in to comment.