forked from M-Labs/artiq
compiler: do not permit mutation of bytes values (#714).
This commit is contained in:
parent
284382b1f5
commit
5d841d08e9
@ -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)
|
||||
|
@ -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)
|
||||
|
5
artiq/test/lit/inferencer/error_bytes_subscript_mut.py
Normal file
5
artiq/test/lit/inferencer/error_bytes_subscript_mut.py
Normal 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
|
Loading…
Reference in New Issue
Block a user