Skip to content

String concatenation is a string, whatever the other operand is (#577) - #580

Merged
godofecht merged 2 commits into
mainfrom
fix-string-concat-inference
Aug 19, 2026
Merged

godofecht merged 2 commits into
mainfrom
fix-string-concat-inference

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #577.

What was reported

(ov as string) inside a concatenation compiled to:

snprintf(_flow_strval_15694000, 64, "%d", ((char*)(ov)))

printing the buffer pointer as a decimal integer. strlen of the result was 39 where 5029 was expected.

Two defects, one wider than the report

1. Casts had no type. _infer_expr_type had no CastExpression branch, so x as T fell through to the i32 default. _is_string_expr therefore said no for (ov as string), and the operand went through _gen_stringify_expr into a 64-byte stack buffer.

2. Concatenation had no type either. Fixing the cast alone made things worse, which is how this surfaced. The BinaryOperation branch of _infer_expr_type tries Dual, Tensor, f64, f32, i64, u64, u32, and then falls back to left or right. There is no string case at all.

Concatenation only ever worked because an unrecognised operand missed every numeric rule and landed on the string by accident. Anything the rules did recognise was claimed by them. On main today, with no cast involved:

let v: i64 = 42     println("i64=" + v)     # prints 4344346952
let f: f64 = 1.5    println("f64=" + f)     # prints 0.000000

Inferring the cast correctly turned (n as i64) into a recognised operand, so "int=" + (n as i64) started failing the same way. My own new test caught that before I pushed it.

The fix

+ with a string on either side yields string before the numeric promotions are consulted. That is the correct rule regardless of the cast bug, and it fixes the reported case, the two cases above, and the one the cast fix broke.

Arithmetic without a string is untouched and still promotes: (a: i32 + b: i64) * 2 is still i64.

Verification

Check Result
pytest tests/ 1665 passed, 6 skipped
./flow test --tier2 --strict 687/687
ruff check src clean

Six tests in tests/integration/test_cast_in_string_concat.py: the issue's own repro asserting len=5029, that the content survives and not only the length, that numeric casts still format as numbers, that the workaround from the issue still works, the wider i64/f64/u32 cases, and that arithmetic without a string still promotes.

One note on the run: an early --tier2 run reported transpile failures in examples/compilers/know_index_demo.flow and lsp_ordering_port.flow. Both transpile cleanly on their own, and two subsequent full runs were 687/687, so I am recording it as transient rather than claiming it never happened.

godofecht and others added 2 commits August 19, 2026 12:14
Reported: `(ov as string)` inside a concatenation compiled to

    snprintf(_flow_strval[64], 64, "%d", ((char*)(ov)))

printing the buffer pointer as a decimal integer. strlen of the result was
39 where 5029 was expected.

Two defects, one of which is wider than the report.

`_infer_expr_type` had no CastExpression branch, so `x as T` fell through to
the i32 default. `_is_string_expr` therefore said no for `(ov as string)`,
and the operand went through _gen_stringify_expr into a 64-byte buffer.

Fixing that alone made things worse, which is how the second defect showed
up. The BinaryOperation branch has no string case: it tries Dual, Tensor,
f64, f32, i64, u64, u32 and then falls back to `left or right`. Concatenation
only ever worked because an unrecognised operand missed every numeric rule
and landed on the string by accident. Anything the rules did recognise was
claimed by them, so on main today:

    let v: i64 = 42     println("i64=" + v)     # prints a pointer
    let f: f64 = 1.5    println("f64=" + f)     # prints 0.000000

Inferring the cast correctly turned `(n as i64)` into a recognised operand
and broke `"int=" + (n as i64)` the same way. `+` with a string on either
side now yields string before the numeric promotions are consulted, which
fixes the reported case, the cases above, and the one my own fix broke.

Arithmetic without a string is untouched and still promotes.

Closes #577

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit a06589a into main Aug 19, 2026
18 checks passed
@godofecht
godofecht deleted the fix-string-concat-inference branch August 19, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transpiler miscompiles (array<u8,N> as string) inside a string-concat flatten

1 participant