Skip to content

Commit ae8ce99

Browse files
committed
Bring back the hex in const hover
1 parent e6ba791 commit ae8ce99

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

crates/hir-ty/src/display.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::fmt::{self, Debug};
66

77
use base_db::CrateId;
8-
use chalk_ir::BoundVar;
8+
use chalk_ir::{BoundVar, TyKind};
99
use hir_def::{
1010
adt::VariantData,
1111
body,
@@ -36,7 +36,7 @@ use crate::{
3636
AdtId, AliasEq, AliasTy, Binders, CallableDefId, CallableSig, Const, ConstScalar, ConstValue,
3737
DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives,
3838
MemoryMap, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar,
39-
Substitution, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
39+
Substitution, TraitRef, TraitRefExt, Ty, TyExt, WhereClause,
4040
};
4141

4242
pub trait HirWrite: fmt::Write {
@@ -383,6 +383,28 @@ impl HirDisplay for Const {
383383
}
384384
}
385385

386+
pub struct HexifiedConst(pub Const);
387+
388+
impl HirDisplay for HexifiedConst {
389+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
390+
let data = &self.0.data(Interner);
391+
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
392+
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
393+
if let ConstValue::Concrete(c) = &data.value {
394+
if let ConstScalar::Bytes(b, m) = &c.interned {
395+
let value = u128::from_le_bytes(pad16(b, false));
396+
if value >= 10 {
397+
render_const_scalar(f, &b, m, &data.ty)?;
398+
return write!(f, " ({:#X})", value);
399+
}
400+
}
401+
}
402+
}
403+
}
404+
self.0.hir_fmt(f)
405+
}
406+
}
407+
386408
fn render_const_scalar(
387409
f: &mut HirFormatter<'_>,
388410
b: &[u8],

crates/hir/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use hir_ty::{
6060
all_super_traits, autoderef,
6161
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
6262
diagnostics::BodyValidationDiagnostic,
63+
display::HexifiedConst,
6364
layout::layout_of_ty,
6465
method_resolution::{self, TyFingerprint},
6566
mir::interpret_mir,
@@ -1883,8 +1884,18 @@ impl Const {
18831884
Type::new_with_resolver_inner(db, &resolver, ty)
18841885
}
18851886

1886-
pub fn eval(self, db: &dyn HirDatabase) -> Result<hir_ty::Const, ConstEvalError> {
1887-
db.const_eval(self.id)
1887+
pub fn render_eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
1888+
let c = db.const_eval(self.id)?;
1889+
let r = format!("{}", HexifiedConst(c).display(db));
1890+
// We want to see things like `<utf8-error>` and `<layout-error>` as they are probably bug in our
1891+
// implementation, but there is no need to show things like `<enum-not-supported>` or `<ref-not-supported>` to
1892+
// the user.
1893+
if r.contains("not-supported>") {
1894+
return Err(ConstEvalError::MirEvalError(MirEvalError::NotSupported(
1895+
"rendering complex constants".to_string(),
1896+
)));
1897+
}
1898+
return Ok(r);
18881899
}
18891900
}
18901901

crates/ide/src/hover/render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ pub(super) fn definition(
432432
}
433433
}),
434434
Definition::Const(it) => label_value_and_docs(db, it, |it| {
435-
let body = it.eval(db);
435+
let body = it.render_eval(db);
436436
match body {
437-
Ok(x) => Some(format!("{}", x.display(db))),
437+
Ok(x) => Some(x),
438438
Err(_) => {
439439
let source = it.source(db)?;
440440
let mut body = source.value.body()?.syntax().clone();

crates/ide/src/hover/tests.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ fn hover_const_static() {
531531
```
532532
533533
```rust
534-
const foo: u32 = 123
534+
const foo: u32 = 123 (0x7B)
535535
```
536536
"#]],
537537
);
@@ -3770,7 +3770,6 @@ const FOO$0: usize = 1 << 3;
37703770
This is a doc
37713771
"#]],
37723772
);
3773-
// FIXME: show hex for >10
37743773
check(
37753774
r#"
37763775
/// This is a doc
@@ -3784,7 +3783,7 @@ const FOO$0: usize = (1 << 3) + (1 << 2);
37843783
```
37853784
37863785
```rust
3787-
const FOO: usize = 12
3786+
const FOO: usize = 12 (0xC)
37883787
```
37893788
37903789
---
@@ -3828,7 +3827,7 @@ const FOO$0: i32 = 2 - 3;
38283827
```
38293828
38303829
```rust
3831-
const FOO: i32 = -1
3830+
const FOO: i32 = -1 (0xFFFFFFFF)
38323831
```
38333832
38343833
---
@@ -3915,7 +3914,7 @@ const FOO$0: u8 = b'a';
39153914
```
39163915
39173916
```rust
3918-
const FOO: u8 = 97
3917+
const FOO: u8 = 97 (0x61)
39193918
```
39203919
39213920
---
@@ -3937,7 +3936,7 @@ const FOO$0: u8 = b'\x61';
39373936
```
39383937
39393938
```rust
3940-
const FOO: u8 = 97
3939+
const FOO: u8 = 97 (0x61)
39413940
```
39423941
39433942
---
@@ -3989,6 +3988,28 @@ const FOO$0: f32 = 1f32;
39893988
This is a doc
39903989
"#]],
39913990
);
3991+
// Don't show `<ref-not-supported>` in const hover
3992+
check(
3993+
r#"
3994+
/// This is a doc
3995+
const FOO$0: &i32 = &2;
3996+
"#,
3997+
expect![[r#"
3998+
*FOO*
3999+
4000+
```rust
4001+
test
4002+
```
4003+
4004+
```rust
4005+
const FOO: &i32 = &2
4006+
```
4007+
4008+
---
4009+
4010+
This is a doc
4011+
"#]],
4012+
);
39924013
//show f64 typecasted from float
39934014
check(
39944015
r#"

0 commit comments

Comments
 (0)