Skip to content

Rust output fails to compile ("literal out of range for i32") when an inferred local is initialised with a long literal above i32 range #122

Closed

Description

@matt-edmondson

What's wrong

RustGenerator.GenerateVariableDeclaration (Coder/Languages/RustGenerator.cs:1109-1129) leaves the type annotation off when the type is inferred. It then writes the initialiser through LanguageGeneratorBase, which emits a LiteralExpression<long> as bare digits with no suffix (LanguageGeneratorBase.cs:140-141). RustGenerator does not override literal emission.

Rust infers an unconstrained integer literal as i32. A long value outside the i32 range therefore trips the overflowing_literals lint, which is deny-by-default, and compilation fails.

Failure scenario

new FunctionDeclaration("main")
{
    Body = { new VariableDeclaration("big", null, Literal.BigNumberValue(5_000_000_000)) /* then only printed */ },
}

The generator emits let mut big = 5000000000;. If nothing later constrains big (for example, it is only printed), rustc 1.94.1 rejects it:

error: literal out of range for `i32`

Both the generator output and the rustc failure were reproduced during review. If a later use pins big to i64, the code compiles, so the failure needs an unconstrained local.

The other targets all keep the value's type:

  • C# adds the L suffix.
  • C++ uses auto.
  • Go uses :=.
  • Python and JavaScript don't need a width.

LiteralExpression<float> has a similar loss of intent: let x = 0.1; becomes an f64. It compiles, but the declared precision is gone.

Suggested fix

  • In RustGenerator, emit a long literal with an i64 suffix and a float literal with an f32 suffix (5000000000i64, 0.1f32). This mirrors what CSharpGenerator already does with L/f. Alternatively, write : i64 / : f32 on an inferred declaration whose initialiser is such a literal.
  • Add the case to RustGeneratedSourceCompilesTests so rustc checks it.

Related

#84 and #99 are also Rust compile failures, but from different causes.

Activity

  1. added
    bug
    on Sep 29, 2026
  2. matt-edmondson commented on Sep 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Suggested next step: override literal emission in RustGenerator to add the i64 and f32 suffixes, mirroring CSharpGenerator's L/f. Then add the unconstrained-local case to the rustc compile test.


    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