Skip to content

Commit b41f6ab

Browse files
committed
fix: Fix inline_const_as_literal error when the number >= 10
1 parent 1c26c99 commit b41f6ab

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)