Skip to content

Commit 32da5b9

Browse files
committed
fix cast tuple
1 parent c3cc04a commit 32da5b9

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

mypyc/irbuild/for_helpers.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,30 @@ def sequence_from_generator_preallocate_helper(
230230
implementation.
231231
"""
232232
if len(gen.sequences) == 1 and len(gen.indices) == 1 and len(gen.condlists[0]) == 0:
233+
line = gen.line
233234
sequence_expr = gen.sequences[0]
234235
rtype = builder.node_type(sequence_expr)
235236
if not (is_sequence_rprimitive(rtype) or isinstance(rtype, RTuple)):
236237
return None
237238
sequence = builder.accept(sequence_expr)
238-
length: Value
239+
length = get_expr_length_value(
240+
builder, sequence_expr, sequence, line, use_pyssize_t=True
241+
)
239242
if isinstance(rtype, RTuple):
240-
length = Integer(len(rtype.types), c_pyssize_t_rprimitive)
241243
# If input is RTuple, box it to tuple_rprimitive for generic iteration
242244
# TODO: this can be optimized a bit better with an unrolled ForRTuple helper
243-
sequence = builder.coerce(sequence, tuple_rprimitive, gen.line, force=True)
244-
assert is_tuple_rprimitive(sequence.type), sequence.type
245-
else:
246-
length = get_expr_length_value(
247-
builder, sequence_expr, sequence, gen.line, use_pyssize_t=True
248-
)
249-
target_op = empty_op_llbuilder(length, gen.line)
245+
sequence = builder.coerce(sequence, tuple_rprimitive, line, force=True)
246+
items = [builder.add(TupleGet(sequence, i, line) for i in range(len(rtype.types)))]
247+
sequence = builder.new_tuple(items, line)
248+
249+
target_op = empty_op_llbuilder(length, line)
250250

251251
def set_item(item_index: Value) -> None:
252252
e = builder.accept(gen.left_expr)
253-
builder.call_c(set_item_op, [target_op, item_index, e], gen.line)
253+
builder.call_c(set_item_op, [target_op, item_index, e], line)
254254

255255
for_loop_helper_with_index(
256-
builder, gen.indices[0], sequence_expr, sequence, set_item, gen.line, length
256+
builder, gen.indices[0], sequence_expr, sequence, set_item, line, length
257257
)
258258

259259
return target_op
@@ -1224,6 +1224,6 @@ def get_expr_length_value(
12241224
length = get_expr_length(expr)
12251225
if length is None:
12261226
# We cannot compute the length at compile time, so we will fetch it.
1227-
return builder.builder.builtin_len(expr_reg, line, use_pyssize_t=use_pyssize_t)
1227+
return builder.builtin_len(expr_reg, line, use_pyssize_t=use_pyssize_t)
12281228
# The expression result is known at compile time, so we can use a constant.
12291229
return Integer(length, c_pyssize_t_rprimitive if use_pyssize_t else short_int_rprimitive)

0 commit comments

Comments
 (0)