Skip to content

Spell bitwise not as ! and drop unary plus in Rust output [patch] - #129

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/rust-unary-operators
Sep 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/rust-unary-operators

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #97

What was wrong

RustGenerator did not override GetUnaryOperatorSpelling, so it used the C-family spellings and wrote (~x) and (+x). Rust has neither operator, and rustc rejects both.

Change

  • RustGenerator.GetUnaryOperatorSpelling:
    • BitwiseNot → !, which is Rust's bitwise complement on integers.
    • Plus → the operand alone. The shared emitter still wraps it in parentheses, so precedence is unchanged: (x), (-1).
    • Everything else falls through to the shared set.
  • UnaryOperandSignTests.AnOperandThatCannotFuseIsWrittenBare asserted the invalid (+-1) for Rust. That row now leaves Rust out, and Rust's spelling is asserted in RustGeneratorTests instead.

Tests

  • RustGeneratorTests.UnaryOperators_AreSpelledAsRustWritesThem checks (!x) for bitwise not, (x) for plus, and that logical not and negate are unchanged. UnaryPlusOnANegativeLiteral_IsTheLiteral checks (-1).
  • RustGeneratedSourceCompilesTests.UnaryOperatorsRustSpellsDifferently_Compile compiles functions that use both operators with rustc.
  • I reverted RustGenerator.cs and re-ran: all four new tests fail, including rustc rejecting the output. With the fix they pass.
  • Full Coder.Test suite on Linux, with rustc on the path: 987 passed, 0 failed.

🤖 Generated with Claude Code

https://claude.ai/code/session_015i2yeKwKRWtHhdc6NPySjW


Generated by Claude Code

RustGenerator fell back to the C-family unary spellings, so it wrote (~x)
and (+x). Rust has neither operator, and rustc rejects both. It now
overrides GetUnaryOperatorSpelling: BitwiseNot is "!", which is Rust's
bitwise complement on integers, and Plus writes the operand alone, still
parenthesised.

UnaryOperandSignTests asserted the invalid "(+-1)" for Rust. It now leaves
Rust out of the unary-plus row, and RustGeneratorTests covers Rust's
spelling. A rustc compile test covers both operators.

Fixes #97

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015i2yeKwKRWtHhdc6NPySjW
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 7219650 into main Sep 30, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/rust-unary-operators branch September 30, 2026 04:00
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.

Rust output uses (+x) and (~x), and neither unary operator exists in Rust, so the code fails to compile

2 participants