From 586d97c6cb25832b40891ca3cdb252d9d8658502 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Mon, 21 Aug 2023 11:02:08 +0100 Subject: [PATCH] Fix type annotations with mixed tuples The type checker/inferer visits every node in an AST tree, including function return annotations. This means for a function definition like def f() -> TTuple([TInt32, TBool]): ... We attempt to type check the list [TInt32, TBool], which generates the unification constraint builtins.TBool ~ builtins.TInt. This causes an internal error due to compiler weirdness. We can avoid this by just nulling-out the return annotation in the embedding stage. The return type isn't actually used anywhere (it's extracted via the inspect module instead), so this is entirely safe. Arguments aren't affected by this, as we already nulled out the annotation (see visit_arg in embedding.py). Signed-off-by: Jonathan Coates --- artiq/compiler/embedding.py | 2 +- artiq/test/lit/embedding/mixed_tuple.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/embedding/mixed_tuple.py diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index d1b276766..502b364b9 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -564,7 +564,7 @@ class StitchingASTTypedRewriter(ASTTypedRewriter): node = asttyped.QuotedFunctionDefT( typing_env=extractor.typing_env, globals_in_scope=extractor.global_, signature_type=types.TVar(), return_type=types.TVar(), - name=node.name, args=node.args, returns=node.returns, + name=node.name, args=node.args, returns=None, body=node.body, decorator_list=node.decorator_list, keyword_loc=node.keyword_loc, name_loc=node.name_loc, arrow_loc=node.arrow_loc, colon_loc=node.colon_loc, at_locs=node.at_locs, diff --git a/artiq/test/lit/embedding/mixed_tuple.py b/artiq/test/lit/embedding/mixed_tuple.py new file mode 100644 index 000000000..aaf947149 --- /dev/null +++ b/artiq/test/lit/embedding/mixed_tuple.py @@ -0,0 +1,16 @@ +# RUN: %python -m artiq.compiler.testbench.embedding %s + +from artiq.language.core import * +from artiq.language.types import * + +@kernel +def consume_tuple(x: TTuple([TInt32, TBool])): + print(x) + +@kernel +def return_tuple() -> TTuple([TInt32, TBool]): + return (123, False) + +@kernel +def entrypoint(): + consume_tuple(return_tuple())