Skip to content

Commit 86bb4cf

Browse files
Backport "Refactor constant folding of applications" to LTS (#21043)
Backports #20099 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents d57fb04 + 2b642b2 commit 86bb4cf

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,13 @@ trait Applications extends Compatibility {
11061106
}
11071107
app
11081108
}
1109-
app1 match {
1109+
val app2 = app1 match {
11101110
case Apply(Block(stats, fn), args) =>
11111111
tpd.cpy.Block(app1)(stats, tpd.cpy.Apply(app1)(fn, args))
11121112
case _ =>
11131113
app1
11141114
}
1115+
ConstFold(app2)
11151116
}
11161117

11171118
/** Typecheck an Apply node with a typed function and possibly-typed arguments coming from `proto` */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4129,7 +4129,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41294129

41304130
def adaptToSubType(wtp: Type): Tree =
41314131
// try converting a constant to the target type
4132-
ConstFold(tree).tpe.widenTermRefExpr.normalized match
4132+
tree.tpe.widenTermRefExpr.normalized match
41334133
case ConstantType(x) =>
41344134
val converted = x.convertTo(pt)
41354135
if converted != null && (converted ne x) then

tests/pos/constfold.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ object Test extends App {
1515
Console.println(A.y);
1616
Console.println(A.z);
1717
Console.println(A.s);
18+
19+
def f(x: 12): Int = 1
20+
def f(x: Int): Double = 2
21+
val x = f(12)
22+
val _: Int = x
23+
val y = f(2 * 6)
24+
val _: Int = x
25+
1826
}

0 commit comments

Comments
 (0)