Skip to content

Python and JavaScript use true division on a call result: twice(a) / 4 gives -3.5 where the other targets give -3 #273

Closed

Description

@matt-edmondson

What's wrong

The integer-division fix (#114, #183, #191) only applies when StandardLanguageGenerator.IsIntegerArithmetic can prove both operands are integers (Coder/Languages/StandardLanguageGenerator.cs ~L166-L170).

StaticType(Expression) (~L321-L370) has no CallExpression case, so the type of any call result is unknown. The Python and JavaScript division overrides then fall back to true division (PythonGenerator.cs ~L1048, JavaScriptGenerator.cs ~L761). This happens even when:

  • the callee is a sibling FunctionDeclaration in the same file that declares ReturnType = "int", and
  • the result is assigned to a local declared int, or returned from a function declared int.

Reproduction

int twice(int a) { return a * 2; }
int half(int a)  { int t = twice(a); t = twice(a) / 4; return t; }
// entry point prints half(-7)
Target Generated half(-7)
C#, C, C++, Go, Rust twice(a) / 4 -3
Python t = (twice(a) / 4) -3.5, stored in a local annotated int
JavaScript t = (twice(a) / 4); with no Math.trunc -3.5

The program compiles in every target, so nothing flags the wrong value.

This is in the same family as #267, where a unary expression loses its static type. The cause and fix here are separate: a call, not a unary operator.

Suggested fix

Add a CallExpression case to StaticType:

  1. If the call has an ExpectedType, use it. This is already how the code behaves, and setting it is the only workaround today.
  2. Otherwise, for a call with no receiver, resolve Callee against the FunctionDeclarations in the enclosing SourceFile, namespace or class. When exactly one function matches, return its ReturnType.

Acceptance criteria

  • In Python and JavaScript, twice(a) / 4 truncates exactly as a / 4 does when twice is declared int.
  • % follows the same rule.
  • A call to an unknown or overloaded callee keeps today's behaviour.
  • Regression tests are added to the existing Python and JavaScript 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

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions