Skip to content

Commit

Permalink
compiler: support short form of raise.
Browse files Browse the repository at this point in the history
Fixes #240.
whitequark committed May 10, 2016
1 parent 6d29e76 commit a5bb4a2
Showing 3 changed files with 16 additions and 12 deletions.
5 changes: 4 additions & 1 deletion artiq/compiler/transforms/artiq_ir_generator.py
Original file line number Diff line number Diff line change
@@ -599,7 +599,10 @@ def raise_exn(self, exn=None, loc=None):
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")
15 changes: 4 additions & 11 deletions artiq/compiler/transforms/inferencer.py
Original file line number Diff line number Diff line change
@@ -1290,20 +1290,13 @@ def visit_Raise(self, node):

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):
8 changes: 8 additions & 0 deletions artiq/test/lit/integration/raise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RUN: %python -m artiq.compiler.testbench.jit %s
# RUN: %python %s
# REQUIRES: exceptions

try:
raise ValueError
except ValueError:
pass

0 comments on commit a5bb4a2

Please sign in to comment.