From 5d841d08e933c0c415d4c377fee48764f0684795 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 9 Jun 2017 07:24:57 +0000 Subject: [PATCH] compiler: do not permit mutation of bytes values (#714). --- artiq/compiler/analyses/constness.py | 9 ++++++++- artiq/compiler/testbench/inferencer.py | 2 ++ artiq/test/lit/inferencer/error_bytes_subscript_mut.py | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/inferencer/error_bytes_subscript_mut.py diff --git a/artiq/compiler/analyses/constness.py b/artiq/compiler/analyses/constness.py index c6458497f..fbca1ac9b 100644 --- a/artiq/compiler/analyses/constness.py +++ b/artiq/compiler/analyses/constness.py @@ -4,7 +4,7 @@ as constant is ever set. """ from pythonparser import algorithm, diagnostic -from .. import types +from .. import types, builtins class Constness(algorithm.Visitor): def __init__(self, engine): @@ -23,6 +23,13 @@ class Constness(algorithm.Visitor): self.visit(node.slice) self.in_assign = old_in_assign + if self.in_assign and builtins.is_bytes(node.value.type): + diag = diagnostic.Diagnostic("error", + "type {typ} is not mutable", + {"typ": "bytes"}, + node.loc) + self.engine.process(diag) + def visit_AttributeT(self, node): old_in_assign, self.in_assign = self.in_assign, False self.visit(node.value) diff --git a/artiq/compiler/testbench/inferencer.py b/artiq/compiler/testbench/inferencer.py index 3e36d0a38..e7deffd33 100644 --- a/artiq/compiler/testbench/inferencer.py +++ b/artiq/compiler/testbench/inferencer.py @@ -3,6 +3,7 @@ from pythonparser import source, diagnostic, algorithm, parse_buffer from .. import prelude, types from ..transforms import ASTTypedRewriter, Inferencer, IntMonomorphizer, CastMonomorphizer from ..transforms import IODelayEstimator +from ..analyses import Constness class Printer(algorithm.Visitor): """ @@ -83,6 +84,7 @@ def main(): parsed, comments = parse_buffer(buf, engine=engine) typed = ASTTypedRewriter(engine=engine, prelude=prelude.globals()).visit(parsed) Inferencer(engine=engine).visit(typed) + Constness(engine=engine).visit(typed) if monomorphize: CastMonomorphizer(engine=engine).visit(typed) IntMonomorphizer(engine=engine).visit(typed) diff --git a/artiq/test/lit/inferencer/error_bytes_subscript_mut.py b/artiq/test/lit/inferencer/error_bytes_subscript_mut.py new file mode 100644 index 000000000..96abe780d --- /dev/null +++ b/artiq/test/lit/inferencer/error_bytes_subscript_mut.py @@ -0,0 +1,5 @@ +# RUN: %python -m artiq.compiler.testbench.inferencer +diag %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: ${LINE:+1}: error: type bytes is not mutable +(b"a")[0] = 1