Skip to content

Commit

Permalink
Backport "Refactor constant folding of applications" to LTS (#21043)
Browse files Browse the repository at this point in the history
Backports #20099 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 5, 2024
2 parents d57fb04 + 2b642b2 commit 86bb4cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,13 @@ trait Applications extends Compatibility {
}
app
}
app1 match {
val app2 = app1 match {
case Apply(Block(stats, fn), args) =>
tpd.cpy.Block(app1)(stats, tpd.cpy.Apply(app1)(fn, args))
case _ =>
app1
}
ConstFold(app2)
}

/** Typecheck an Apply node with a typed function and possibly-typed arguments coming from `proto` */
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4129,7 +4129,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

def adaptToSubType(wtp: Type): Tree =
// try converting a constant to the target type
ConstFold(tree).tpe.widenTermRefExpr.normalized match
tree.tpe.widenTermRefExpr.normalized match
case ConstantType(x) =>
val converted = x.convertTo(pt)
if converted != null && (converted ne x) then
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/constfold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ object Test extends App {
Console.println(A.y);
Console.println(A.z);
Console.println(A.s);

def f(x: 12): Int = 1
def f(x: Int): Double = 2
val x = f(12)
val _: Int = x
val y = f(2 * 6)
val _: Int = x

}

0 comments on commit 86bb4cf

Please sign in to comment.