compiler: Fix type inference for "ternary" if expressions

Previously, any type would be accepted for the test expression,
leading to internal errors in the code generator if the passed
value wasn't in fact a bool.
This commit is contained in:
David Nadlinger 2021-03-20 00:27:25 +00:00 committed by Sebastien Bourdeauducq
parent ca6db87895
commit 01352236ee
2 changed files with 7 additions and 0 deletions

View File

@ -305,6 +305,7 @@ class Inferencer(algorithm.Visitor):
def visit_IfExpT(self, node): def visit_IfExpT(self, node):
self.generic_visit(node) self.generic_visit(node)
self._unify(node.test.type, builtins.TBool(), node.test.loc, None)
self._unify(node.body.type, node.orelse.type, self._unify(node.body.type, node.orelse.type,
node.body.loc, node.orelse.loc) node.body.loc, node.orelse.loc)
self._unify(node.type, node.body.type, self._unify(node.type, node.body.type,

View File

@ -0,0 +1,6 @@
# RUN: %python -m artiq.compiler.testbench.inferencer %s >%t
# RUN: OutputCheck %s --file-to-check=%t
# CHECK-L: def foo(val:bool)->numpy.int?:
def foo(val):
return 1 if val else 0