Skip to content

Commit f7640fc

Browse files
[mypyc] feat: try constant fold in convert_format_expr_to_str
1 parent c2a82b9 commit f7640fc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

mypyc/irbuild/format_str_tokenizer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,19 @@ def convert_format_expr_to_str(
143143
for x, format_op in zip(exprs, format_ops):
144144
node_type = builder.node_type(x)
145145
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?
146+
if is_str_rprimitive(node_type) or isinstance(x, StrExpr):
147+
# NOTE: why does mypyc think our fake StrExprs are not str rprimitives?
149148
var_str = builder.accept(x)
149+
elif (folded := constant_fold_expr(builder, x)) is not None:
150+
var_str = builder.accept(StrExpr(str(folded)))
150151
elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
151152
var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line)
152153
else:
153154
var_str = builder.primitive_op(str_op, [builder.accept(x)], line)
154155
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.accept(StrExpr(str(folded)))
158+
elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
156159
var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line)
157160
else:
158161
return None

0 commit comments

Comments
 (0)