Permalink
Browse files

better error messages for various macro definition errors

  • Loading branch information...
xeno-by committed Dec 3, 2013
1 parent 6ff3c3f commit 1d3ec4e708154ec05554f540d7d68ed55dc12426
@@ -10,6 +10,7 @@ trait Errors extends Traces {
import global._
import analyzer._
import definitions._
+ import treeInfo._
import typer.TyperErrorGen._
import typer.infer.InferErrorGen._
private val runDefinitions = currentRun.runDefinitions
@@ -18,22 +19,39 @@ trait Errors extends Traces {
// sanity check errors
- private def implRefError(message: String) = abort(macroDdef.pos, message)
+ private def implRefError(message: String) = {
+ val Applied(culprit, _, _) = macroDdef.rhs
+ abort(culprit.pos, message)
+ }
+
+ private def bundleRefError(message: String) = {
+ val Applied(core, _, _) = macroDdef.rhs
+ val culprit = core match {
+ case Select(Applied(core, _, _), _) => core
+ case _ => core
+ }
+ abort(culprit.pos, message)
+ }
def MacroImplReferenceWrongShapeError() = implRefError(
"macro implementation reference has wrong shape. required:\n"+
"macro [<static object>].<method name>[[<type args>]] or\n" +
"macro [<macro bundle>].<method name>[[<type args>]]")
+ def MacroImplWrongNumberOfTypeArgumentsError() = {
+ val diagnostic = if (macroImpl.typeParams.length > targs.length) "has too few type arguments" else "has too many arguments"
+ implRefError(s"macro implementation reference $diagnostic for " + treeSymTypeMsg(macroImplRef))
+ }
+
def MacroImplNotPublicError() = implRefError("macro implementation must be public")
def MacroImplOverloadedError() = implRefError("macro implementation cannot be overloaded")
- def MacroImplWrongNumberOfTypeArgumentsError() = implRefError(TypedApplyWrongNumberOfTpeParametersErrorMessage(macroImplRef))
+ def MacroImplNonTagImplicitParameters(params: List[Symbol]) = implRefError("macro implementations cannot have implicit parameters other than WeakTypeTag evidences")
- def MacroBundleNonStaticError() = implRefError("macro bundles must be static")
+ def MacroBundleNonStaticError() = bundleRefError("macro bundles must be static")
- def MacroBundleWrongShapeError() = implRefError("macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member")
+ def MacroBundleWrongShapeError() = bundleRefError("macro bundles must be monomorphic traits extending either BlackboxMacro or WhiteboxMacro and not implementing their `val c: BlackboxContext/WhiteboxContext` member")
// compatibility errors
@@ -91,14 +109,12 @@ trait Errors extends Traces {
private def compatibilityError(message: String) =
implRefError(
- "macro implementation has wrong shape:"+
+ "macro implementation has incompatible shape:"+
"\n required: " + showMeth(rparamss, rret, abbreviate = true, untype = false) +
"\n or : " + showMeth(rparamss, rret, abbreviate = true, untype = true) +
"\n found : " + showMeth(aparamss, aret, abbreviate = false, untype = false) +
"\n" + message)
- def MacroImplNonTagImplicitParameters(params: List[Symbol]) = compatibilityError("macro implementations cannot have implicit parameters other than WeakTypeTag evidences")
-
def MacroImplParamssMismatchError() = compatibilityError("number of parameter sections differ")
def MacroImplExtraParamsError(aparams: List[Symbol], rparams: List[Symbol]) = compatibilityError(lengthMsg("value", "found", aparams(rparams.length)))
@@ -21,9 +21,11 @@ trait Validators {
private def sanityCheck() = {
if (!macroImpl.isMethod) MacroImplReferenceWrongShapeError()
+ if (macroImpl.typeParams.length != targs.length) MacroImplWrongNumberOfTypeArgumentsError()
if (!macroImpl.isPublic) MacroImplNotPublicError()
if (macroImpl.isOverloaded) MacroImplOverloadedError()
- if (macroImpl.typeParams.length != targs.length) MacroImplWrongNumberOfTypeArgumentsError()
+ val implicitParams = aparamss.flatten filter (_.isImplicit)
+ if (implicitParams.nonEmpty) MacroImplNonTagImplicitParameters(implicitParams)
val declaredInStaticObject = isImplMethod && (macroImplOwner.isStaticOwner || macroImplOwner.moduleClass.isStaticOwner)
val declaredInTopLevelClass = isImplBundle && macroImplOwner.owner.isPackageClass
if (!declaredInStaticObject && !declaredInTopLevelClass) MacroImplReferenceWrongShapeError()
@@ -35,8 +37,6 @@ trait Validators {
// we only check strict correspondence between value parameterss
// type parameters of macro defs and macro impls don't have to coincide with each other
- val implicitParams = aparamss.flatten filter (_.isImplicit)
- if (implicitParams.nonEmpty) MacroImplNonTagImplicitParameters(implicitParams)
if (aparamss.length != rparamss.length) MacroImplParamssMismatchError()
map2(aparamss, rparamss)((aparams, rparams) => {
if (aparams.length < rparams.length) MacroImplMissingParamsError(aparams, rparams)
@@ -1,4 +1,4 @@
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
def foo = macro Bundle.impl
- ^
+ ^
one error found
@@ -1,4 +1,4 @@
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
def foo = macro Bundle.impl
- ^
+ ^
one error found
@@ -1,8 +1,8 @@
-macro-bundle-object.scala:10: error: macro implementation has wrong shape:
+macro-bundle-object.scala:10: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : : Nothing
number of parameter sections differ
def foo = macro Bundle.impl
- ^
+ ^
one error found
@@ -1,10 +1,10 @@
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
def foo = macro Bundle.impl
- ^
+ ^
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
def foo = macro Bundle[Int].impl
- ^
+ ^
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
def foo = macro Bundle[Int, Nothing].impl
- ^
+ ^
three errors found
@@ -1,4 +1,4 @@
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
def foo = macro Bundle.impl
- ^
+ ^
one error found
@@ -2,22 +2,22 @@ Macros_Test_2.scala:5: error: macro implementation reference has wrong shape. re
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo(x: Any) = macro impls.foo
- ^
+ ^
Macros_Test_2.scala:10: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo(x: Any) = macro impls.foo
- ^
+ ^
Macros_Test_2.scala:18: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo(x: Any) = macro Impls3.foo
- ^
+ ^
Macros_Test_2.scala:22: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo(x: Any) = macro Impls4.foo
- ^
+ ^
Macros_Test_2.scala:26: error: ambiguous reference to overloaded definition,
both method foo in object Impls5 of type (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Any], y: c.Expr[Any])Nothing
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
match expected type ?
def foo(x: Any, y: Any) = macro Impls5.foo
^
-Macros_Test_2.scala:31: error: macro implementation has wrong shape:
+Macros_Test_2.scala:31: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Unit]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(): c.Expr[Unit]
number of parameter sections differ
def foo1 = macro Impls6.fooEmpty
- ^
-Macros_Test_2.scala:32: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:32: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(): c.Expr[Unit]
or : (c: scala.reflect.macros.BlackboxContext)(): c.Tree
found : (c: scala.reflect.macros.BlackboxContext): c.Expr[Unit]
number of parameter sections differ
def bar1() = macro Impls6.fooNullary
- ^
+ ^
Macros_Test_2.scala:36: error: type arguments [String] do not conform to method foo's type parameter bounds [U <: Int]
def foo = macro Impls7.foo[String]
^
Macros_Test_2.scala:53: error: macro implementation must be public
def foo = macro Impls8.impl
- ^
+ ^
10 errors found
@@ -1,15 +1,15 @@
-Macros_Test_2.scala:2: error: macro implementation has wrong shape:
+Macros_Test_2.scala:2: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (c: scala.reflect.macros.BlackboxContext): Int
type mismatch for return type: Int does not conform to c.Expr[Any]
def foo1 = macro Impls.foo1
- ^
-Macros_Test_2.scala:3: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:3: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (c: scala.reflect.macros.BlackboxContext): reflect.runtime.universe.Literal
type mismatch for return type: reflect.runtime.universe.Literal does not conform to c.Expr[Any]
def foo2 = macro Impls.foo2
- ^
+ ^
two errors found
@@ -2,12 +2,12 @@ Macros_Test_2.scala:2: error: macro implementation reference has wrong shape. re
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo1(x: Any) = macro 2
- ^
+ ^
Macros_Test_2.scala:3: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo2(x: Any) = macro Impls.foo(null)(null)
- ^
+ ^
Macros_Test_2.scala:4: error: missing arguments for method foo in object Impls;
follow this method with `_' if you want to treat it as a partially applied function
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
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def foo = macro impl
- ^
+ ^
four errors found
@@ -1,8 +1,8 @@
-Impls_Macros_1.scala:8: error: macro implementation has wrong shape:
+Impls_Macros_1.scala:8: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(x: Int): Nothing
type mismatch for parameter x: c.Expr[Int] does not conform to Int
def foo(x: Int) = macro Impls.foo
- ^
+ ^
one error found
@@ -1,92 +1,84 @@
-Macros_Test_2.scala:2: error: macro implementation has wrong shape:
- required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
- or : (c: scala.reflect.macros.BlackboxContext): c.Tree
- found : (c: scala.reflect.macros.BlackboxContext)(implicit evidence$2: Numeric[U]): c.universe.Literal
-macro implementations cannot have implicit parameters other than WeakTypeTag evidences
+Macros_Test_2.scala:2: error: macro implementations cannot have implicit parameters other than WeakTypeTag evidences
def foo[U] = macro Impls1.foo[U]
- ^
-Macros_Test_2.scala:6: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:6: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : : Nothing
number of parameter sections differ
def foo = macro Impls2.foo
- ^
-Macros_Test_2.scala:10: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:10: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (c: scala.reflect.api.Universe): Nothing
type mismatch for parameter c: scala.reflect.macros.BlackboxContext does not conform to scala.reflect.api.Universe
def foo = macro Impls3.foo
- ^
-Macros_Test_2.scala:14: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:14: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (cs: scala.reflect.macros.BlackboxContext*): Nothing
types incompatible for parameter cs: corresponding is not a vararg parameter
def foo = macro Impls4.foo
- ^
-Macros_Test_2.scala:18: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:18: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Any]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext): Nothing
number of parameter sections differ
def foo(x: Any) = macro Impls5.foo
- ^
-Macros_Test_2.scala:22: error: macro implementation has wrong shape:
- required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Unit]
- or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
- found : (c: scala.reflect.macros.BlackboxContext)(implicit x: c.Expr[Int]): c.Expr[Unit]
-macro implementations cannot have implicit parameters other than WeakTypeTag evidences
+ ^
+Macros_Test_2.scala:22: error: macro implementations cannot have implicit parameters other than WeakTypeTag evidences
def foo[U](x: Int) = macro Impls6.foo[T, U]
- ^
-Macros_Test_2.scala:26: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:26: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): Nothing
parameter lists have different length, found extra parameter y: c.Expr[Int]
def foo(x: Int) = macro Impls7.foo
- ^
-Macros_Test_2.scala:30: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:30: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(x: c.universe.Symbol): Nothing
type mismatch for parameter x: c.Expr[Int] does not conform to c.universe.Symbol
def foo(x: Int) = macro Impls8.foo
- ^
-Macros_Test_2.scala:34: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:34: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree, y: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(xs: c.Expr[Int]*): Nothing
parameter lists have different length, required extra parameter y: c.Expr[Int]
def foo(x: Int, y: Int) = macro Impls9.foo
- ^
-Macros_Test_2.scala:38: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:38: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext)(x: c.Expr[Int], y: c.Expr[Int]): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext)(x: c.Tree, y: c.Tree): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(y: c.Expr[Int], x: c.Expr[Int]): Nothing
parameter names differ: x != y
def foo(x: Int, y: Int) = macro Impls10.foo
- ^
-Macros_Test_2.scala:42: error: macro implementation has wrong shape:
+ ^
+Macros_Test_2.scala:42: error: macro implementation has incompatible shape:
required: (c: scala.reflect.macros.BlackboxContext): c.Expr[Any]
or : (c: scala.reflect.macros.BlackboxContext): c.Tree
found : (c: scala.reflect.macros.BlackboxContext)(U: c.universe.Type): Nothing
number of parameter sections differ
def foo[U] = macro Impls11.foo[U]
- ^
+ ^
Macros_Test_2.scala:46: error: type arguments [U] do not conform to method foo's type parameter bounds [U <: String]
def foo[U] = macro Impls12.foo[U]
^
Macros_Test_2.scala:50: error: type arguments [U] do not conform to method foo's type parameter bounds [U <: String]
def foo[U <: Int] = macro Impls13.foo[U]
^
-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
+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
def foo = macro Impls14.foo
- ^
-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]
+ ^
+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]
def foo15[V] = macro Impls15.foo
- ^
+ ^
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]
def foo16[V] = macro Impls16.foo[V]
^
Oops, something went wrong.

0 comments on commit 1d3ec4e

Please sign in to comment.