Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor constant folding of applications #20099

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,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 Expand Up @@ -1189,7 +1190,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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But do we need this one then ? I'm confused about why we need both of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We don't need the one here.

}
}

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 @@ -4200,7 +4200,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

}
Loading