From ea0d11b8be9b31417429a6fbfe6d81674f2d2545 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 26 Jun 2015 19:14:24 +0300 Subject: [PATCH] Allow also passing iterables to lists. --- artiq/py2llvm/typing.py | 19 ++++++++++++++++--- .../py2llvm/typing/error_builtin_calls.py | 3 +++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/artiq/py2llvm/typing.py b/artiq/py2llvm/typing.py index 27ba7a1ac..775b0d1f2 100644 --- a/artiq/py2llvm/typing.py +++ b/artiq/py2llvm/typing.py @@ -804,8 +804,7 @@ class Inferencer(algorithm.Visitor): 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(), @@ -813,6 +812,20 @@ class Inferencer(algorithm.Visitor): 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 @@ class Inferencer(algorithm.Visitor): 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", diff --git a/lit-test/py2llvm/typing/error_builtin_calls.py b/lit-test/py2llvm/typing/error_builtin_calls.py index 86cab873d..caf77b1a8 100644 --- a/lit-test/py2llvm/typing/error_builtin_calls.py +++ b/lit-test/py2llvm/typing/error_builtin_calls.py @@ -8,5 +8,8 @@ int(1.0, width=a) # 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([])