Skip to content

fix: preserve numeric type suffix in inline_const_as_literal#22412

Open
LeSingh1 wants to merge 2 commits into
rust-lang:masterfrom
LeSingh1:inline-const-preserve-numeric-suffix
Open

fix: preserve numeric type suffix in inline_const_as_literal#22412
LeSingh1 wants to merge 2 commits into
rust-lang:masterfrom
LeSingh1:inline-const-preserve-numeric-suffix

Conversation

@LeSingh1
Copy link
Copy Markdown

@LeSingh1 LeSingh1 commented May 20, 2026

Closes #22051.

inline_const_as_literal was stripping the type suffix off numeric scalar constants. For const BASE: i32 = 24;, invoking the assist on BASE.wrapping_add(7i32) produced 24.wrapping_add(7i32), which fails to type-check with E0689 because 24 is 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 like inf and NaN where 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 f32 value.

I used an LLM tool while writing this patch (disclosed in the commit message and noted here per CONTRIBUTING.md).

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 20, 2026
@rustbot

This comment has been minimized.

LeSingh1 added 2 commits May 20, 2026 20:49
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
@LeSingh1 LeSingh1 force-pushed the inline-const-preserve-numeric-suffix branch from 4953272 to 23a659d Compare May 21, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inline_const_as_literal: drops type suffix from numeric literal producing E0689

2 participants