about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcoekjan <cn_yzr@qq.com>2024-09-05 13:50:44 +0800
committercoekjan <cn_yzr@qq.com>2024-09-05 14:26:21 +0800
commitb41f6ab525dbab21a73de3dc844b87a98b36190b (patch)
treeeb0463c696304f56d83fadbd2728317c7098b577
parent1c26c99fcd741c801e08f4007d31398dd61f8921 (diff)
downloadrust-b41f6ab525dbab21a73de3dc844b87a98b36190b.tar.gz
rust-b41f6ab525dbab21a73de3dc844b87a98b36190b.zip
fix: Fix `inline_const_as_literal` error when the number >= 10
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/lib.rs11
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs7
2 files changed, 14 insertions, 4 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/lib.rs b/src/tools/rust-analyzer/crates/hir/src/lib.rs
index 81d6466c2f3..410e05fb1d1 100644
--- a/src/tools/rust-analyzer/crates/hir/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/hir/src/lib.rs
@@ -2553,6 +2553,17 @@ impl Const {
         Type::from_value_def(db, self.id)
     }
 
+    /// Evaluate the constant and return the result as a string.
+    ///
+    /// This function is intended for IDE assistance, different from [`Const::render_eval`].
+    pub fn eval(self, db: &dyn HirDatabase, edition: Edition) -> Result<String, ConstEvalError> {
+        let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
+        Ok(format!("{}", c.display(db, edition)))
+    }
+
+    /// Evaluate the constant and return the result as a string, with more detailed information.
+    ///
+    /// This function is intended for user-facing display.
     pub fn render_eval(
         self,
         db: &dyn HirDatabase,
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs
index f1c2acdd3ed..6b504a918b4 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs
@@ -53,10 +53,7 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>
             | ast::Expr::BinExpr(_)
             | ast::Expr::CallExpr(_) => {
                 let edition = ctx.sema.scope(variable.syntax())?.krate().edition(ctx.db());
-                match konst.render_eval(ctx.sema.db, edition) {
-                    Ok(result) => result,
-                    Err(_) => return None,
-                }
+                konst.eval(ctx.sema.db, edition).ok()?
             }
             _ => return None,
         };
@@ -127,12 +124,14 @@ mod tests {
         ("u64", "0", NUMBER),
         ("u128", "0", NUMBER),
         ("usize", "0", NUMBER),
+        ("usize", "16", NUMBER),
         ("i8", "0", NUMBER),
         ("i16", "0", NUMBER),
         ("i32", "0", NUMBER),
         ("i64", "0", NUMBER),
         ("i128", "0", NUMBER),
         ("isize", "0", NUMBER),
+        ("isize", "16", NUMBER),
         ("bool", "false", BOOL),
         ("&str", "\"str\"", STR),
         ("char", "'c'", CHAR),