mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Allow static initializers to be const evaluated
This commit is contained in:
parent
d1fbfc676b
commit
94c35f6138
7 changed files with 87 additions and 28 deletions
|
@ -2600,7 +2600,7 @@ impl Const {
|
|||
}
|
||||
}
|
||||
}
|
||||
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id, &c) {
|
||||
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
|
||||
Ok(s)
|
||||
} else {
|
||||
Ok(format!("{}", c.display(db, edition)))
|
||||
|
@ -2639,6 +2639,45 @@ impl Static {
|
|||
pub fn ty(self, db: &dyn HirDatabase) -> Type {
|
||||
Type::from_value_def(db, self.id)
|
||||
}
|
||||
|
||||
/// 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,
|
||||
edition: Edition,
|
||||
) -> Result<String, ConstEvalError> {
|
||||
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
|
||||
let data = &c.data(Interner);
|
||||
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
|
||||
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
|
||||
if let hir_ty::ConstValue::Concrete(c) = &data.value {
|
||||
if let hir_ty::ConstScalar::Bytes(b, _) = &c.interned {
|
||||
let value = u128::from_le_bytes(mir::pad16(b, false));
|
||||
let value_signed =
|
||||
i128::from_le_bytes(mir::pad16(b, matches!(s, Scalar::Int(_))));
|
||||
let mut result = if let Scalar::Int(_) = s {
|
||||
value_signed.to_string()
|
||||
} else {
|
||||
value.to_string()
|
||||
};
|
||||
if value >= 10 {
|
||||
format_to!(result, " ({value:#X})");
|
||||
return Ok(result);
|
||||
} else {
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
|
||||
Ok(s)
|
||||
} else {
|
||||
Ok(format!("{}", c.display(db, edition)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasVisibility for Static {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue