diff --git a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala index 18ff1ab41dca..17390f830ecf 100644 --- a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala @@ -926,10 +926,13 @@ object SymDenotations { * except for a toplevel module, where its module class is returned. */ final def topLevelClass(implicit ctx: Context): Symbol = { - def topLevel(d: SymDenotation): Symbol = { - if (d.isEffectiveRoot || (d is PackageClass) || (d.owner is PackageClass)) d.symbol - else topLevel(d.owner) - } + + def topLevel(d: SymDenotation): Symbol = + if (!exists || d.isEffectiveRoot || (d is PackageClass) || (d.owner is PackageClass)) + d.symbol + else + topLevel(d.owner) + val sym = topLevel(this) if (sym.isClass) sym else sym.moduleClass } diff --git a/tests/pos/i3647.scala b/tests/pos/i3647.scala new file mode 100644 index 000000000000..5bad3d5beb63 --- /dev/null +++ b/tests/pos/i3647.scala @@ -0,0 +1,21 @@ +object App { + def main(args: Array[String]): Unit = { + trait FooT { + type T + type Bar[A] + + def get(k: Bar[T]): String + } + val test: FooT = new FooT { + type T = String + type Bar[A] = J[A] + sealed abstract class J[A] + final case object JName extends J[T] + final case object JInt extends J[Int] + + def get(k: J[T]): String = k match { + case JName => "Age" + } + } + } +}