Skip to content

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

Description

@matt-edmondson

What's wrong

StandardLanguageGenerator.GetUnaryOperatorSpelling (Coder/Languages/StandardLanguageGenerator.cs:747) falls back to the C-family symbols in OperatorSymbols (Coder/Ast/OperatorSymbols.cs:127-129): Plus => "+" and BitwiseNot => "~". Go and Python override the method where their spelling differs (GoGenerator.cs:258 writes ^ for bitwise not). RustGenerator does not override it.

Repro

new RustGenerator().Generate(new UnaryExpression(UnaryOperator.Plus, new VariableReference("x")));       // "(+x)"
new RustGenerator().Generate(new UnaryExpression(UnaryOperator.BitwiseNot, new VariableReference("x")));  // "(~x)"

rustc rejects both:

  • let a = (+x); fails with error: expected expression, found '+'
  • let b = (~x); fails with error: '~' cannot be used as a unary operator

Coder.Test/Languages/UnaryOperandSignTests.cs includes [DataRow(UnaryOperator.Plus, -1, "(+-1)")] for Rust. That test locks in the invalid output.

Why it matters

Any AST that uses bitwise complement, such as mask manipulation like flags & ~MASK, or an explicit unary plus, produces Rust that does not compile.

Suggested fix

Override GetUnaryOperatorSpelling in RustGenerator:

  • BitwiseNot => "!". Rust's ! is bitwise not on integers.
  • Plus => "". Unary plus is the identity, so write just the operand, and keep the parentheses if needed for precedence.

Correct the Rust expectation in UnaryOperandSignTests.

Acceptance: Rust output for both operators compiles with rustc, and the tests assert (!x) and (x).

Activity

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

    bugSomething isn't workingreadyFully specified; implement as written

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions