Skip to content

Commit 9b0dbdf

Browse files
committed
Merge pull request scala#2175 from retronym/ticket/7185
SI-7185 Avoid NPE in TreeInfo.isExprSafeToInline
2 parents 1a84c86 + 387fbf4 commit 9b0dbdf

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/reflect/scala/reflect/internal/TreeInfo.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ abstract class TreeInfo {
9898
// However, before typing, applications of nullary functional values are also
9999
// Apply(function, Nil) trees. To prevent them from being treated as pure,
100100
// we check that the callee is a method.
101-
fn.symbol.isMethod && !fn.symbol.isLazy && isExprSafeToInline(fn)
101+
// The callee might also be a Block, which has a null symbol, so we guard against that (SI-7185)
102+
fn.symbol != null && fn.symbol.isMethod && !fn.symbol.isLazy && isExprSafeToInline(fn)
102103
case Typed(expr, _) =>
103104
isExprSafeToInline(expr)
104105
case Block(stats, expr) =>

test/files/neg/t7185.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
t7185.scala:2: error: overloaded method value apply with alternatives:
2+
(f: scala.xml.Node => Boolean)scala.xml.NodeSeq <and>
3+
(i: Int)scala.xml.Node
4+
cannot be applied to ()
5+
<e></e>()
6+
^
7+
one error found

test/files/neg/t7185.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
<e></e>()
3+
}

test/files/run/t7185.check

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Type in expressions to have them evaluated.
2+
Type :help for more information.
3+
4+
scala>
5+
6+
scala> import scala.tools.reflect.ToolBox
7+
import scala.tools.reflect.ToolBox
8+
9+
scala> import scala.reflect.runtime.universe._
10+
import scala.reflect.runtime.universe._
11+
12+
scala> object O { def apply() = 0 }
13+
defined module O
14+
15+
scala> val ORef = reify { O }.tree
16+
ORef: reflect.runtime.universe.Tree = $read.O
17+
18+
scala> val tree = Apply(Block(Nil, Block(Nil, ORef)), Nil)
19+
tree: reflect.runtime.universe.Apply =
20+
{
21+
{
22+
$read.O
23+
}
24+
}()
25+
26+
scala> {val tb = reflect.runtime.currentMirror.mkToolBox(); tb.typeCheck(tree): Any}
27+
res0: Any =
28+
{
29+
{
30+
$read.O.apply()
31+
}
32+
}
33+
34+
scala>

test/files/run/t7185.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.tools.partest.ReplTest
2+
3+
object Test extends ReplTest {
4+
override def code = """
5+
import scala.tools.reflect.ToolBox
6+
import scala.reflect.runtime.universe._
7+
object O { def apply() = 0 }
8+
val ORef = reify { O }.tree
9+
val tree = Apply(Block(Nil, Block(Nil, ORef)), Nil)
10+
{val tb = reflect.runtime.currentMirror.mkToolBox(); tb.typeCheck(tree): Any}
11+
"""
12+
}

0 commit comments

Comments
 (0)