From a5bb4a24af2e873402b4d51d05df80faa41c3040 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 10 May 2016 01:41:40 +0000 Subject: [PATCH] compiler: support short form of raise. Fixes #240. --- artiq/compiler/transforms/artiq_ir_generator.py | 5 ++++- artiq/compiler/transforms/inferencer.py | 15 ++++----------- artiq/test/lit/integration/raise.py | 8 ++++++++ 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 artiq/test/lit/integration/raise.py diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index c5b25de7a..ac74bdba3 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -599,7 +599,10 @@ class ARTIQIRGenerator(algorithm.Visitor): self.append(ir.Reraise()) def visit_Raise(self, node): - self.raise_exn(self.visit(node.exc), loc=self.current_loc) + if types.is_exn_constructor(node.exc.type): + self.raise_exn(self.alloc_exn(node.exc.type.instance), loc=self.current_loc) + else: + self.raise_exn(self.visit(node.exc), loc=self.current_loc) def visit_Try(self, node): dispatcher = self.add_block("try.dispatch") diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 4149c7392..50fb7a298 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -1290,20 +1290,13 @@ class Inferencer(algorithm.Visitor): if node.exc is not None: exc_type = node.exc.type - if not types.is_var(exc_type) and not builtins.is_exception(exc_type): - if types.is_exn_constructor(exc_type): - notes = [diagnostic.Diagnostic("note", - "this value is an exception constructor; use {suggestion} instead", - {"suggestion": node.exc.loc.source() + "()"}, - node.exc.loc)] - else: - notes = [] - + if types.is_exn_constructor(exc_type): + pass # short form + elif not types.is_var(exc_type) and not builtins.is_exception(exc_type): diag = diagnostic.Diagnostic("error", "cannot raise a value of type {type}, which is not an exception", {"type": types.TypePrinter().name(exc_type)}, - node.loc, - notes=notes) + node.loc) self.engine.process(diag) def visit_Assert(self, node): diff --git a/artiq/test/lit/integration/raise.py b/artiq/test/lit/integration/raise.py new file mode 100644 index 000000000..7b9d4fc70 --- /dev/null +++ b/artiq/test/lit/integration/raise.py @@ -0,0 +1,8 @@ +# RUN: %python -m artiq.compiler.testbench.jit %s +# RUN: %python %s +# REQUIRES: exceptions + +try: + raise ValueError +except ValueError: + pass