Skip to content

Commit 321d0d2

Browse files
authored
Refactor constant folding of applications (#20099)
Move them in typedApply/typedTypeApply instead of leaving them until adapt. This aligns these folds with folds of unary operations, which are done already in typedSelect and avoids potentially several calls to ConstFold when arguments are passed to overloaded methods.
2 parents 4daf050 + 1b61ed6 commit 321d0d2

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1124,12 +1124,13 @@ trait Applications extends Compatibility {
11241124
}
11251125
app
11261126
}
1127-
app1 match {
1127+
val app2 = app1 match {
11281128
case Apply(Block(stats, fn), args) =>
11291129
tpd.cpy.Block(app1)(stats, tpd.cpy.Apply(app1)(fn, args))
11301130
case _ =>
11311131
app1
11321132
}
1133+
ConstFold(app2)
11331134
}
11341135

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4196,7 +4196,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41964196

41974197
def adaptToSubType(wtp: Type): Tree =
41984198
// try converting a constant to the target type
4199-
ConstFold(tree).tpe.widenTermRefExpr.normalized match
4199+
tree.tpe.widenTermRefExpr.normalized match
42004200
case ConstantType(x) =>
42014201
val converted = x.convertTo(pt)
42024202
if converted != null && (converted ne x) then

tests/pos/constfold.scala

+8
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)