diff options
author | Som Snytt <som.snytt@gmail.com> | 2017-02-15 10:33:28 -0800 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2017-02-15 15:17:01 -0800 |
commit | 6fb382501f7a63f3227a18396a2af0c862e6ab1c (patch) | |
tree | 51ca8422a68a13537f1ce9b2b7a182de6f266fec /src | |
parent | 05d5338ff4ee3ffe5ef1c0e33faa1a29480be2b0 (diff) | |
download | scala-6fb382501f7a63f3227a18396a2af0c862e6ab1c.tar.gz scala-6fb382501f7a63f3227a18396a2af0c862e6ab1c.tar.bz2 scala-6fb382501f7a63f3227a18396a2af0c862e6ab1c.zip |
SI-10190 Elide string to empty instead of null
Avoid NPE when eliding string-valued functions.
For example, `log(s"$cheap$expensive")` needn't print null.
This is a natural and inexpensive way to elide strings.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/UnCurry.scala | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index 096b6b9263..c3006a4786 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -342,12 +342,16 @@ abstract class UnCurry extends InfoTransform * the whole tree with it. */ private def replaceElidableTree(tree: Tree): Tree = { + def elisionOf(t: Type): Tree = t.typeSymbol match { + case StringClass => Literal(Constant("")) setType t + case _ => gen.mkZero(t) + } tree match { case DefDef(_,_,_,_,_,rhs) => - val rhs1 = if (rhs == EmptyTree) rhs else Block(Nil, gen.mkZero(rhs.tpe)) setType rhs.tpe + val rhs1 = if (rhs == EmptyTree) rhs else Block(Nil, elisionOf(rhs.tpe)) setType rhs.tpe deriveDefDef(tree)(_ => rhs1) setSymbol tree.symbol setType tree.tpe case _ => - gen.mkZero(tree.tpe) setType tree.tpe + elisionOf(tree.tpe) } } |