Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::fmt::{self, Debug};

use base_db::CrateId;
use chalk_ir::BoundVar;
use chalk_ir::{BoundVar, TyKind};
use hir_def::{
adt::VariantData,
body,
Expand Down Expand Up @@ -36,7 +36,7 @@ use crate::{
AdtId, AliasEq, AliasTy, Binders, CallableDefId, CallableSig, Const, ConstScalar, ConstValue,
DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives,
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 {
Expand Down Expand Up @@ -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(
f: &mut HirFormatter<'_>,
b: &[u8],
Expand Down
15 changes: 13 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use hir_ty::{
all_super_traits, autoderef,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic,
display::HexifiedConst,
layout::layout_of_ty,
method_resolution::{self, TyFingerprint},
mir::interpret_mir,
Expand Down Expand Up @@ -1883,8 +1884,18 @@ impl Const {
Type::new_with_resolver_inner(db, &resolver, ty)
}

pub fn eval(self, db: &dyn HirDatabase) -> Result<hir_ty::Const, ConstEvalError> {
db.const_eval(self.id)
pub fn render_eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ pub(super) fn definition(
}
}),
Definition::Const(it) => label_value_and_docs(db, it, |it| {
let body = it.eval(db);
let body = it.render_eval(db);
match body {
Ok(x) => Some(format!("{}", x.display(db))),
Ok(x) => Some(x),
Err(_) => {
let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone();
Expand Down
33 changes: 27 additions & 6 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ fn hover_const_static() {
```

```rust
const foo: u32 = 123
const foo: u32 = 123 (0x7B)
```
"#]],
);
Expand Down Expand Up @@ -3770,7 +3770,6 @@ const FOO$0: usize = 1 << 3;
This is a doc
"#]],
);
// FIXME: show hex for >10
check(
r#"
/// This is a doc
Expand All @@ -3784,7 +3783,7 @@ const FOO$0: usize = (1 << 3) + (1 << 2);
```

```rust
const FOO: usize = 12
const FOO: usize = 12 (0xC)
```

---
Expand Down Expand Up @@ -3828,7 +3827,7 @@ const FOO$0: i32 = 2 - 3;
```

```rust
const FOO: i32 = -1
const FOO: i32 = -1 (0xFFFFFFFF)
```

---
Expand Down Expand Up @@ -3915,7 +3914,7 @@ const FOO$0: u8 = b'a';
```

```rust
const FOO: u8 = 97
const FOO: u8 = 97 (0x61)
```

---
Expand All @@ -3937,7 +3936,7 @@ const FOO$0: u8 = b'\x61';
```

```rust
const FOO: u8 = 97
const FOO: u8 = 97 (0x61)
```

---
Expand Down Expand Up @@ -3989,6 +3988,28 @@ const FOO$0: f32 = 1f32;
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
check(
r#"
Expand Down