forked from M-Labs/artiq
1
0
Fork 0

compiler: do not permit mutation of bytes values (#714).

This commit is contained in:
whitequark 2017-06-09 07:24:57 +00:00
parent 284382b1f5
commit 5d841d08e9
3 changed files with 15 additions and 1 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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