Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map class literal constant types #16988

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/Formatting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at

        val countsAsPure =
          if dropOp(tree1).symbol.isInlineVal
          then isIdempotentExpr(tree1)
          else isPureExpr(tree1)

in constToLiteral and needed to print purity levels.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be achieved by making PurityLevel an enum instead to generate a sensible toString?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps. But what's a nice way to reimplement min?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I hadn't considered that, nevermind then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could still be implemented as the toString of PurityLevel since it doesn't require a Context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it could.


given Show[Showable] = ShowAny
given Show[Shown] = ShowAny
given Show[Int] = ShowAny
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i16954.scala
Original file line number Diff line number Diff line change
@@ -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)