From 5b4fde30a8d5a979d0c21ae8fd833716d1440190 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 9 Jun 2017 06:50:48 +0000 Subject: [PATCH] compiler: unbreak subscripts for bytes values (#714). --- artiq/compiler/builtins.py | 10 ++++------ artiq/compiler/transforms/inferencer.py | 5 ++++- artiq/test/lit/integration/subscript.py | 4 ++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/artiq/compiler/builtins.py b/artiq/compiler/builtins.py index 9965f5284..a64129dcc 100644 --- a/artiq/compiler/builtins.py +++ b/artiq/compiler/builtins.py @@ -285,15 +285,13 @@ def is_exception(typ, name=None): typ.name == name def is_iterable(typ): - typ = typ.find() - return isinstance(typ, types.TMono) and \ - typ.name in ('list', 'array', 'range') + return is_listish(typ) or is_range(typ) def get_iterable_elt(typ): - if is_iterable(typ): - return typ.find()["elt"].find() - elif is_str(typ) or is_bytes(typ): + if is_str(typ) or is_bytes(typ): return TInt(types.TValue(8)) + elif is_iterable(typ): + return typ.find()["elt"].find() else: assert False diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 9a3cbd0ed..d6a23a36e 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -180,7 +180,10 @@ class Inferencer(algorithm.Visitor): self.engine.process(diag) def _unify_iterable(self, element, collection): - if builtins.is_iterable(collection.type): + if builtins.is_bytes(collection.type): + self._unify(element.type, builtins.TInt(), + element.loc, None) + elif builtins.is_iterable(collection.type): rhs_type = collection.type.find() rhs_wrapped_lhs_type = types.TMono(rhs_type.name, {"elt": element.type}) self._unify(rhs_wrapped_lhs_type, rhs_type, diff --git a/artiq/test/lit/integration/subscript.py b/artiq/test/lit/integration/subscript.py index eaa7f8455..f0398be4c 100644 --- a/artiq/test/lit/integration/subscript.py +++ b/artiq/test/lit/integration/subscript.py @@ -17,3 +17,7 @@ assert lst[0:3:2] == [0, 2] lst = [0, 0, 0, 0, 0] lst[0:5:2] = [1, 2, 3] assert lst == [1, 0, 2, 0, 3] + +byt = b"abc" +assert byt[0] == 97 +assert byt[1] == 98