Skip to content

Constant fold String.== #11111

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

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dotty.tools.dotc.core.NameKinds._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
import dotty.tools.dotc.typer.ConstFold

/**
* MiniPhase to transform s and raw string interpolators from using StringContext to string
Expand Down Expand Up @@ -162,6 +163,18 @@ class StringInterpolatorOpt extends MiniPhase {
}
}
else
tree
tree.tpe match
case _: ConstantType => tree
case _ =>
ConstFold.Apply(tree).tpe match
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
case _ => tree
}

override def transformSelect(tree: Select)(using Context): Tree = {
ConstFold.Select(tree).tpe match
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
case _ => tree
}

}
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ object ConstFold:
case nme.MOD => Constant(x.doubleValue % y.doubleValue)
case _ => null
}
private def foldStringOp(op: Name, x: Constant, y: Constant): Constant = op match {
case nme.ADD => Constant(x.stringValue + y.stringValue)
case nme.EQ => Constant(x.stringValue == y.stringValue)
case _ => null
}

private def foldBinop(op: Name, x: Constant, y: Constant): Constant =
val optag =
Expand All @@ -176,7 +181,7 @@ object ConstFold:
case LongTag => foldLongOp(op, x, y)
case FloatTag => foldFloatOp(op, x, y)
case DoubleTag => foldDoubleOp(op, x, y)
case StringTag if op == nme.ADD => Constant(x.stringValue + y.stringValue)
case StringTag => foldStringOp(op, x, y)
case _ => null
catch case ex: ArithmeticException => null // the code will crash at runtime,
// but that is better than the
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/i10521.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
transparent inline def default(inline name: String): Any =
inline if name == "Int" then 0
else inline if name == "String" then ""
else ???


def test =
default("Int")
5 changes: 5 additions & 0 deletions tests/pos/stringConstantFold.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test =
("A" + "B": "AB")

("A" == "A": true)
("A" == "B": false)
3 changes: 3 additions & 0 deletions tests/pos/t5856b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Test:
def test = f("a" == s"a")
inline def f(inline b: Boolean): Boolean = !b
4 changes: 2 additions & 2 deletions tests/run-macros/tasty-extractors-1.check
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ ConstantType(IntConstant(2))
Inlined(None, Nil, Try(Literal(IntConstant(3)), List(CaseDef(Ident("_"), None, Block(Nil, Literal(UnitConstant())))), Some(Literal(UnitConstant()))))
OrType(ConstantType(IntConstant(3)), TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit"))

Inlined(None, Nil, Apply(Select(Literal(StringConstant("a")), "=="), List(Literal(StringConstant("b")))))
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Boolean")
Inlined(None, Nil, Literal(BooleanConstant(false)))
ConstantType(BooleanConstant(false))

Inlined(None, Nil, Apply(Select(New(TypeIdent("Object")), "<init>"), Nil))
TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "Object")
Expand Down