Skip to content

Commit 338f77f

Browse files
[mypyc] feat: use get_expr_length_value in translate_len
Currently, `translate_len` can determine the length of an RTuple at compile time This PR extends this capability to all expressions supported by `get_expr_length_value`
1 parent 2c6c395 commit 338f77f

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

mypyc/irbuild/specialize.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
is_int64_rprimitive,
7070
is_int_rprimitive,
7171
is_list_rprimitive,
72+
is_sequence_rprimitive,
7273
is_uint8_rprimitive,
7374
list_rprimitive,
7475
object_rprimitive,
@@ -221,17 +222,11 @@ def translate_len(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
221222
if len(expr.args) == 1 and expr.arg_kinds == [ARG_POS]:
222223
arg = expr.args[0]
223224
expr_rtype = builder.node_type(arg)
224-
if isinstance(expr_rtype, RTuple):
225-
# len() of fixed-length tuple can be trivially determined
226-
# statically, though we still need to evaluate it.
227-
builder.accept(arg)
228-
return Integer(len(expr_rtype.types))
225+
# NOTE (?) I'm not sure if my handling of can_borrow is correct here
226+
obj = builder.accept(arg, can_borrow=is_list_rprimitive(expr_rtype))
227+
if is_sequence_rprimitive(expr_rtype) or isinstance(expr_rtype, RTuple):
228+
return get_expr_length_value(builder, arg, obj, expr.line, use_pyssize_t=False)
229229
else:
230-
if is_list_rprimitive(builder.node_type(arg)):
231-
borrow = True
232-
else:
233-
borrow = False
234-
obj = builder.accept(arg, can_borrow=borrow)
235230
return builder.builtin_len(obj, expr.line)
236231
return None
237232

0 commit comments

Comments
 (0)