From 3a1f14c16c5482274560a382059c6a10f74f052b Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 26 Feb 2017 01:58:21 +0000 Subject: [PATCH] compiler: fix overly strict constness analysis. Before this commit, the following code would fail to compile... obj.foo.bar = True ... if foo is marked kernel_invariant in obj, even if bar is not marked as such in obj.foo. --- artiq/compiler/analyses/constness.py | 5 ++++- artiq/test/lit/embedding/invariant_nested.py | 22 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/embedding/invariant_nested.py diff --git a/artiq/compiler/analyses/constness.py b/artiq/compiler/analyses/constness.py index add1f7fac..c6458497f 100644 --- a/artiq/compiler/analyses/constness.py +++ b/artiq/compiler/analyses/constness.py @@ -24,7 +24,10 @@ class Constness(algorithm.Visitor): self.in_assign = old_in_assign def visit_AttributeT(self, node): - self.generic_visit(node) + old_in_assign, self.in_assign = self.in_assign, False + self.visit(node.value) + self.in_assign = old_in_assign + if self.in_assign: typ = node.value.type.find() if types.is_instance(typ) and node.attr in typ.constant_attributes: diff --git a/artiq/test/lit/embedding/invariant_nested.py b/artiq/test/lit/embedding/invariant_nested.py new file mode 100644 index 000000000..5957dcb3d --- /dev/null +++ b/artiq/test/lit/embedding/invariant_nested.py @@ -0,0 +1,22 @@ +# RUN: %python -m artiq.compiler.testbench.embedding %s + +from artiq.language.core import * +from artiq.language.types import * + + +class ClassA: + def __init__(self): + self.foo = False + + +class ClassB: + kernel_invariants = {"bar"} + + def __init__(self): + self.bar = ClassA() + +obj = ClassB() + +@kernel +def entrypoint(): + obj.bar.foo = True