From c5d7445973dc126ed48a7ac5c98de8fb5fa7ad2a Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 21 Apr 2017 18:11:14 +0000 Subject: [PATCH] compiler: reject reachable implicit return if not returning TNone. Fixes #718. --- artiq/compiler/transforms/artiq_ir_generator.py | 8 ++++++++ artiq/test/lit/codegen/error_illegal_return.py | 9 +++++++++ artiq/test/lit/codegen/unreachable_implicit_return.py | 9 +++++++++ 3 files changed, 26 insertions(+) create mode 100644 artiq/test/lit/codegen/error_illegal_return.py create mode 100644 artiq/test/lit/codegen/unreachable_implicit_return.py diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index aea58f0d5..98fc8a90e 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -317,7 +317,15 @@ class ARTIQIRGenerator(algorithm.Visitor): self.current_block.append(ir.Return(ir.Constant(None, builtins.TNone()))) else: if not self.current_block.is_terminated(): + if len(self.current_block.predecessors()) != 0: + diag = diagnostic.Diagnostic("error", + "this function must return a value of type {typ} explicitly", + {"typ": types.TypePrinter().name(typ.ret)}, + node.keyword_loc) + self.engine.process(diag) + self.current_block.append(ir.Unreachable()) + finally: self.name = old_name self.current_args = old_args diff --git a/artiq/test/lit/codegen/error_illegal_return.py b/artiq/test/lit/codegen/error_illegal_return.py new file mode 100644 index 000000000..5e15cade7 --- /dev/null +++ b/artiq/test/lit/codegen/error_illegal_return.py @@ -0,0 +1,9 @@ +# RUN: %python -m artiq.compiler.testbench.signature +diag %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: ${LINE:+1}: error: this function must return a value of type numpy.int32 explicitly +def foo(x): + if x: + return 1 + +foo(True) diff --git a/artiq/test/lit/codegen/unreachable_implicit_return.py b/artiq/test/lit/codegen/unreachable_implicit_return.py new file mode 100644 index 000000000..a434f8a28 --- /dev/null +++ b/artiq/test/lit/codegen/unreachable_implicit_return.py @@ -0,0 +1,9 @@ +# RUN: %python -m artiq.compiler.testbench.signature %s + +def foo(x): + if x: + return 1 + else: + return 2 + +foo(True)