Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Use primitives for calls via type aliases #14784

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Statement,
SymbolNode,
TupleExpr,
TypeAlias,
TypeInfo,
UnaryExpr,
Var,
Expand Down Expand Up @@ -1018,7 +1019,8 @@ def call_refexpr_with_args(

# Handle data-driven special-cased primitive call ops.
if callee.fullname and expr.arg_kinds == [ARG_POS] * len(arg_values):
call_c_ops_candidates = function_ops.get(callee.fullname, [])
fullname = get_call_target_fullname(callee)
call_c_ops_candidates = function_ops.get(fullname, [])
target = self.builder.matching_call_c(
call_c_ops_candidates, arg_values, expr.line, self.node_type(expr)
)
Expand Down Expand Up @@ -1349,3 +1351,12 @@ def remangle_redefinition_name(name: str) -> str:
lookups.
"""
return name.replace("'", "__redef__")


def get_call_target_fullname(ref: RefExpr) -> str:
if isinstance(ref.node, TypeAlias):
# Resolve simple type aliases. In calls they evaluate to the type they point to.
target = get_proper_type(ref.node.target)
if isinstance(target, Instance):
return target.type.fullname
return ref.fullname
16 changes: 16 additions & 0 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ L0:
x = r0
return 1

[case testNewListEmptyViaAlias]
from typing import List

ListAlias = list

def f() -> None:
x: List[int] = ListAlias()

[out]
def f():
r0, x :: list
L0:
r0 = PyList_New(0)
x = r0
return 1

[case testNewListTwoItems]
from typing import List
def f() -> None:
Expand Down