compiler: Fix/test 1D array construction from generic iterables

This commit is contained in:
David Nadlinger 2020-07-26 02:51:00 +01:00
parent 38c17622cc
commit cb1cadb46a
2 changed files with 12 additions and 0 deletions

View File

@ -740,6 +740,10 @@ class Inferencer(algorithm.Visitor):
# KLUDGE: Support multidimensional arary creation if lexically # KLUDGE: Support multidimensional arary creation if lexically
# specified as a rectangular array of lists. # specified as a rectangular array of lists.
elt, num_dims = match_rectangular_list([arg]) 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, self._unify(node.type,
builtins.TArray(elt, types.TValue(num_dims)), builtins.TArray(elt, types.TValue(num_dims)),
node.loc, arg.loc) node.loc, arg.loc)

View File

@ -13,6 +13,14 @@ assert len(empty_array) == 0
assert empty_array.shape == (0,) assert empty_array.shape == (0,)
assert [x * x for x in empty_array] == [] 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]]) matrix = array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
assert len(matrix) == 2 assert len(matrix) == 2
assert matrix.shape == (2, 3) assert matrix.shape == (2, 3)