Skip to content

Commit c1809bc

Browse files
authored
Map class literal constant types (#16988)
2 parents 1e981ab + 92118c4 commit c1809bc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,6 +5735,12 @@ object Types {
57355735
case tp @ SuperType(thistp, supertp) =>
57365736
derivedSuperType(tp, this(thistp), this(supertp))
57375737

5738+
case tp @ ConstantType(const @ Constant(_: Type)) =>
5739+
val classType = const.tpe
5740+
val classType1 = this(classType)
5741+
if classType eq classType1 then tp
5742+
else classType1
5743+
57385744
case tp: LazyRef =>
57395745
LazyRef { refCtx =>
57405746
given Context = refCtx

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ object Formatting {
7171
given Show[TypeComparer.ApproxState] with
7272
def show(x: TypeComparer.ApproxState) = TypeComparer.ApproxState.Repr.show(x)
7373

74+
given Show[ast.TreeInfo.PurityLevel] with
75+
def show(x: ast.TreeInfo.PurityLevel) = x match
76+
case ast.TreeInfo.Path => "PurityLevel.Path"
77+
case ast.TreeInfo.Pure => "PurityLevel.Pure"
78+
case ast.TreeInfo.Idempotent => "PurityLevel.Idempotent"
79+
case ast.TreeInfo.Impure => "PurityLevel.Impure"
80+
case ast.TreeInfo.PurePath => "PurityLevel.PurePath"
81+
case ast.TreeInfo.IdempotentPath => "PurityLevel.IdempotentPath"
82+
case _ => s"PurityLevel(${x.x})"
83+
7484
given Show[Showable] = ShowAny
7585
given Show[Shown] = ShowAny
7686
given Show[Int] = ShowAny

tests/pos/i16954.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Test:
2+
def test =
3+
classOf[Test]
4+
5+
def blck =
6+
class Blck
7+
val cls = classOf[Blck]
8+
cls
9+
10+
def expr =
11+
class Expr
12+
classOf[Expr] // was: "assertion failed: leak: Expr in { [..] }" crash
13+
14+
object Test extends Test:
15+
def main(args: Array[String]): Unit =
16+
assert(test.getName == "Test", test.getName)
17+
assert(blck.getName == "Test$Blck$1", blck.getName)
18+
assert(expr.getName == "Test$Expr$1", expr.getName)

0 commit comments

Comments
 (0)