Skip to content

Commit a82c0fb

Browse files
committed
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.
1 parent 868d199 commit a82c0fb

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
544544
// add type to term nodes; replace type nodes with their types unless -Yprint-pos is also set.
545545
def tp = tree.typeOpt match {
546546
case tp: TermRef if tree.isInstanceOf[RefTree] && !tp.denot.isOverloaded => tp.underlying
547+
case tp: ConstantType if homogenizedView =>
548+
// constant folded types are forgotten in Tasty, are reconstituted subsequently in FirstTransform.
549+
// Therefore we have to gloss over this when comparing before/after pickling by widening to
550+
// underlying type `T`, or, if expression is a unary primitive operation, to `=> T`.
551+
tree match {
552+
case Select(qual, _) if qual.typeOpt.widen.typeSymbol.isPrimitiveValueClass =>
553+
ExprType(tp.widen)
554+
case _ => tp.widen
555+
}
547556
case tp => tp
548557
}
549558
if (!suppressTypes)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ class Typer extends Namer
437437
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
438438
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
439439
val select = typedSelect(tree, pt, qual1)
440-
if (select.tpe ne TryDynamicCallType) checkStableIdentPattern(select, pt)
440+
if (select.tpe ne TryDynamicCallType) ConstFold(checkStableIdentPattern(select, pt))
441441
else if (pt.isInstanceOf[PolyProto] || pt.isInstanceOf[FunProto] || pt == AssignProto) select
442442
else typedDynamicSelect(tree, Nil, pt)
443443
}

0 commit comments

Comments
 (0)