fix: Fix inline_const_as_literal error when the number >= 10

This commit is contained in:
coekjan 2024-09-05 13:50:44 +08:00
parent 1c26c99fcd
commit b41f6ab525
No known key found for this signature in database
GPG Key ID: E2B19491B838BB2B
2 changed files with 14 additions and 4 deletions

View File

@ -2553,6 +2553,17 @@ impl Const {
Type::from_value_def(db, self.id) 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( pub fn render_eval(
self, self,
db: &dyn HirDatabase, db: &dyn HirDatabase,

View File

@ -53,10 +53,7 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>
| ast::Expr::BinExpr(_) | ast::Expr::BinExpr(_)
| ast::Expr::CallExpr(_) => { | ast::Expr::CallExpr(_) => {
let edition = ctx.sema.scope(variable.syntax())?.krate().edition(ctx.db()); let edition = ctx.sema.scope(variable.syntax())?.krate().edition(ctx.db());
match konst.render_eval(ctx.sema.db, edition) { konst.eval(ctx.sema.db, edition).ok()?
Ok(result) => result,
Err(_) => return None,
}
} }
_ => return None, _ => return None,
}; };
@ -127,12 +124,14 @@ mod tests {
("u64", "0", NUMBER), ("u64", "0", NUMBER),
("u128", "0", NUMBER), ("u128", "0", NUMBER),
("usize", "0", NUMBER), ("usize", "0", NUMBER),
("usize", "16", NUMBER),
("i8", "0", NUMBER), ("i8", "0", NUMBER),
("i16", "0", NUMBER), ("i16", "0", NUMBER),
("i32", "0", NUMBER), ("i32", "0", NUMBER),
("i64", "0", NUMBER), ("i64", "0", NUMBER),
("i128", "0", NUMBER), ("i128", "0", NUMBER),
("isize", "0", NUMBER), ("isize", "0", NUMBER),
("isize", "16", NUMBER),
("bool", "false", BOOL), ("bool", "false", BOOL),
("&str", "\"str\"", STR), ("&str", "\"str\"", STR),
("char", "'c'", CHAR), ("char", "'c'", CHAR),