Skip to content

Commit aa227ff

Browse files
Update constant_fold.py
1 parent 31f9f49 commit aa227ff

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
9595
return None
9696
folded_items.append(val)
9797
return folded_callee.join(folded_items)
98+
99+
# builtins.str methods
100+
elif isinstance(folded_callee, bytes):
101+
# str.join
102+
if (
103+
callee.name == "join"
104+
and len(args := expr.args) == 1
105+
# TODO extend this to work with rtuples comprised of known literal values
106+
and isinstance(arg := args[0], (ListExpr, TupleExpr))
107+
):
108+
folded_items = []
109+
for item in arg.items:
110+
val = constant_fold_expr(builder, item)
111+
if not isinstance(val, bytes):
112+
return None
113+
folded_items.append(val)
114+
return folded_callee.join(folded_items)
98115
return None
99116

100117

0 commit comments

Comments
 (0)