Give an error when &kwonly or &kwargs is used in a macro definition (closes #959)
This commit is contained in:
parent
7c36fe9a14
commit
9e5f881958
@ -2371,6 +2371,9 @@ class HyASTCompiler(object):
|
||||
raise HyTypeError(name, ("received a `%s' instead of a symbol "
|
||||
"for macro name" % type(name).__name__))
|
||||
name = HyString(name).replace(name)
|
||||
for kw in ("&kwonly", "&kwargs"):
|
||||
if kw in expression[0]:
|
||||
raise HyTypeError(name, "macros cannot use %s" % kw)
|
||||
new_expression = HyExpression([
|
||||
HySymbol("with_decorator"),
|
||||
HyExpression([HySymbol("hy.macros.macro"), name]),
|
||||
|
@ -1,3 +1,5 @@
|
||||
(import [hy.errors [HyTypeError]])
|
||||
|
||||
(defmacro rev [&rest body]
|
||||
"Execute the `body` statements in reverse"
|
||||
(quasiquote (do (unquote-splice (list (reversed body))))))
|
||||
@ -48,6 +50,20 @@
|
||||
(defmacro bar [x y]
|
||||
(foo x y))
|
||||
|
||||
(defn test-macro-kw []
|
||||
"NATIVE: test that an error is raised when &kwonly or &kwargs is used in a macro"
|
||||
(try
|
||||
(eval '(defmacro f [&kwonly a b]))
|
||||
(except [e HyTypeError]
|
||||
(assert (= e.message "macros cannot use &kwonly")))
|
||||
(else (assert false)))
|
||||
|
||||
(try
|
||||
(eval '(defmacro f [&kwargs kw]))
|
||||
(except [e HyTypeError]
|
||||
(assert (= e.message "macros cannot use &kwargs")))
|
||||
(else (assert false))))
|
||||
|
||||
(defn test-fn-calling-macro []
|
||||
"NATIVE: test macro calling a plain function"
|
||||
(assert (= 3 (bar 1 2))))
|
||||
|
Loading…
Reference in New Issue
Block a user