Skip to content

Python/JavaScript do true division on a file-scope int field (LIMIT / 2 gives -3.5, C gives -3), and C types a local copied from it as void* #304

Closed

Description

@matt-edmondson

What's wrong

StandardLanguageGenerator.StaticType (Coder/Languages/StandardLanguageGenerator.cs:322-372) resolves a VariableReference only through staticTypeScopes (:361-370). Those scopes hold only a function's parameters and locals (PushStaticTypeScope, :82-95; RegisterStaticType, :287). A FieldDeclaration at file or namespace scope is never registered, so a bare reference to it has no static type, even though its Type is declared right alongside.

Python's and JavaScript's integer-division overrides depend on StaticType, so they fall back to true division. C's inferred-local typing depends on it too and falls back to void*. This is the same family as #273 (fixed for calls in 6284a3f), but field reads still fall through this gap.

Reproduce

var file = new SourceFile { Name = "probe" };
file.Members.Add(new FieldDeclaration("LIMIT", new TypeReference("int")) { IsStatic = true, IsConstant = true, InitialValue = new LiteralExpression<int>(-7) });
var half = new FunctionDeclaration { Name = "half", ReturnType = new TypeReference("int") };
half.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("LIMIT"), BinaryOperator.Divide, new LiteralExpression<int>(2))));
var copy = new FunctionDeclaration { Name = "copy", ReturnType = new TypeReference("int") };
copy.Body.Add(new VariableDeclaration("k", null, new VariableReference("LIMIT")) { IsTypeInferred = true });
copy.Body.Add(new ReturnStatement(new VariableReference("k")));
file.Members.Add(half); file.Members.Add(copy);
  • Python: return (LIMIT / 2), so half() returns -3.5
  • JavaScript: return (LIMIT / 2);, so half() returns -3.5
  • C / C# / C++ / Go / Rust: -3
  • C: void* k = LIMIT;. gcc 13 warns -Wint-conversion; gcc 14+ rejects it, since int-conversion is an error by default there.

Suggested fix

When StaticType resolves a VariableReference that no function scope declares, look it up among the FieldDeclarations (and file-scope VariableDeclarations) visible from GenerationRoot, the same way DeclaredReturnType (:388-400) resolves a callee. Resolve only when exactly one declaration has the name.

Acceptance criteria

  • LIMIT / 2 and LIMIT % 2 with an int file-scope field truncate in Python and JavaScript (-3 and -1 for -7).
  • An inferred local initialised from an int field is written int k in C (and in C++ if affected).
  • A name declared by more than one field, or shadowed by a parameter or local, keeps today's behaviour, with the parameter or local winning.

Activity

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