Skip to content

Commit dc5c343

Browse files
authored
Merge pull request #4614 from dotty-staging/change-constfold-unary
Constant-fold unary operations in Typer
2 parents 228b232 + ef827b5 commit dc5c343

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
169169
}
170170

171171
homogenize(tp) match {
172-
case x: ConstantType if homogenizedView =>
173-
return toText(x.widen)
174172
case AppliedType(tycon, args) =>
175173
val cls = tycon.typeSymbol
176174
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
@@ -544,6 +542,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
544542
// add type to term nodes; replace type nodes with their types unless -Yprint-pos is also set.
545543
def tp = tree.typeOpt match {
546544
case tp: TermRef if tree.isInstanceOf[RefTree] && !tp.denot.isOverloaded => tp.underlying
545+
case tp: ConstantType if homogenizedView =>
546+
// constant folded types are forgotten in Tasty, are reconstituted subsequently in FirstTransform.
547+
// Therefore we have to gloss over this when comparing before/after pickling by widening to
548+
// underlying type `T`, or, if expression is a unary primitive operation, to `=> T`.
549+
tree match {
550+
case Select(qual, _) if qual.typeOpt.widen.typeSymbol.isPrimitiveValueClass =>
551+
ExprType(tp.widen)
552+
case _ => tp.widen
553+
}
547554
case tp => tp
548555
}
549556
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)