From 92118c4201b96653f25d06f031a221a8a8e48559 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 21 Feb 2023 19:05:06 +0000 Subject: [PATCH] Avoid in ConstantTypes --- compiler/src/dotty/tools/dotc/core/Types.scala | 6 ++++++ .../dotty/tools/dotc/printing/Formatting.scala | 10 ++++++++++ tests/pos/i16954.scala | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/pos/i16954.scala diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index d005cabebcfb..f53763cdf0bf 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -5736,6 +5736,12 @@ object Types { case tp @ SuperType(thistp, supertp) => derivedSuperType(tp, this(thistp), this(supertp)) + case tp @ ConstantType(const @ Constant(_: Type)) => + val classType = const.tpe + val classType1 = this(classType) + if classType eq classType1 then tp + else classType1 + case tp: LazyRef => LazyRef { refCtx => given Context = refCtx diff --git a/compiler/src/dotty/tools/dotc/printing/Formatting.scala b/compiler/src/dotty/tools/dotc/printing/Formatting.scala index 0acfd06de433..3f32b29654c9 100644 --- a/compiler/src/dotty/tools/dotc/printing/Formatting.scala +++ b/compiler/src/dotty/tools/dotc/printing/Formatting.scala @@ -71,6 +71,16 @@ object Formatting { given Show[TypeComparer.ApproxState] with def show(x: TypeComparer.ApproxState) = TypeComparer.ApproxState.Repr.show(x) + given Show[ast.TreeInfo.PurityLevel] with + def show(x: ast.TreeInfo.PurityLevel) = x match + case ast.TreeInfo.Path => "PurityLevel.Path" + case ast.TreeInfo.Pure => "PurityLevel.Pure" + case ast.TreeInfo.Idempotent => "PurityLevel.Idempotent" + case ast.TreeInfo.Impure => "PurityLevel.Impure" + case ast.TreeInfo.PurePath => "PurityLevel.PurePath" + case ast.TreeInfo.IdempotentPath => "PurityLevel.IdempotentPath" + case _ => s"PurityLevel(${x.x})" + given Show[Showable] = ShowAny given Show[Shown] = ShowAny given Show[Int] = ShowAny diff --git a/tests/pos/i16954.scala b/tests/pos/i16954.scala new file mode 100644 index 000000000000..ce7418e5e5e2 --- /dev/null +++ b/tests/pos/i16954.scala @@ -0,0 +1,18 @@ +class Test: + def test = + classOf[Test] + + def blck = + class Blck + val cls = classOf[Blck] + cls + + def expr = + class Expr + classOf[Expr] // was: "assertion failed: leak: Expr in { [..] }" crash + +object Test extends Test: + def main(args: Array[String]): Unit = + assert(test.getName == "Test", test.getName) + assert(blck.getName == "Test$Blck$1", blck.getName) + assert(expr.getName == "Test$Expr$1", expr.getName)