Repository navigation
Annotate an inferred Rust local whose initialiser is a long or a float [patch] - #212
Merged
Merged
Conversation
…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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #122
What changed
RustGenerator.GenerateVariableDeclarationwrites no type for an inferred local. When nothing else constrains an integer literal, Rust infers it asi32, and it infers a float literal asf64. That caused two problems:let mut big = 5000000000;failed the deny-by-defaultoverflowing_literalslint (literal out of range for i32).let mut narrow = 0.1;compiled, but as anf64, so the declaredfloatprecision was lost.Now, when the initialiser's static type (from
StandardLanguageGenerator.StaticType) islongorfloat, the declaration says so:let mut big: i64 = 5000000000;andlet mut narrow: f32 = …;. The annotation also goes toWriteValueas the declared type.intanddoubleinitialisers 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 everylongandfloatliteral everywhere, and only the inferred local is broken.Tests (
RustInferredLiteralWidthTests)AnInferredLongOrFloatLocal_IsAnnotatedWithItsWidth:longandfloatlocals get: i64and: f32.intanddoublelocals stay unannotated.AnUnconstrainedLongLocal_Compiles: the same function, whose locals nothing reads, compiles withrustc --edition 2021. Before the fix it failed withliteral 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