|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | 10 | from collections.abc import Callable |
11 | | -from typing import ClassVar |
| 11 | +from typing import ClassVar, cast |
12 | 12 |
|
13 | 13 | from mypy.nodes import ( |
14 | 14 | ARG_POS, |
@@ -242,25 +242,45 @@ def sequence_from_generator_preallocate_helper( |
242 | 242 | rtype = builder.node_type(sequence_expr) |
243 | 243 | if not (is_sequence_rprimitive(rtype) or isinstance(rtype, RTuple)): |
244 | 244 | return None |
245 | | - sequence = builder.accept(sequence_expr) |
246 | | - length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True) |
| 245 | + |
247 | 246 | if isinstance(rtype, RTuple): |
248 | 247 | # If input is RTuple, box it to tuple_rprimitive for generic iteration |
249 | 248 | # TODO: this can be optimized a bit better with an unrolled ForRTuple helper |
250 | 249 | proper_type = get_proper_type(builder.types[sequence_expr]) |
251 | 250 | assert isinstance(proper_type, TupleType), proper_type |
252 | 251 |
|
253 | | - get_item_ops = [ |
254 | | - ( |
255 | | - LoadLiteral(typ.value, object_rprimitive) |
256 | | - if isinstance(typ, LiteralType) |
257 | | - else TupleGet(sequence, i, line) |
258 | | - ) |
259 | | - for i, typ in enumerate(get_proper_types(proper_type.items)) |
260 | | - ] |
| 252 | + # the for_loop_helper_with_index crashes for empty tuples, bail out |
| 253 | + if not proper_type.items: |
| 254 | + return None |
| 255 | + |
| 256 | + proper_types = get_proper_types(proper_type.items) |
| 257 | + |
| 258 | + get_item_ops: list[LoadLiteral | TupleGet] |
| 259 | + if all(isinstance(typ, LiteralType) for typ in proper_types): |
| 260 | + get_item_ops = [ |
| 261 | + LoadLiteral(cast(LiteralType, typ).value, object_rprimitive) |
| 262 | + for typ in proper_types |
| 263 | + ] |
| 264 | + |
| 265 | + else: |
| 266 | + sequence = builder.accept(sequence_expr) |
| 267 | + get_item_ops = [ |
| 268 | + ( |
| 269 | + LoadLiteral(typ.value, object_rprimitive) |
| 270 | + if isinstance(typ, LiteralType) |
| 271 | + else TupleGet(sequence, i, line) |
| 272 | + ) |
| 273 | + for i, typ in enumerate(proper_types) |
| 274 | + ] |
| 275 | + |
261 | 276 | items = list(map(builder.add, get_item_ops)) |
262 | 277 | sequence = builder.new_tuple(items, line) |
263 | 278 |
|
| 279 | + else: |
| 280 | + sequence = builder.accept(sequence_expr) |
| 281 | + |
| 282 | + length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True) |
| 283 | + |
264 | 284 | target_op = empty_op_llbuilder(length, line) |
265 | 285 |
|
266 | 286 | def set_item(item_index: Value) -> None: |
|
0 commit comments