You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Adding a number to a string ("count: " + n) raises TypeError in Python, fails to compile in Go, and returns garbage in C++ #350
Take string label(int n) { return "count: " + n; }, built as BinaryExpression(LiteralExpression<string>("count: "), Add, VariableReference("n")) with n an int parameter.
TypeError: can only concatenate str (not "int") to str
Go
return ("count: " + n)
compile error: invalid operation: "count: " + n (mismatched types untyped string and int)
C++
return ("count: " + n);
compiles, then does pointer arithmetic on the literal: label(3) returns "nt: ", and once n > 7 the behaviour is undefined
These results come from running the generated code through python3, go and g++.
Cause
PythonGenerator.GenerateBinary (Coder/Languages/PythonGenerator.cs:1118) and GoGenerator.GenerateBinary (Coder/Languages/GoGenerator.cs:1824) only special-case integer division and boolean bitwise operators. C++ has no override at all. So all three fall through to StandardLanguageGenerator.GenerateBinary and write a bare +.
#240 fixed string + string for C and Rust. Mixed string + non-string was not covered.
Suggested fix
Reuse Rust's rule: an Add where one operand's StaticType is string and the other's isn't is a join. Wrap the non-string operand:
Python:str(x), or emit an f-string
Go:fmt.Sprint(x), adding the fmt import (or strconv.Itoa for integers)
C++:std::to_string(x). For a literal left operand, also wrap it in std::string("...")
Acceptance criteria
The Python, Go and C++ output for the example above runs or compiles, and label(3) returns "count: 3".
Tests sit next to Coder.Test/Languages/StringConcatenationTests.cs, covering string + int and int + string.
Priority: High. "text" + n is one of the most common expressions there is. Python raises at runtime and Go fails to compile. C++ compiles and returns garbage through pointer arithmetic, with undefined behaviour once n exceeds the literal's length, and that failure is silent and memory-unsafe.
Area:GenerateBinary in the Python, Go and C++ generators. Rust's IsStringJoin rule is the model to follow.
Activity
matt-edmondson commentedon Oct 29, 2026
Triage
"text" + nis one of the most common expressions there is. Python raises at runtime and Go fails to compile. C++ compiles and returns garbage through pointer arithmetic, with undefined behaviour oncenexceeds the literal's length, and that failure is silent and memory-unsafe.GenerateBinaryin the Python, Go and C++ generators. Rust'sIsStringJoinrule is the model to follow.Addon string operands) fails to compile in C and Rust:"Hello, " + name(C: invalid operands; Rust: E0369/E0308) #240, which fixed string + string for C and Rust only.Generated by Claude Code