diff options
author | Martin Odersky <odersky@gmail.com> | 2014-01-15 10:56:41 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2014-01-15 10:57:16 +0100 |
commit | 3d9a664e75307410b8845ecc1540a00924867912 (patch) | |
tree | d9ae348b660156897d97a0162d83aa424236ec6f /src/dotty/tools | |
parent | a709a72cd2ff64e5fb81a388d95f85c62ede7db3 (diff) | |
download | dotty-3d9a664e75307410b8845ecc1540a00924867912.tar.gz dotty-3d9a664e75307410b8845ecc1540a00924867912.tar.bz2 dotty-3d9a664e75307410b8845ecc1540a00924867912.zip |
Fix for constant folding
Diffstat (limited to 'src/dotty/tools')
-rw-r--r-- | src/dotty/tools/dotc/parsing/Scanners.scala | 18 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Applications.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/ConstFold.scala | 39 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Typer.scala | 2 |
4 files changed, 30 insertions, 31 deletions
diff --git a/src/dotty/tools/dotc/parsing/Scanners.scala b/src/dotty/tools/dotc/parsing/Scanners.scala index 42758c711..c4ee199c6 100644 --- a/src/dotty/tools/dotc/parsing/Scanners.scala +++ b/src/dotty/tools/dotc/parsing/Scanners.scala @@ -158,7 +158,7 @@ object Scanners { /** Produce next token, filling TokenData fields of Scanner. */ - def nextToken() { + def nextToken(): Unit = { val lastToken = token adjustSepRegions(lastToken) @@ -244,7 +244,7 @@ object Scanners { /** read next token, filling TokenData fields of Scanner. */ - protected final def fetchToken() { + protected final def fetchToken(): Unit = { offset = charOffset - 1 (ch: @switch) match { case ' ' | '\t' | CR | LF | FF => @@ -471,7 +471,7 @@ object Scanners { // Identifiers --------------------------------------------------------------- - private def getBackquotedIdent() { + private def getBackquotedIdent(): Unit = { nextChar() getLitChars('`') if (ch == '`') { @@ -533,7 +533,7 @@ object Scanners { else finishNamed() } - private def getIdentOrOperatorRest() { + private def getIdentOrOperatorRest(): Unit = { if (isIdentifierPart(ch)) getIdentRest() else ch match { @@ -709,7 +709,7 @@ object Scanners { /** read fractional part and exponent of floating point number * if one is present. */ - protected def getFraction() { + protected def getFraction(): Unit = { token = DOUBLELIT while ('0' <= ch && ch <= '9') { putChar(ch) @@ -746,14 +746,14 @@ object Scanners { } checkNoLetter() } - def checkNoLetter() { + def checkNoLetter(): Unit = { if (isIdentifierPart(ch) && ch >= ' ') error("Invalid literal number") } /** Read a number into strVal and set base */ - protected def getNumber() { + protected def getNumber(): Unit = { while (digit2int(ch, base) >= 0) { putChar(ch) nextChar() @@ -805,7 +805,7 @@ object Scanners { /** Parse character literal if current character is followed by \', * or follow with given op and return a symbol literal token */ - def charLitOr(op: () => Unit) { + def charLitOr(op: () => Unit): Unit = { putChar(ch) nextChar() if (ch == '\'') { @@ -940,7 +940,7 @@ object Scanners { } /** signal an error where the input ended in the middle of a token */ - def incompleteInputError(msg: String) { + def incompleteInputError(msg: String): Unit = { ctx.incompleteInputError(msg, source atPos Position(offset)) token = EOF errOffset = offset diff --git a/src/dotty/tools/dotc/typer/Applications.scala b/src/dotty/tools/dotc/typer/Applications.scala index 3a8605570..644227878 100644 --- a/src/dotty/tools/dotc/typer/Applications.scala +++ b/src/dotty/tools/dotc/typer/Applications.scala @@ -442,7 +442,7 @@ trait Applications extends Compatibility { self: Typer => if (proto.argsAreTyped) new ApplyToTyped(tree, fun1, funRef, proto.typedArgs, pt) else new ApplyToUntyped(tree, fun1, funRef, proto, pt) val result = app.result - ConstFold(result) orElse result + ConstFold(result) } { (failedVal, failedState) => fun1 match { case Select(qual, name) => // try with prototype `[].name(args)`, this might succeed by inserting an diff --git a/src/dotty/tools/dotc/typer/ConstFold.scala b/src/dotty/tools/dotc/typer/ConstFold.scala index a66e8a9c8..7930b5d4a 100644 --- a/src/dotty/tools/dotc/typer/ConstFold.scala +++ b/src/dotty/tools/dotc/typer/ConstFold.scala @@ -17,26 +17,25 @@ object ConstFold { import tpd._ /** If tree is a constant operation, replace with result. */ - def apply(tree: Tree)(implicit ctx: Context): Tree = - finish(tree) { - tree match { - case Apply(Select(xt, op), yt :: Nil) => - xt.tpe match { - case ConstantType(x) => - yt.tpe match { - case ConstantType(y) => foldBinop(op, x, y) - case _ => null - } - case _ => null - } - case Select(xt, op) => - xt.tpe match { - case ConstantType(x) => foldUnop(op, x) - case _ => null - } - case _ => null - } + def apply(tree: Tree)(implicit ctx: Context): Tree = finish(tree) { + tree match { + case Apply(Select(xt, op), yt :: Nil) => + xt.tpe match { + case ConstantType(x) => + yt.tpe match { + case ConstantType(y) => foldBinop(op, x, y) + case _ => null + } + case _ => null + } + case Select(xt, op) => + xt.tpe match { + case ConstantType(x) => foldUnop(op, x) + case _ => null + } + case _ => null } + } /** If tree is a constant value that can be converted to type `pt`, perform * the conversion. @@ -53,7 +52,7 @@ object ConstFold { try { val x = compX if (x ne null) tree withType ConstantType(x) - else EmptyTree + else tree } catch { case _: ArithmeticException => tree // the code will crash at runtime, // but that is better than the diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index a40062127..61ab0ed56 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1205,7 +1205,7 @@ class Typer extends Namer with Applications with Implicits { def adaptToSubType(wtp: Type): Tree = { // try converting a constant to the target type val folded = ConstFold(tree, pt) - if (folded ne EmptyTree) return folded + if (folded ne tree) return folded // drop type if prototype is Unit if (pt isRef defn.UnitClass) return tpd.Block(tree :: Nil, Literal(Constant(()))) |