Skip to content

Commit 099b047

Browse files
committed
Use non-native calls to registered implementations
Registered implementations are always decorated because they always have a register decorator, and we can't make native calls to decorated functions because otherwise we wouldn't be able to use the function emitted from the decorator (which would be a Python callable object). Previously, we removed the register decorator on those implementations and treated them as non-decorated functions, but that strategy stops working as soon as you add another decorator, so this just treats those implementations as regular decorated functions and uses a non-native call for those functions.
1 parent 33798df commit 099b047

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

mypyc/irbuild/function.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,19 +779,25 @@ def add_singledispatch_registered_impls(builder: IRBuilder) -> None:
779779
line = fitem.line
780780
current_func_decl = builder.mapper.func_to_decl[fitem]
781781
arg_info = get_args(builder, current_func_decl.sig.args, line)
782+
783+
def gen_func_call_and_return(func_name: str) -> None:
784+
func = builder.load_global_str(func_name, line)
785+
# TODO: don't pass optional arguments if they weren't passed to this function
786+
ret_val = builder.builder.py_call(
787+
func, arg_info.args, line, arg_info.arg_kinds, arg_info.arg_names
788+
)
789+
coerced = builder.coerce(ret_val, current_func_decl.sig.ret_type, line)
790+
builder.nonlocal_control[-1].gen_return(builder, coerced, line)
791+
782792
# Reverse the list of registered implementations so we use the implementations defined later
783793
# if there are multiple overlapping implementations
784794
for dispatch_type, impl in reversed(impls):
785-
func_decl = builder.mapper.func_to_decl[impl]
786795
call_impl, next_impl = BasicBlock(), BasicBlock()
787796
should_call_impl = check_if_isinstance(builder, arg_info.args[0], dispatch_type, line)
788797
builder.add_bool_branch(should_call_impl, call_impl, next_impl)
789798

790799
# Call the registered implementation
791800
builder.activate_block(call_impl)
792801

793-
ret_val = builder.builder.call(
794-
func_decl, arg_info.args, arg_info.arg_kinds, arg_info.arg_names, line
795-
)
796-
builder.nonlocal_control[-1].gen_return(builder, ret_val, line)
802+
gen_func_call_and_return(impl.name)
797803
builder.activate_block(next_impl)

0 commit comments

Comments
 (0)