compiler: reject reachable implicit return if not returning TNone.

Fixes #718.
This commit is contained in:
whitequark 2017-04-21 18:11:14 +00:00
parent ed2b10c5aa
commit c5d7445973
3 changed files with 26 additions and 0 deletions

View File

@ -317,7 +317,15 @@ class ARTIQIRGenerator(algorithm.Visitor):
self.current_block.append(ir.Return(ir.Constant(None, builtins.TNone()))) self.current_block.append(ir.Return(ir.Constant(None, builtins.TNone())))
else: else:
if not self.current_block.is_terminated(): 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()) self.current_block.append(ir.Unreachable())
finally: finally:
self.name = old_name self.name = old_name
self.current_args = old_args self.current_args = old_args

View File

@ -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)

View File

@ -0,0 +1,9 @@
# RUN: %python -m artiq.compiler.testbench.signature %s
def foo(x):
if x:
return 1
else:
return 2
foo(True)