diff options
author | Martin Odersky <odersky@gmail.com> | 2016-09-02 13:39:48 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2016-10-02 16:11:21 +0200 |
commit | 729815bec8035f2d6132fde0eb928e877dd21a64 (patch) | |
tree | 27b8efddae77f9d3a9e28ddd1dea72186096ff66 /src/dotty/tools/dotc/typer | |
parent | 617be516b261524c2f0762de10f2bde376043ad7 (diff) | |
download | dotty-729815bec8035f2d6132fde0eb928e877dd21a64.tar.gz dotty-729815bec8035f2d6132fde0eb928e877dd21a64.tar.bz2 dotty-729815bec8035f2d6132fde0eb928e877dd21a64.zip |
Implement inline if
Inline conditionals with constant conditions
Diffstat (limited to 'src/dotty/tools/dotc/typer')
-rw-r--r-- | src/dotty/tools/dotc/typer/Inliner.scala | 13 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Typer.scala | 2 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/dotty/tools/dotc/typer/Inliner.scala b/src/dotty/tools/dotc/typer/Inliner.scala index 8d25e3250..8d9b5bf30 100644 --- a/src/dotty/tools/dotc/typer/Inliner.scala +++ b/src/dotty/tools/dotc/typer/Inliner.scala @@ -10,6 +10,7 @@ import Flags._ import Symbols._ import Types._ import Decorators._ +import Constants._ import StdNames.nme import Contexts.Context import Names.Name @@ -56,6 +57,18 @@ object Inliner { case res => res } } + override def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context) = { + val cond1 = typed(tree.cond, defn.BooleanType) + cond1.tpe.widenTermRefExpr match { + case ConstantType(Constant(condVal: Boolean)) => + val selected = typed(if (condVal) tree.thenp else tree.elsep, pt) + if (isIdempotentExpr(cond1)) selected + else Block(cond1 :: Nil, selected) + case _ => + val if1 = untpd.cpy.If(tree)(cond = untpd.TypedSplice(cond1)) + super.typedIf(if1, pt) + } + } } def inlineCall(tree: Tree, pt: Type)(implicit ctx: Context): Tree = { diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index dc41cc567..e809605f4 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -609,7 +609,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit em"local definition of ${leaks.head.name} escapes as part of expression's type ${tree.tpe}"/*; full type: ${result.tpe.toString}"*/) } - def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context) = track("typedIf") { + def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): Tree = track("typedIf") { val cond1 = typed(tree.cond, defn.BooleanType) val thenp1 = typed(tree.thenp, pt.notApplied) val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt.notApplied) |