Skip to content

Adding a number to a string ("count: " + n) raises TypeError in Python, fails to compile in Go, and returns garbage in C++ #350

Open

Description

@matt-edmondson

What's wrong

Take string label(int n) { return "count: " + n; }, built as BinaryExpression(LiteralExpression<string>("count: "), Add, VariableReference("n")) with n an int parameter.

Target Output Result
C#, JavaScript ("count: " + n) correct
Rust format!("{}{}", "count: ", n) correct, because RustGenerator.IsStringJoin treats an Add with either operand a string as a join
Python return ("count: " + n) 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.

Activity

  1. added
    bug
    on Oct 29, 2026
  2. matt-edmondson commented on Oct 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions