Skip to content

Commit ee646e9

Browse files
committed
fixes a crash on a degenerate macro definition
Previous version of the MacroImplReference extractor didn't take into the account the fact that RefTree.qualifier.symbol can be null (and it can be null if RefTree is an Ident, because then qualifier is an EmptyTree). This led to NPEs for really weird macro defs that refer to local methods as their corresponding macro impls. Now I check for this corner case, and the stuff now longer crashes. This was wrong; this is how I fixed it; the world is now a better place.
1 parent 488444b commit ee646e9

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,12 @@ abstract class TreeInfo {
837837

838838
def unapply(tree: Tree) = refPart(tree) match {
839839
case ref: RefTree => {
840-
val isBundle = definitions.isMacroBundleType(ref.qualifier.tpe)
840+
val qual = ref.qualifier
841+
val isBundle = definitions.isMacroBundleType(qual.tpe)
841842
val owner =
842-
if (isBundle) ref.qualifier.tpe.typeSymbol
843+
if (isBundle) qual.tpe.typeSymbol
843844
else {
844-
val sym = ref.qualifier.symbol
845+
val sym = if (qual.hasSymbolField) qual.symbol else NoSymbol
845846
if (sym.isModule) sym.moduleClass else sym
846847
}
847848
Some((isBundle, owner, ref.symbol, dissectApplied(tree).targs))

test/files/neg/macro-invalidshape.check

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ Macros_Test_2.scala:4: error: missing arguments for method foo in object Impls;
1212
follow this method with `_' if you want to treat it as a partially applied function
1313
def foo3(x: Any) = macro {2; Impls.foo}
1414
^
15-
three errors found
15+
Macros_Test_2.scala:7: error: macro implementation reference has wrong shape. required:
16+
macro [<static object>].<method name>[[<type args>]] or
17+
macro [<macro bundle>].<method name>[[<type args>]]
18+
def foo = macro impl
19+
^
20+
four errors found

test/files/neg/macro-invalidshape/Macros_Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ object Macros {
22
def foo1(x: Any) = macro 2
33
def foo2(x: Any) = macro Impls.foo(null)(null)
44
def foo3(x: Any) = macro {2; Impls.foo}
5+
{
6+
def impl(c: scala.reflect.macros.Context) = c.literalUnit
7+
def foo = macro impl
8+
foo
9+
}
510
}
611

712
object Test extends App {

0 commit comments

Comments
 (0)