Skip to content

Commit 0e8331f

Browse files
authored
Variadic arguments should be coerced to nil when none are provided (#802)
Fixes #801
1 parent bb0e011 commit 0e8331f

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
* Fix issue with keywords throwing `TypeError` when used as a function on vectors (#770)
2929
* Fix an issue where the constructors of types created by `deftype` and `defrecord` could not be called if they contained `-` characters (#777)
3030
* Fix issue with the variadic ampersand operator treated as a binding in macros (#772)
31+
* Fix a bug the variadic arg symbol was not correctly bound to `nil` when no variadic arguments were provided (#801)
3132

3233
### Removed
3334
* Removed support for PyPy 3.8 (#785)

src/basilisp/core.lpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
(def ^{:doc "Create a list from the arguments."
2727
:arglists '([& args])}
2828
list
29-
(fn* list [& args] args))
29+
(fn* list [& args] (if args args '())))
3030

3131
(def
3232
^{:doc "If ``seq`` is a Seq, return the first element from ``seq``. If ``seq``

src/basilisp/lang/compiler/generator.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ def _var_ns_as_python_sym(name: str) -> str:
694694
_ATTR_CLASS_DECORATOR_NAME = _load_attr("attr.define")
695695
_ATTR_FROZEN_DECORATOR_NAME = _load_attr("attr.frozen")
696696
_ATTRIB_FIELD_FN_NAME = _load_attr("attr.field")
697-
_COLLECT_ARGS_FN_NAME = _load_attr(f"{_RUNTIME_ALIAS}._collect_args")
698697
_COERCE_SEQ_FN_NAME = _load_attr(f"{_RUNTIME_ALIAS}.to_seq")
699698
_BASILISP_FN_FN_NAME = _load_attr(f"{_RUNTIME_ALIAS}._basilisp_fn")
700699
_FN_WITH_ATTRS_FN_NAME = _load_attr(f"{_RUNTIME_ALIAS}._with_attrs")
@@ -1627,10 +1626,14 @@ def __fn_args_to_py_ast(
16271626
fn_body_ast.append(
16281627
ast.Assign(
16291628
targets=[ast.Name(id=safe_local, ctx=ast.Store())],
1630-
value=ast.Call(
1631-
func=_COLLECT_ARGS_FN_NAME,
1632-
args=[ast.Name(id=arg_name, ctx=ast.Load())],
1633-
keywords=[],
1629+
value=ast.IfExp(
1630+
test=ast.Name(id=arg_name, ctx=ast.Load()),
1631+
body=ast.Call(
1632+
func=_NEW_LIST_FN_NAME,
1633+
args=[ast.Name(id=arg_name, ctx=ast.Load())],
1634+
keywords=[],
1635+
),
1636+
orelse=ast.Constant(None),
16341637
),
16351638
)
16361639
)

src/basilisp/lang/runtime.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,17 +1638,6 @@ def repl_completions(text: str) -> Iterable[str]:
16381638
####################
16391639

16401640

1641-
@functools.singledispatch
1642-
def _collect_args(args) -> ISeq:
1643-
"""Collect Python starred arguments into a Basilisp list."""
1644-
raise TypeError("Python variadic arguments should always be a tuple")
1645-
1646-
1647-
@_collect_args.register(tuple)
1648-
def _collect_args_tuple(args: tuple) -> ISeq:
1649-
return llist.list(args)
1650-
1651-
16521641
class _TrampolineArgs:
16531642
__slots__ = ("_has_varargs", "_args", "_kwargs")
16541643

tests/basilisp/compiler_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,12 @@ def test_variadic_arity_fn_method_has_variadic_argument(
23132313
with pytest.raises(compiler.CompilerException):
23142314
lcompile("(fn* ([] :a) ([m &] m))")
23152315

2316+
def test_variadic_arity_fn_collects_args_correctly(self, lcompile: CompileFn):
2317+
f = lcompile("(fn* [& args] args)")
2318+
assert f() is None
2319+
assert f(1) == llist.l(1)
2320+
assert f(1, 2, 3) == llist.l(1, 2, 3)
2321+
23162322
def test_fn_argument_vector_is_vector(self, lcompile: CompileFn):
23172323
with pytest.raises(compiler.CompilerException):
23182324
lcompile("(fn* () :a)")

0 commit comments

Comments
 (0)