Skip to content

Commit 91e9cd3

Browse files
committed
Auto merge of rust-lang#18052 - Coekjan:fix-inline-const, r=Veykril
fix: Fix `inline_const_as_literal` error when the number >= 10 ## Description ### The Bug This PR fixes a small bug in the IDE assistence (`inline_const_as_literal`). When the being-inlined constant is a number and it is greater than or equal to 10, the assistence inserts unexpected string `(0x...)` after the number itself. A simple example is followed: Current `inline_const_as_literal` changes ```rs const A: usize = 16; fn f() -> usize { A // inline the constant } ``` into ```rs const A: usize = 16; fn f() -> usize { 16 (0x10) } ``` The bug originates from rust-lang#14925 & rust-lang#15306 . rust-lang#14925 added some unittests, but it just tested the number-inlining behavior when the number is `0`. https://github.com/rust-lang/rust-analyzer/blob/50882fbfa204027c84753e6d51a1a12884dc1b19/crates/ide-assists/src/handlers/inline_const_as_literal.rs#L124-L138 And rust-lang#15306 modified the behavior of `Const::render_eval` and added the `(0x...)` part after the number (if the number >= `10`). Because of insufficient unittests in rust-lang#14925, changes about `Const::render_eval` in rust-lang#15306 introduced this bug with no CI failure. ### The Fix I think `Const::render_eval` is intended for user-facing value displaying (e.g. hover) and not designed for `inline_const_as_literal`. To fix the bug, I defined a new function named `Const::eval`, which evaluates the value itself faithfully and simply and does nothing else. ## Thanks Thanks `@roife` for your kind help. Your guidance helped me better understand the code.
2 parents 97907ca + b41f6ab commit 91e9cd3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,17 @@ impl Const {
25532553
Type::from_value_def(db, self.id)
25542554
}
25552555

2556+
/// Evaluate the constant and return the result as a string.
2557+
///
2558+
/// This function is intended for IDE assistance, different from [`Const::render_eval`].
2559+
pub fn eval(self, db: &dyn HirDatabase, edition: Edition) -> Result<String, ConstEvalError> {
2560+
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
2561+
Ok(format!("{}", c.display(db, edition)))
2562+
}
2563+
2564+
/// Evaluate the constant and return the result as a string, with more detailed information.
2565+
///
2566+
/// This function is intended for user-facing display.
25562567
pub fn render_eval(
25572568
self,
25582569
db: &dyn HirDatabase,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>
5353
| ast::Expr::BinExpr(_)
5454
| ast::Expr::CallExpr(_) => {
5555
let edition = ctx.sema.scope(variable.syntax())?.krate().edition(ctx.db());
56-
match konst.render_eval(ctx.sema.db, edition) {
57-
Ok(result) => result,
58-
Err(_) => return None,
59-
}
56+
konst.eval(ctx.sema.db, edition).ok()?
6057
}
6158
_ => return None,
6259
};
@@ -127,12 +124,14 @@ mod tests {
127124
("u64", "0", NUMBER),
128125
("u128", "0", NUMBER),
129126
("usize", "0", NUMBER),
127+
("usize", "16", NUMBER),
130128
("i8", "0", NUMBER),
131129
("i16", "0", NUMBER),
132130
("i32", "0", NUMBER),
133131
("i64", "0", NUMBER),
134132
("i128", "0", NUMBER),
135133
("isize", "0", NUMBER),
134+
("isize", "16", NUMBER),
136135
("bool", "false", BOOL),
137136
("&str", "\"str\"", STR),
138137
("char", "'c'", CHAR),

0 commit comments

Comments
 (0)