Auto merge of #14255 - HKalbasi:const-hex, r=HKalbasi

Bring back the hex in const hover
This commit is contained in:
bors 2023-03-05 16:27:48 +00:00
commit 0a956ec932
4 changed files with 66 additions and 12 deletions

View File

@ -5,7 +5,7 @@
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
use base_db::CrateId; use base_db::CrateId;
use chalk_ir::BoundVar; use chalk_ir::{BoundVar, TyKind};
use hir_def::{ use hir_def::{
adt::VariantData, adt::VariantData,
body, body,
@ -36,7 +36,7 @@ use crate::{
AdtId, AliasEq, AliasTy, Binders, CallableDefId, CallableSig, Const, ConstScalar, ConstValue, AdtId, AliasEq, AliasTy, Binders, CallableDefId, CallableSig, Const, ConstScalar, ConstValue,
DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives,
MemoryMap, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar, MemoryMap, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar,
Substitution, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, Substitution, TraitRef, TraitRefExt, Ty, TyExt, WhereClause,
}; };
pub trait HirWrite: fmt::Write { pub trait HirWrite: fmt::Write {
@ -383,6 +383,28 @@ impl HirDisplay for Const {
} }
} }
pub struct HexifiedConst(pub Const);
impl HirDisplay for HexifiedConst {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let data = &self.0.data(Interner);
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
if let ConstValue::Concrete(c) = &data.value {
if let ConstScalar::Bytes(b, m) = &c.interned {
let value = u128::from_le_bytes(pad16(b, false));
if value >= 10 {
render_const_scalar(f, &b, m, &data.ty)?;
return write!(f, " ({:#X})", value);
}
}
}
}
}
self.0.hir_fmt(f)
}
}
fn render_const_scalar( fn render_const_scalar(
f: &mut HirFormatter<'_>, f: &mut HirFormatter<'_>,
b: &[u8], b: &[u8],

View File

@ -60,6 +60,7 @@ use hir_ty::{
all_super_traits, autoderef, all_super_traits, autoderef,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt}, consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic, diagnostics::BodyValidationDiagnostic,
display::HexifiedConst,
layout::layout_of_ty, layout::layout_of_ty,
method_resolution::{self, TyFingerprint}, method_resolution::{self, TyFingerprint},
mir::interpret_mir, mir::interpret_mir,
@ -1883,8 +1884,18 @@ impl Const {
Type::new_with_resolver_inner(db, &resolver, ty) Type::new_with_resolver_inner(db, &resolver, ty)
} }
pub fn eval(self, db: &dyn HirDatabase) -> Result<hir_ty::Const, ConstEvalError> { pub fn render_eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
db.const_eval(self.id) let c = db.const_eval(self.id)?;
let r = format!("{}", HexifiedConst(c).display(db));
// We want to see things like `<utf8-error>` and `<layout-error>` as they are probably bug in our
// implementation, but there is no need to show things like `<enum-not-supported>` or `<ref-not-supported>` to
// the user.
if r.contains("not-supported>") {
return Err(ConstEvalError::MirEvalError(MirEvalError::NotSupported(
"rendering complex constants".to_string(),
)));
}
return Ok(r);
} }
} }

View File

@ -432,9 +432,9 @@ pub(super) fn definition(
} }
}), }),
Definition::Const(it) => label_value_and_docs(db, it, |it| { Definition::Const(it) => label_value_and_docs(db, it, |it| {
let body = it.eval(db); let body = it.render_eval(db);
match body { match body {
Ok(x) => Some(format!("{}", x.display(db))), Ok(x) => Some(x),
Err(_) => { Err(_) => {
let source = it.source(db)?; let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone(); let mut body = source.value.body()?.syntax().clone();

View File

@ -531,7 +531,7 @@ fn hover_const_static() {
``` ```
```rust ```rust
const foo: u32 = 123 const foo: u32 = 123 (0x7B)
``` ```
"#]], "#]],
); );
@ -3770,7 +3770,6 @@ const FOO$0: usize = 1 << 3;
This is a doc This is a doc
"#]], "#]],
); );
// FIXME: show hex for >10
check( check(
r#" r#"
/// This is a doc /// This is a doc
@ -3784,7 +3783,7 @@ const FOO$0: usize = (1 << 3) + (1 << 2);
``` ```
```rust ```rust
const FOO: usize = 12 const FOO: usize = 12 (0xC)
``` ```
--- ---
@ -3828,7 +3827,7 @@ const FOO$0: i32 = 2 - 3;
``` ```
```rust ```rust
const FOO: i32 = -1 const FOO: i32 = -1 (0xFFFFFFFF)
``` ```
--- ---
@ -3915,7 +3914,7 @@ const FOO$0: u8 = b'a';
``` ```
```rust ```rust
const FOO: u8 = 97 const FOO: u8 = 97 (0x61)
``` ```
--- ---
@ -3937,7 +3936,7 @@ const FOO$0: u8 = b'\x61';
``` ```
```rust ```rust
const FOO: u8 = 97 const FOO: u8 = 97 (0x61)
``` ```
--- ---
@ -3989,6 +3988,28 @@ const FOO$0: f32 = 1f32;
This is a doc This is a doc
"#]], "#]],
); );
// Don't show `<ref-not-supported>` in const hover
check(
r#"
/// This is a doc
const FOO$0: &i32 = &2;
"#,
expect![[r#"
*FOO*
```rust
test
```
```rust
const FOO: &i32 = &2
```
---
This is a doc
"#]],
);
//show f64 typecasted from float //show f64 typecasted from float
check( check(
r#" r#"