From 448f04836ba82b08ec3f84ce52227374e0c8b8e6 Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 5 Apr 2024 12:50:43 +0200 Subject: [PATCH 1/2] Refactor constant folding of applications Move them in typedApply/typedTypeApply instead of leaving them until adapt. This aligns these folds with folds of uniary operations, which are done already in typedSelect and avoids potentially several calls to ConstFold when arguments are passed to overloaded methods. [Cherry-picked 235c047315572b09f44cd79430215c6f817116fb] --- compiler/src/dotty/tools/dotc/typer/Applications.scala | 6 ++++-- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- tests/pos/constfold.scala | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index f337c72a405d..f4be915df072 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -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` */ @@ -1171,7 +1172,8 @@ trait Applications extends Compatibility { case _ => tree.withType(TryDynamicCallType) } if (typedFn.tpe eq TryDynamicCallType) tryDynamicTypeApply() - else assignType(cpy.TypeApply(tree)(typedFn, typedArgs), typedFn, typedArgs) + else + ConstFold(assignType(cpy.TypeApply(tree)(typedFn, typedArgs), typedFn, typedArgs)) } } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 0c230f8daf49..33c3f2de7aa8 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -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 diff --git a/tests/pos/constfold.scala b/tests/pos/constfold.scala index a45400d7f259..5a76d414032d 100644 --- a/tests/pos/constfold.scala +++ b/tests/pos/constfold.scala @@ -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 + } From 2b642b2945f5154d71e33b3a3270a4c59b69706a Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 8 Apr 2024 15:40:34 +0200 Subject: [PATCH 2/2] Drop redundant ConstFold [Cherry-picked 1b61ed621bc21a29f9200436a9c68bf28dedbc6d] --- compiler/src/dotty/tools/dotc/typer/Applications.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index f4be915df072..fad50a8cb07e 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -1172,8 +1172,7 @@ trait Applications extends Compatibility { case _ => tree.withType(TryDynamicCallType) } if (typedFn.tpe eq TryDynamicCallType) tryDynamicTypeApply() - else - ConstFold(assignType(cpy.TypeApply(tree)(typedFn, typedArgs), typedFn, typedArgs)) + else assignType(cpy.TypeApply(tree)(typedFn, typedArgs), typedFn, typedArgs) } }