Skip to content

Commit 7490001

Browse files
committed
Re-constant-fold when rechecking
This is necessary since not all constant folded terms are converted to literals. The conversion does not happen if one of the operands is impure. A test case is run/final-field.scala
1 parent 424c719 commit 7490001

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

compiler/src/dotty/tools/dotc/transform/Recheck.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import NullOpsDecorator.stripNull
1313
import typer.ErrorReporting.err
1414
import typer.ProtoTypes.*
1515
import typer.TypeAssigner.seqLitType
16+
import typer.ConstFold
1617
import config.Printers.recheckr
1718
import util.Property
1819
import StdNames.nme
@@ -70,6 +71,11 @@ abstract class Recheck extends Phase, IdentityDenotTransformer:
7071
sym.updateInfo(reinferResult(sym.info))
7172
case _ =>
7273

74+
def constFold(tree: Tree, tp: Type)(using Context): Type =
75+
val tree1 = tree.withType(tp)
76+
val tree2 = ConstFold(tree1)
77+
if tree2 ne tree1 then tree2.tpe else tp
78+
7379
def recheckIdent(tree: Ident)(using Context): Type =
7480
tree.tpe
7581

@@ -83,7 +89,7 @@ abstract class Recheck extends Phase, IdentityDenotTransformer:
8389
val mbr = qualType.findMember(name, qualType,
8490
excluded = if tree.symbol.is(Private) then EmptyFlags else Private
8591
).suchThat(tree.symbol ==)
86-
qualType.select(name, mbr)
92+
constFold(tree, qualType.select(name, mbr))
8793

8894
def recheckBind(tree: Bind, pt: Type)(using Context): Type = tree match
8995
case Bind(name, body) =>
@@ -147,14 +153,14 @@ abstract class Recheck extends Phase, IdentityDenotTransformer:
147153
assert(formals.isEmpty)
148154
Nil
149155
val argTypes = recheckArgs(tree.args, formals, fntpe.paramRefs)
150-
fntpe.instantiate(argTypes)
156+
constFold(tree, fntpe.instantiate(argTypes))
151157

152158
def recheckTypeApply(tree: TypeApply, pt: Type)(using Context): Type =
153159
recheck(tree.fun).widen match
154160
case fntpe: PolyType =>
155161
assert(sameLength(fntpe.paramInfos, tree.args))
156162
val argTypes = tree.args.map(recheck(_))
157-
fntpe.instantiate(argTypes)
163+
constFold(tree, fntpe.instantiate(argTypes))
158164

159165
def recheckTyped(tree: Typed)(using Context): Type =
160166
val tptType = recheck(tree.tpt)

0 commit comments

Comments
 (0)