forked from M-Labs/artiq
1
0
Fork 0

compiler: support short form of raise.

Fixes #240.
This commit is contained in:
whitequark 2016-05-10 01:41:40 +00:00
parent 6d29e768a5
commit a5bb4a24af
3 changed files with 16 additions and 12 deletions

View File

@ -599,6 +599,9 @@ class ARTIQIRGenerator(algorithm.Visitor):
self.append(ir.Reraise()) self.append(ir.Reraise())
def visit_Raise(self, node): def visit_Raise(self, node):
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) self.raise_exn(self.visit(node.exc), loc=self.current_loc)
def visit_Try(self, node): def visit_Try(self, node):

View File

@ -1290,20 +1290,13 @@ class Inferencer(algorithm.Visitor):
if node.exc is not None: if node.exc is not None:
exc_type = node.exc.type 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): if types.is_exn_constructor(exc_type):
notes = [diagnostic.Diagnostic("note", pass # short form
"this value is an exception constructor; use {suggestion} instead", elif not types.is_var(exc_type) and not builtins.is_exception(exc_type):
{"suggestion": node.exc.loc.source() + "()"},
node.exc.loc)]
else:
notes = []
diag = diagnostic.Diagnostic("error", diag = diagnostic.Diagnostic("error",
"cannot raise a value of type {type}, which is not an exception", "cannot raise a value of type {type}, which is not an exception",
{"type": types.TypePrinter().name(exc_type)}, {"type": types.TypePrinter().name(exc_type)},
node.loc, node.loc)
notes=notes)
self.engine.process(diag) self.engine.process(diag)
def visit_Assert(self, node): def visit_Assert(self, node):

View File

@ -0,0 +1,8 @@
# RUN: %python -m artiq.compiler.testbench.jit %s
# RUN: %python %s
# REQUIRES: exceptions
try:
raise ValueError
except ValueError:
pass