Skip to content

Annotate an inferred Rust local whose initialiser is a long or a float [patch] - #212

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/122-rust-long-literal
Oct 13, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/122-rust-long-literal

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #122

What changed

RustGenerator.GenerateVariableDeclaration writes no type for an inferred local. When nothing else constrains an integer literal, Rust infers it as i32, and it infers a float literal as f64. That caused two problems:

  • let mut big = 5000000000; failed the deny-by-default overflowing_literals lint (literal out of range for i32).
  • let mut narrow = 0.1; compiled, but as an f64, so the declared float precision was lost.

Now, when the initialiser's static type (from StandardLanguageGenerator.StaticType) is long or float, the declaration says so: let mut big: i64 = 5000000000; and let mut narrow: f32 = …;. The annotation also goes to WriteValue as the declared type. int and double initialisers are what Rust would infer anyway, so they are still left unannotated: let mut small = 5;.

I chose an annotation over a literal suffix (5000000000i64). The suffix would change the spelling of every long and float literal everywhere, and only the inferred local is broken.

Tests (RustInferredLiteralWidthTests)

  • AnInferredLongOrFloatLocal_IsAnnotatedWithItsWidth: long and float locals get : i64 and : f32. int and double locals stay unannotated.
  • AnUnconstrainedLongLocal_Compiles: the same function, whose locals nothing reads, compiles with rustc --edition 2021. Before the fix it failed with literal out of range for i32.

With the generator change reverted, both tests fail. With it, the full suite passes (1117/1117).

🤖 Generated with Claude Code

https://claude.ai/code/session_01NgDciLntSfQTDeqFaJ3BM1


Generated by Claude Code

…t [patch]

Rust infers an unconstrained integer literal as i32 and a float as f64. An
inferred local holding a long past i32's range then failed the
deny-by-default overflowing_literals lint, and a float silently became an
f64. RustGenerator.GenerateVariableDeclaration now writes `: i64` or `: f32`
when the initialiser's static type is long or float. An int or double
initialiser is still left to inference.

Fixes #122

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NgDciLntSfQTDeqFaJ3BM1
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit bcd2c17 into main Oct 13, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/122-rust-long-literal branch October 13, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants