Skip to content

Commit

Permalink
Allow also passing iterables to lists.
Browse files Browse the repository at this point in the history
whitequark committed Jun 26, 2015
1 parent e07057c commit ea0d11b
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 16 additions & 3 deletions artiq/py2llvm/typing.py
Original file line number Diff line number Diff line change
@@ -804,15 +804,28 @@ def diagnose(valid_forms):
elif builtins.is_builtin(typ, "class list"):
valid_forms = lambda: [
valid_form("list() -> list(elt='a)"),
# TODO: add this form when adding iterators
# valid_form("list(x) -> list(elt='a)")
valid_form("list(x:'a) -> list(elt='b) where 'a is iterable")
]

self._unify(node.type, builtins.TList(),
node.loc, None)

if len(node.args) == 0 and len(node.keywords) == 0:
pass # []
elif len(node.args) == 1 and len(node.keywords) == 0:
arg, = node.args

if builtins.is_iterable(arg.type):
pass
else:
note = diagnostic.Diagnostic("note",
"this expression has type {type}",
{"type": types.TypePrinter().name(arg.type)},
arg.loc)
diag = diagnostic.Diagnostic("error",
"the argument of list() must be of an iterable type", {},
node.func.loc, notes=[note])
self.engine.process(diag)
else:
diagnose(valid_forms())
elif builtins.is_builtin(typ, "function range"):
@@ -854,7 +867,7 @@ def diagnose(valid_forms):
if len(node.args) == 1 and len(node.keywords) == 0:
arg, = node.args

if builtins.is_list(arg.type) or builtins.is_range(arg.type):
if builtins.is_iterable(arg.type):
pass
else:
note = diagnostic.Diagnostic("note",
3 changes: 3 additions & 0 deletions lit-test/py2llvm/typing/error_builtin_calls.py
Original file line number Diff line number Diff line change
@@ -8,5 +8,8 @@
# CHECK-L: ${LINE:+1}: error: the argument of len() must be of an iterable type
len(1)

# CHECK-L: ${LINE:+1}: error: the argument of list() must be of an iterable type
list(1)

# CHECK-L: ${LINE:+1}: error: an argument of range() must be of a numeric type
range([])

0 comments on commit ea0d11b

Please sign in to comment.