fix: preserve numeric type suffix in inline_const_as_literal#22412
Open
LeSingh1 wants to merge 2 commits into
Open
fix: preserve numeric type suffix in inline_const_as_literal#22412LeSingh1 wants to merge 2 commits into
LeSingh1 wants to merge 2 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
When inlining a numeric scalar const, append the const's type as a
literal suffix so the resulting literal keeps its type. Without this,
inlining `const N: i32 = 24;` followed by `N.wrapping_add(7i32)` would
produce `24.wrapping_add(7i32)`, which fails with E0689 because `24` is
left as an ambiguous `{integer}`.
The suffix is only attached when the const's outer type is one of the
built-in integer or float types and the rendered value ends in a digit,
which skips special floats like `inf` and `NaN`.
This change was prepared with the assistance of an LLM tool.
Closes rust-lang#22051
4953272 to
23a659d
Compare
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.
Closes #22051.
inline_const_as_literalwas stripping the type suffix off numeric scalar constants. Forconst BASE: i32 = 24;, invoking the assist onBASE.wrapping_add(7i32)produced24.wrapping_add(7i32), which fails to type-check with E0689 because24is left as an ambiguous{integer}literal and the method probe can't pick a concrete receiver type.The fix attaches the const's type as a literal suffix (
i32,u64,f32, ...) when the const's outer type is one of the built-in integer or float types. A small guard skips the suffix when the rendered value doesn't end in a digit, which covers special floats likeinfandNaNwhere appending a suffix would yield invalid syntax. Non-numeric and aggregate types (tuples, arrays, slices,&str,char,bool) are untouched.The existing scalar tests in this file expected the suffix-less output, so they're updated alongside the fix. Two new regression tests cover the bug report directly: one for the method-receiver case and one for an
f32value.I used an LLM tool while writing this patch (disclosed in the commit message and noted here per CONTRIBUTING.md).