Repository navigation
String concatenation is a string, whatever the other operand is (#577) - #580
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #577.
What was reported
(ov as string)inside a concatenation compiled to:printing the buffer pointer as a decimal integer.
strlenof the result was 39 where 5029 was expected.Two defects, one wider than the report
1. Casts had no type.
_infer_expr_typehad noCastExpressionbranch, sox as Tfell through to thei32default._is_string_exprtherefore said no for(ov as string), and the operand went through_gen_stringify_exprinto a 64-byte stack buffer.2. Concatenation had no type either. Fixing the cast alone made things worse, which is how this surfaced. The
BinaryOperationbranch of_infer_expr_typetries Dual, Tensor, f64, f32, i64, u64, u32, and then falls back toleft 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
maintoday, with no cast involved: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 yieldsstringbefore 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) * 2is stilli64.Verification
pytest tests/./flow test --tier2 --strictruff check srcSix tests in
tests/integration/test_cast_in_string_concat.py: the issue's own repro assertinglen=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
--tier2run reported transpile failures inexamples/compilers/know_index_demo.flowandlsp_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.