Skip to content

A unary expression loses its static type: C writes void* b = (-a);, Go types -(1.5f) as float64, and Python/JS do true division on -a / 2 #267

Open

Description

@matt-edmondson

What's wrong

StaticType(Expression) in Coder/Languages/StandardLanguageGenerator.cs:322-371 handles literals, constructions, conditionals, binaries and variable references. It has no UnaryExpression case, so -a, !on and ~mask fall through to default: return null. Every caller that relies on the static type then goes wrong:

  • C inferred locals. CGenerator.cs:1189-1191 falls back to void*.
  • IsIntegerArithmetic (StandardLanguageGenerator.cs ~166) returns false. The Python and JS division/modulo overrides (PythonGenerator.cs:1043, JavaScriptGenerator.cs:756) use it, so they skip integer semantics.
  • InferredLiteralWidth (~311) loses the width. Go and Rust use it.

Reproduction

int f(int a)  { var b = -a; return b / 2; }
float g()     { var x = -(1.5f); return x; }
bool h(bool on) { var off = !on; return off; }
int k(int a)  { return -a / 2; }
Target Output Result
C void* b = (-a);, void* x = (-1.5f);, void* off = (!on); gcc: "invalid operands to binary /", "incompatible types when initializing type 'void *'"
Go x := (-1.5) "cannot use x (float64) as float32 value in return"
Python return ((-a) / 2) k(7) returns -3.5; the same function written as a / 2 is correctly floored/truncated
JS return ((-a) / 2) with no Math.trunc k(7) returns -3.5

Suggested fix

case UnaryExpression u:
    return u.Operator == UnaryOperator.LogicalNot ? new TypeReference("bool") : StaticType(u.Operand);

Acceptance criteria

  • C emits int b = (-a);, float x = ... and bool off = ... for the cases above, and the output compiles.
  • In Python and JS, -a / 2 with an int a truncates exactly as a / 2 does.
  • Go keeps float32 for -(1.5f).
  • Each case has a regression test in the existing per-language generator tests.

Activity

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