|
23 | 23 | is_str_rprimitive, |
24 | 24 | ) |
25 | 25 | from mypyc.irbuild.builder import IRBuilder |
| 26 | +from mypyc.irbuild.constant_fold import constant_fold_expr |
26 | 27 | from mypyc.primitives.bytes_ops import bytes_build_op |
27 | 28 | from mypyc.primitives.int_ops import int_to_str_op |
28 | 29 | from mypyc.primitives.str_ops import str_build_op, str_op |
@@ -143,16 +144,18 @@ def convert_format_expr_to_str( |
143 | 144 | for x, format_op in zip(exprs, format_ops): |
144 | 145 | node_type = builder.node_type(x) |
145 | 146 | if format_op == FormatOp.STR: |
146 | | - if is_str_rprimitive(node_type) or isinstance( |
147 | | - x, StrExpr |
148 | | - ): # NOTE: why does mypyc think our fake StrExprs are not str rprimitives? |
| 147 | + if isinstance(folded := constant_fold_expr(builder, x), str): |
| 148 | + var_str = builder.load_literal_value(folded) |
| 149 | + elif is_str_rprimitive(node_type): |
149 | 150 | var_str = builder.accept(x) |
150 | 151 | elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type): |
151 | 152 | var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line) |
152 | 153 | else: |
153 | 154 | var_str = builder.primitive_op(str_op, [builder.accept(x)], line) |
154 | 155 | elif format_op == FormatOp.INT: |
155 | | - if is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type): |
| 156 | + if isinstance(folded := constant_fold_expr(builder, x), int): |
| 157 | + var_str = builder.load_literal_value(str(folded)) |
| 158 | + elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type): |
156 | 159 | var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line) |
157 | 160 | else: |
158 | 161 | return None |
|
0 commit comments