Skip to content

s += name on strings fails to compile in Rust (E0308) and C (invalid operands): the #240 fix only covers a + b, not += #363

Closed

Description

@matt-edmondson

What's wrong

#240 / PR #323 made string Add work in Rust (format!, RustGenerator.GenerateBinary) and added an explanatory note in C (CGenerator.GenerateBinary). Both hooks only see a BinaryExpression.

An AssignmentStatement with AssignmentOperator.AddAssign on a string target goes through StandardLanguageGenerator.GenerateAssignmentStatement (Coder/Languages/StandardLanguageGenerator.cs, around lines 1929-1956) and comes out as a plain +=.

The compound form has needed its own handling before. Floating %= is special-cased in that same method, and #183 was the /= follow-up to #114.

Repro (HEAD 89d8424)

The input is string greet(string name) { string s = "Hello, "; s += name; return s; }, with the assignment built as new AssignmentStatement(new VariableReference("s"), new VariableReference("name"), AssignmentOperator.AddAssign):

Target Generated Compiler
Rust let mut s: String = String::from("Hello, "); / s += name; rustc E0308: expected &str, found String
C const char* s = "Hello, "; / s += name; gcc -std=c11: invalid operands to binary + (have 'const char *' and 'const char *'), and no note

Go and C++ (std::string) compile as they are.

Suggested fix / acceptance criteria

  • Rust: when the target's static type is an owned String and the value is a String, write s += &name (or s.push_str(&name)). Keep s += "lit" as it is.
  • C: put the same "C has no string concatenation" note in front of a string += that GenerateBinary puts in front of a string +.
  • Extend StringConcatenationTests:
    • Rust: s += name and s += "!" compile with rustc.
    • C: the output carries the note.

Activity

  1. added
    bug
    on Oct 30, 2026
  2. matt-edmondson commented on Oct 30, 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

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions