# How to get rid of Dropped Class Shadowing in Scala 3 Migration

**URL:** <https://contributors.scala-lang.org/t/how-to-get-rid-of-dropped-class-shadowing-in-scala-3-migration/7111>\
**Category:** Scala Platform\
**Created:** [April 25, 2025, 11:14am UTC](https://contributors.scala-lang.org/t/how-to-get-rid-of-dropped-class-shadowing-in-scala-3-migration/7111 "2025-04-25T11:14:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMatveev](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/amatveev/32/847_2.png) [@AMatveev](https://contributors.scala-lang.org/u/AMatveev)\
**Post date:** [April 25, 2025, 11:14am UTC](https://contributors.scala-lang.org/t/how-to-get-rid-of-dropped-class-shadowing-in-scala-3-migration/7111/1 "2025-04-25T11:14:46Z")

</div>

Scala 3 dropped [class shadowing](https://docs.scala-lang.org/scala3/reference/dropped-features/class-shadowing.html), so we need to rename some traits to migrate to scala3.

It is not an easy task when there are many such traits.

```scala
class Base {
  trait Card { }
}

class Sub extends Base {
  trait Card extends super.card{ }
}

```

## Stage 1

We should initially refactor the base modules in a way that allows refactoring of the submodules without causing any disruptions.

```scala
class Base {
   //mark for scalafix
  @Scala2Only
  trait Card { 
    //new name
    private def useInsted:"BaseCard" = "BaseCard"
  }
  // type to rename
  type BaseCard = Card
}

```

## Stage 2

Run scala fix on submodules.  
It will remove deprecated names:

```scala
class Sub extends Base {
  trait Card extends BaseCard
}

```

Scalafix example:

```scala
package fix

import scalafix.v1.{XtensionTreeScalafix, _}

import scala.meta._

class UpdateAvi extends SemanticRule("UpdateAvi") {
  override def fix(implicit doc: SemanticDocument): Patch = {
    Patch.fromIterable {
      doc.tree.collect { case traitDef: Defn.Trait =>
        traitDef.templ.inits.flatMap { init =>
          val newInitNameOpt = findReplacementName(
            parentSymbol = init.tpe.symbol
          )
          newInitNameOpt.map { newInitName =>
            Patch.replaceTree(init.tpe, newInitName)
          }
        }
      }.flatten
    }
  }

  private def findReplacementName(parentSymbol: Symbol)(implicit doc: SemanticDocument): Option[String] = {
    parentSymbol.info.flatMap { info =>
      lazy val hasAnnotate = (name: String) => info.annotations.exists(_.tpe.toString() == name)

      if (hasAnnotate("Scala2OnlyDvi")) {
        Some(s"Dvi${parentSymbol.displayName}")
      } else if (hasAnnotate("Scala2Only")) {
        info.signature match {
          case ClassSignature(_, _, _, declarations) =>
            declarations.collectFirst {
              case decl if decl.displayName == "useInstead" =>
                decl.signature.asInstanceOf[MethodSignature].returnType.asInstanceOf[ConstantType].constant.asInstanceOf[StringConstant].value
            }
          case _ => None
        }
      } else None
    }
  }
}

```

## Stage 3

Remove deprecaded code in base modules:

```scala
class Base {
  trait BaseCard{ }
}

```
