From 05fa80818a3310bc3499b2ef8484de148836df05 Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 4 Jan 2016 21:26:03 +0800 Subject: [PATCH] transforms.inferencer: make sure parallel/sequential is lone manager. --- artiq/compiler/transforms/inferencer.py | 16 ++++++++++++++-- lit-test/test/inferencer/error_with_many.py | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 lit-test/test/inferencer/error_with_many.py diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 3169ed3dd..6bc0cec3f 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -963,8 +963,7 @@ class Inferencer(algorithm.Visitor): self.generic_visit(node) typ = node.context_expr.type - if not (types.is_builtin(typ, "parallel") or - types.is_builtin(typ, "sequential") or + if (types.is_builtin(typ, "parallel") or types.is_builtin(typ, "sequential") or (isinstance(node.context_expr, asttyped.CallT) and types.is_builtin(node.context_expr.func.type, "watchdog"))): diag = diagnostic.Diagnostic("error", @@ -977,6 +976,19 @@ class Inferencer(algorithm.Visitor): self._unify(node.optional_vars.type, node.context_expr.type, node.optional_vars.loc, node.context_expr.loc) + def visit_With(self, node): + self.generic_visit(node) + + for item_node in node.items: + typ = item_node.context_expr.type.find() + if (types.is_builtin(typ, "parallel") or types.is_builtin(typ, "sequential")) and \ + len(node.items) != 1: + diag = diagnostic.Diagnostic("error", + "the '{kind}' context manager must be the only one in a 'with' statement", + {"kind": typ.name}, + node.keyword_loc.join(node.colon_loc)) + self.engine.process(diag) + def visit_ExceptHandlerT(self, node): self.generic_visit(node) diff --git a/lit-test/test/inferencer/error_with_many.py b/lit-test/test/inferencer/error_with_many.py new file mode 100644 index 000000000..ae323786a --- /dev/null +++ b/lit-test/test/inferencer/error_with_many.py @@ -0,0 +1,6 @@ +# RUN: %python -m artiq.compiler.testbench.inferencer +diag %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: ${LINE:+1}: error: the 'parallel' context manager must be the only one in a 'with' statement +with parallel, sequential: + pass