Skip to content

Commit 1d3ec4e

Browse files
committed
better error messages for various macro definition errors
1 parent 6ff3c3f commit 1d3ec4e

15 files changed

+84
-76
lines changed

src/compiler/scala/reflect/macros/compiler/Errors.scala

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ trait Errors extends Traces {
1010
import global._
1111
import analyzer._
1212
import definitions._
13+
import treeInfo._
1314
import typer.TyperErrorGen._
1415
import typer.infer.InferErrorGen._
1516
private val runDefinitions = currentRun.runDefinitions
@@ -18,22 +19,39 @@ trait Errors extends Traces {
1819

1920
// sanity check errors
2021

21-
private def implRefError(message: String) = abort(macroDdef.pos, message)
22+
private def implRefError(message: String) = {
23+
val Applied(culprit, _, _) = macroDdef.rhs
24+
abort(culprit.pos, message)
25+
}
26+
27+
private def bundleRefError(message: String) = {
28+
val Applied(core, _, _) = macroDdef.rhs
29+
val culprit = core match {
30+
case Select(Applied(core, _, _), _) => core
31+
case _ => core
32+
}
33+
abort(culprit.pos, message)
34+
}
2235

2336
def MacroImplReferenceWrongShapeError() = implRefError(
2437
"macro implementation reference has wrong shape. required:\n"+
2538
"macro [<static object>].<method name>[[<type args>]] or\n" +
2639
"macro [<macro bundle>].<method name>[[<type args>]]")
2740

41+
def MacroImplWrongNumberOfTypeArgumentsError() = {
42+
val diagnostic = if (macroImpl.typeParams.length > targs.length) "has too few type arguments" else "has too many arguments"
43+
implRefError(s"macro implementation reference $diagnostic for " + treeSymTypeMsg(macroImplRef))
44+
}
45+
2846
def MacroImplNotPublicError() = implRefError("macro implementation must be public")
2947

3048
def MacroImplOverloadedError() = implRefError("macro implementation cannot be overloaded")
3149

32-
def MacroImplWrongNumberOfTypeArgumentsError() = implRefError(TypedApplyWrongNumberOfTpeParametersErrorMessage(macroImplRef))
50+
def MacroImplNonTagImplicitParameters(params: List[Symbol]) = implRefError("macro implementations cannot have implicit parameters other than WeakTypeTag evidences")
3351

34-
def MacroBundleNonStaticError() = implRefError("macro bundles must be static")
52+
def MacroBundleNonStaticError() = bundleRefError("macro bundles must be static")
3553

36-
def MacroBundleWrongShapeError() = implRefError("macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member")
54+
def MacroBundleWrongShapeError() = bundleRefError("macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member")
3755

3856
// compatibility errors
3957

@@ -91,14 +109,12 @@ trait Errors extends Traces {
91109

92110
private def compatibilityError(message: String) =
93111
implRefError(
94-
"macro implementation has wrong shape:"+
112+
"macro implementation has incompatible shape:"+
95113
"\n required: " + showMeth(rparamss, rret, abbreviate = true, untype = false) +
96114
"\n or : " + showMeth(rparamss, rret, abbreviate = true, untype = true) +
97115
"\n found : " + showMeth(aparamss, aret, abbreviate = false, untype = false) +
98116
"\n" + message)
99117

100-
def MacroImplNonTagImplicitParameters(params: List[Symbol]) = compatibilityError("macro implementations cannot have implicit parameters other than WeakTypeTag evidences")
101-
102118
def MacroImplParamssMismatchError() = compatibilityError("number of parameter sections differ")
103119

104120
def MacroImplExtraParamsError(aparams: List[Symbol], rparams: List[Symbol]) = compatibilityError(lengthMsg("value", "found", aparams(rparams.length)))

src/compiler/scala/reflect/macros/compiler/Validators.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ trait Validators {
2121

2222
private def sanityCheck() = {
2323
if (!macroImpl.isMethod) MacroImplReferenceWrongShapeError()
24+
if (macroImpl.typeParams.length != targs.length) MacroImplWrongNumberOfTypeArgumentsError()
2425
if (!macroImpl.isPublic) MacroImplNotPublicError()
2526
if (macroImpl.isOverloaded) MacroImplOverloadedError()
26-
if (macroImpl.typeParams.length != targs.length) MacroImplWrongNumberOfTypeArgumentsError()
27+
val implicitParams = aparamss.flatten filter (_.isImplicit)
28+
if (implicitParams.nonEmpty) MacroImplNonTagImplicitParameters(implicitParams)
2729
val declaredInStaticObject = isImplMethod && (macroImplOwner.isStaticOwner || macroImplOwner.moduleClass.isStaticOwner)
2830
val declaredInTopLevelClass = isImplBundle && macroImplOwner.owner.isPackageClass
2931
if (!declaredInStaticObject && !declaredInTopLevelClass) MacroImplReferenceWrongShapeError()
@@ -35,8 +37,6 @@ trait Validators {
3537

3638
// we only check strict correspondence between value parameterss
3739
// type parameters of macro defs and macro impls don't have to coincide with each other
38-
val implicitParams = aparamss.flatten filter (_.isImplicit)
39-
if (implicitParams.nonEmpty) MacroImplNonTagImplicitParameters(implicitParams)
4040
if (aparamss.length != rparamss.length) MacroImplParamssMismatchError()
4141
map2(aparamss, rparamss)((aparams, rparams) => {
4242
if (aparams.length < rparams.length) MacroImplMissingParamsError(aparams, rparams)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
macro-bundle-class.scala:10: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
22
def foo = macro Bundle.impl
3-
^
3+
^
44
one error found
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
macro-bundle-mixbox.scala:9: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
22
def foo = macro Bundle.impl
3-
^
3+
^
44
one error found
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
macro-bundle-object.scala:10: error: macro implementation has wrong shape:
1+
macro-bundle-object.scala:10: error: macro implementation has incompatible shape:
22
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
33
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
44
found : : Nothing
55
number of parameter sections differ
66
def foo = macro Bundle.impl
7-
^
7+
^
88
one error found
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
macro-bundle-polymorphic.scala:9: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
22
def foo = macro Bundle.impl
3-
^
3+
^
44
macro-bundle-polymorphic.scala:10: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
55
def foo = macro Bundle[Int].impl
6-
^
6+
^
77
macro-bundle-polymorphic.scala:11: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
88
def foo = macro Bundle[Int, Nothing].impl
9-
^
9+
^
1010
three errors found
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
macro-bundle-trait.scala:11: error: macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member
22
def foo = macro Bundle.impl
3-
^
3+
^
44
one error found

test/files/neg/macro-invalidimpl.check

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ Macros_Test_2.scala:5: error: macro implementation reference has wrong shape. re
22
macro [<static object>].<method name>[[<type args>]] or
33
macro [<macro bundle>].<method name>[[<type args>]]
44
def foo(x: Any) = macro impls.foo
5-
^
5+
^
66
Macros_Test_2.scala:10: error: macro implementation reference has wrong shape. required:
77
macro [<static object>].<method name>[[<type args>]] or
88
macro [<macro bundle>].<method name>[[<type args>]]
99
def foo(x: Any) = macro impls.foo
10-
^
10+
^
1111
Macros_Test_2.scala:18: error: macro implementation reference has wrong shape. required:
1212
macro [<static object>].<method name>[[<type args>]] or
1313
macro [<macro bundle>].<method name>[[<type args>]]
1414
def foo(x: Any) = macro Impls3.foo
15-
^
15+
^
1616
Macros_Test_2.scala:22: error: macro implementation reference has wrong shape. required:
1717
macro [<static object>].<method name>[[<type args>]] or
1818
macro [<macro bundle>].<method name>[[<type args>]]
1919
def foo(x: Any) = macro Impls4.foo
20-
^
20+
^
2121
Macros_Test_2.scala:26: error: ambiguous reference to overloaded definition,
2222
both method foo in object Impls5 of type (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Any], y: c.Expr[Any])Nothing
2323
and method foo in object Impls5 of type (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Any])Nothing
@@ -30,24 +30,24 @@ and method foo in object Impls5 of type (c: scala.reflect.macros.BlackboxContex
3030
match expected type ?
3131
def foo(x: Any, y: Any) = macro Impls5.foo
3232
^
33-
Macros_Test_2.scala:31: error: macro implementation has wrong shape:
33+
Macros_Test_2.scala:31: error: macro implementation has incompatible shape:
3434
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Unit]
3535
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
3636
found : (c: scala.reflect.macros.BlackboxContext)(): c.Expr[Unit]
3737
number of parameter sections differ
3838
def foo1 = macro Impls6.fooEmpty
39-
^
40-
Macros_Test_2.scala:32: error: macro implementation has wrong shape:
39+
^
40+
Macros_Test_2.scala:32: error: macro implementation has incompatible shape:
4141
required: (c: scala.reflect.macros.BlackboxContext)(): c.Expr[Unit]
4242
or : (c: scala.reflect.macros.BlackboxContext)(): c.Tree
4343
found : (c: scala.reflect.macros.BlackboxContext): c.Expr[Unit]
4444
number of parameter sections differ
4545
def bar1() = macro Impls6.fooNullary
46-
^
46+
^
4747
Macros_Test_2.scala:36: error: type arguments [String] do not conform to method foo's type parameter bounds [U <: Int]
4848
def foo = macro Impls7.foo[String]
4949
^
5050
Macros_Test_2.scala:53: error: macro implementation must be public
5151
def foo = macro Impls8.impl
52-
^
52+
^
5353
10 errors found

test/files/neg/macro-invalidret.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
Macros_Test_2.scala:2: error: macro implementation has wrong shape:
1+
Macros_Test_2.scala:2: error: macro implementation has incompatible shape:
22
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
33
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
44
found : (c: scala.reflect.macros.BlackboxContext): Int
55
type mismatch for return type: Int does not conform to c.Expr[Any]
66
def foo1 = macro Impls.foo1
7-
^
8-
Macros_Test_2.scala:3: error: macro implementation has wrong shape:
7+
^
8+
Macros_Test_2.scala:3: error: macro implementation has incompatible shape:
99
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
1010
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
1111
found : (c: scala.reflect.macros.BlackboxContext): reflect.runtime.universe.Literal
1212
type mismatch for return type: reflect.runtime.universe.Literal does not conform to c.Expr[Any]
1313
def foo2 = macro Impls.foo2
14-
^
14+
^
1515
two errors found

test/files/neg/macro-invalidshape.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ Macros_Test_2.scala:2: error: macro implementation reference has wrong shape. re
22
macro [<static object>].<method name>[[<type args>]] or
33
macro [<macro bundle>].<method name>[[<type args>]]
44
def foo1(x: Any) = macro 2
5-
^
5+
^
66
Macros_Test_2.scala:3: error: macro implementation reference has wrong shape. required:
77
macro [<static object>].<method name>[[<type args>]] or
88
macro [<macro bundle>].<method name>[[<type args>]]
99
def foo2(x: Any) = macro Impls.foo(null)(null)
10-
^
10+
^
1111
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}
@@ -16,5 +16,5 @@ Macros_Test_2.scala:7: error: macro implementation reference has wrong shape. re
1616
macro [<static object>].<method name>[[<type args>]] or
1717
macro [<macro bundle>].<method name>[[<type args>]]
1818
def foo = macro impl
19-
^
19+
^
2020
four errors found
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Impls_Macros_1.scala:8: error: macro implementation has wrong shape:
1+
Impls_Macros_1.scala:8: error: macro implementation has incompatible shape:
22
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
33
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
44
found : (c: scala.reflect.macros.BlackboxContext)(x: Int): Nothing
55
type mismatch for parameter x: c.Expr[Int] does not conform to Int
66
def foo(x: Int) = macro Impls.foo
7-
^
7+
^
88
one error found

test/files/neg/macro-invalidsig.check

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,84 @@
1-
Macros_Test_2.scala:2: error: macro implementation has wrong shape:
2-
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
3-
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
4-
found : (c: scala.reflect.macros.BlackboxContext)(implicit evidence$2: Numeric[U]): c.universe.Literal
5-
macro implementations cannot have implicit parameters other than WeakTypeTag evidences
1+
Macros_Test_2.scala:2: error: macro implementations cannot have implicit parameters other than WeakTypeTag evidences
62
def foo[U] = macro Impls1.foo[U]
7-
^
8-
Macros_Test_2.scala:6: error: macro implementation has wrong shape:
3+
^
4+
Macros_Test_2.scala:6: error: macro implementation has incompatible shape:
95
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
106
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
117
found : : Nothing
128
number of parameter sections differ
139
def foo = macro Impls2.foo
14-
^
15-
Macros_Test_2.scala:10: error: macro implementation has wrong shape:
10+
^
11+
Macros_Test_2.scala:10: error: macro implementation has incompatible shape:
1612
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
1713
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
1814
found : (c: scala.reflect.api.Universe): Nothing
1915
type mismatch for parameter c: scala.reflect.macros.BlackboxContext does not conform to scala.reflect.api.Universe
2016
def foo = macro Impls3.foo
21-
^
22-
Macros_Test_2.scala:14: error: macro implementation has wrong shape:
17+
^
18+
Macros_Test_2.scala:14: error: macro implementation has incompatible shape:
2319
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
2420
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
2521
found : (cs: scala.reflect.macros.BlackboxContext*): Nothing
2622
types incompatible for parameter cs: corresponding is not a vararg parameter
2723
def foo = macro Impls4.foo
28-
^
29-
Macros_Test_2.scala:18: error: macro implementation has wrong shape:
24+
^
25+
Macros_Test_2.scala:18: error: macro implementation has incompatible shape:
3026
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Any]): c.Expr[Any]
3127
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
3228
found : (c: scala.reflect.macros.BlackboxContext): Nothing
3329
number of parameter sections differ
3430
def foo(x: Any) = macro Impls5.foo
35-
^
36-
Macros_Test_2.scala:22: error: macro implementation has wrong shape:
37-
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Unit]
38-
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
39-
found : (c: scala.reflect.macros.BlackboxContext)(implicit x: c.Expr[Int]): c.Expr[Unit]
40-
macro implementations cannot have implicit parameters other than WeakTypeTag evidences
31+
^
32+
Macros_Test_2.scala:22: error: macro implementations cannot have implicit parameters other than WeakTypeTag evidences
4133
def foo[U](x: Int) = macro Impls6.foo[T, U]
42-
^
43-
Macros_Test_2.scala:26: error: macro implementation has wrong shape:
34+
^
35+
Macros_Test_2.scala:26: error: macro implementation has incompatible shape:
4436
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
4537
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
4638
found : (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): Nothing
4739
parameter lists have different length, found extra parameter y: c.Expr[Int]
4840
def foo(x: Int) = macro Impls7.foo
49-
^
50-
Macros_Test_2.scala:30: error: macro implementation has wrong shape:
41+
^
42+
Macros_Test_2.scala:30: error: macro implementation has incompatible shape:
5143
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
5244
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
5345
found : (c: scala.reflect.macros.BlackboxContext)(x: c.universe.Symbol): Nothing
5446
type mismatch for parameter x: c.Expr[Int] does not conform to c.universe.Symbol
5547
def foo(x: Int) = macro Impls8.foo
56-
^
57-
Macros_Test_2.scala:34: error: macro implementation has wrong shape:
48+
^
49+
Macros_Test_2.scala:34: error: macro implementation has incompatible shape:
5850
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): c.Expr[Any]
5951
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree, y: c.Tree): c.Tree
6052
found : (c: scala.reflect.macros.BlackboxContext)(xs: c.Expr[Int]*): Nothing
6153
parameter lists have different length, required extra parameter y: c.Expr[Int]
6254
def foo(x: Int, y: Int) = macro Impls9.foo
63-
^
64-
Macros_Test_2.scala:38: error: macro implementation has wrong shape:
55+
^
56+
Macros_Test_2.scala:38: error: macro implementation has incompatible shape:
6557
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): c.Expr[Any]
6658
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree, y: c.Tree): c.Tree
6759
found : (c: scala.reflect.macros.BlackboxContext)(y: c.Expr[Int], x: c.Expr[Int]): Nothing
6860
parameter names differ: x != y
6961
def foo(x: Int, y: Int) = macro Impls10.foo
70-
^
71-
Macros_Test_2.scala:42: error: macro implementation has wrong shape:
62+
^
63+
Macros_Test_2.scala:42: error: macro implementation has incompatible shape:
7264
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
7365
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
7466
found : (c: scala.reflect.macros.BlackboxContext)(U: c.universe.Type): Nothing
7567
number of parameter sections differ
7668
def foo[U] = macro Impls11.foo[U]
77-
^
69+
^
7870
Macros_Test_2.scala:46: error: type arguments [U] do not conform to method foo's type parameter bounds [U <: String]
7971
def foo[U] = macro Impls12.foo[U]
8072
^
8173
Macros_Test_2.scala:50: error: type arguments [U] do not conform to method foo's type parameter bounds [U <: String]
8274
def foo[U <: Int] = macro Impls13.foo[U]
8375
^
84-
Macros_Test_2.scala:54: error: wrong number of type parameters for method foo: [U](c: scala.reflect.macros.BlackboxContext)(implicit evidence$4: c.WeakTypeTag[U])Nothing
76+
Macros_Test_2.scala:54: error: macro implementation reference has too few type arguments for method foo: [U](c: scala.reflect.macros.BlackboxContext)(implicit evidence$4: c.WeakTypeTag[U])Nothing
8577
def foo = macro Impls14.foo
86-
^
87-
Macros_Test_2.scala:59: error: wrong number of type parameters for method foo: [T, U, V](c: scala.reflect.macros.BlackboxContext)(implicit evidence$5: c.WeakTypeTag[T], implicit evidence$6: c.WeakTypeTag[U], implicit V: c.WeakTypeTag[V])c.Expr[Unit]
78+
^
79+
Macros_Test_2.scala:59: error: macro implementation reference has too few type arguments for method foo: [T, U, V](c: scala.reflect.macros.BlackboxContext)(implicit evidence$5: c.WeakTypeTag[T], implicit evidence$6: c.WeakTypeTag[U], implicit V: c.WeakTypeTag[V])c.Expr[Unit]
8880
def foo15[V] = macro Impls15.foo
89-
^
81+
^
9082
Macros_Test_2.scala:60: error: wrong number of type parameters for method foo: [T, U, V](c: scala.reflect.macros.BlackboxContext)(implicit evidence$7: c.WeakTypeTag[T], implicit evidence$8: c.WeakTypeTag[U], implicit V: c.WeakTypeTag[V])c.Expr[Unit]
9183
def foo16[V] = macro Impls16.foo[V]
9284
^

0 commit comments

Comments
 (0)