From 3573a8750aa46860b03df94c213c83fd7be84303 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 20 Jan 2016 03:20:25 +0000 Subject: [PATCH] transforms.inferencer: give a suggestion on "raise Exception". --- artiq/compiler/transforms/inferencer.py | 11 ++++++++++- artiq/test/lit/exceptions/error_raise_class.py | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/exceptions/error_raise_class.py diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 2a95c01e1..8432f9cae 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -1221,10 +1221,19 @@ 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 = [] + diag = diagnostic.Diagnostic("error", "cannot raise a value of type {type}, which is not an exception", {"type": types.TypePrinter().name(exc_type)}, - node.exc.loc) + node.loc, + notes=notes) self.engine.process(diag) def visit_Assert(self, node): diff --git a/artiq/test/lit/exceptions/error_raise_class.py b/artiq/test/lit/exceptions/error_raise_class.py new file mode 100644 index 000000000..3c85f0dc8 --- /dev/null +++ b/artiq/test/lit/exceptions/error_raise_class.py @@ -0,0 +1,5 @@ +# RUN: %python -m artiq.compiler.testbench.signature +diag %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: ${LINE:+1}: note: this value is an exception constructor; use IndexError() instead +raise IndexError