From a82c0fb78a3a6cd138546b4cd7d2c7f53866dd46 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 2 Jun 2018 14:57:26 +0200 Subject: [PATCH 1/2] Constant-fold unary operations in Typer This was previously forgotten, even though unary operations such as `!` and `~` were folded later in FirstTransform. As a consequence, inlining simplifications involving constant expressions using these operations were not done. This affected in particular the trace macro, which was always expanded to an operation taking a closure argument. --- .../src/dotty/tools/dotc/printing/RefinedPrinter.scala | 9 +++++++++ compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 6fd5da3fc623..06c0f214f35f 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -544,6 +544,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { // add type to term nodes; replace type nodes with their types unless -Yprint-pos is also set. def tp = tree.typeOpt match { case tp: TermRef if tree.isInstanceOf[RefTree] && !tp.denot.isOverloaded => tp.underlying + case tp: ConstantType if homogenizedView => + // constant folded types are forgotten in Tasty, are reconstituted subsequently in FirstTransform. + // Therefore we have to gloss over this when comparing before/after pickling by widening to + // underlying type `T`, or, if expression is a unary primitive operation, to `=> T`. + tree match { + case Select(qual, _) if qual.typeOpt.widen.typeSymbol.isPrimitiveValueClass => + ExprType(tp.widen) + case _ => tp.widen + } case tp => tp } if (!suppressTypes) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9e34deeb2555..6a6b655f239d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -437,7 +437,7 @@ class Typer extends Namer val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this)) if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos) val select = typedSelect(tree, pt, qual1) - if (select.tpe ne TryDynamicCallType) checkStableIdentPattern(select, pt) + if (select.tpe ne TryDynamicCallType) ConstFold(checkStableIdentPattern(select, pt)) else if (pt.isInstanceOf[PolyProto] || pt.isInstanceOf[FunProto] || pt == AssignProto) select else typedDynamicSelect(tree, Nil, pt) } From ef827b548df75ad356c0620643ba6e1ad280de9d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 2 Jun 2018 15:27:12 +0200 Subject: [PATCH 2/2] Drop redundant homogenization --- compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 06c0f214f35f..7b333e3a4c51 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -169,8 +169,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { } homogenize(tp) match { - case x: ConstantType if homogenizedView => - return toText(x.widen) case AppliedType(tycon, args) => val cls = tycon.typeSymbol if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"