From cb1cadb46ac3bb1fbe3402e827f9a1ed9d8d8ed6 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Sun, 26 Jul 2020 02:51:00 +0100 Subject: [PATCH] compiler: Fix/test 1D array construction from generic iterables --- artiq/compiler/transforms/inferencer.py | 4 ++++ artiq/test/lit/integration/array.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 2838a9e14..1c9ea3e6c 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -740,6 +740,10 @@ class Inferencer(algorithm.Visitor): # KLUDGE: Support multidimensional arary creation if lexically # specified as a rectangular array of lists. elt, num_dims = match_rectangular_list([arg]) + if num_dims == 0: + # Not given as a list, so just default to 1 dimension. + elt = builtins.get_iterable_elt(arg.type) + num_dims = 1 self._unify(node.type, builtins.TArray(elt, types.TValue(num_dims)), node.loc, arg.loc) diff --git a/artiq/test/lit/integration/array.py b/artiq/test/lit/integration/array.py index 1bca6304c..aa4001fb6 100644 --- a/artiq/test/lit/integration/array.py +++ b/artiq/test/lit/integration/array.py @@ -13,6 +13,14 @@ assert len(empty_array) == 0 assert empty_array.shape == (0,) assert [x * x for x in empty_array] == [] +# Creating a list from a generic iterable always generates an 1D array, as we can't +# check for rectangularity at compile time. (This could be changed to *assume* +# rectangularity and insert runtime checks instead.) +list_of_lists = [[1, 2], [3, 4]] +array_of_lists = array(list_of_lists) +assert array_of_lists.shape == (2,) +assert [x for x in array_of_lists] == list_of_lists + matrix = array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) assert len(matrix) == 2 assert matrix.shape == (2, 3)