Skip to content

Commit d501b3c

Browse files
authored
Merge pull request #18694 from Veykril/push-uxpuruvqpwmx
internal: Show mir eval errors on hover with debug env var set
2 parents 97de6dc + 9e22cbf commit d501b3c

File tree

5 files changed

+113
-97
lines changed

5 files changed

+113
-97
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

+28-72
Original file line numberDiff line numberDiff line change
@@ -2649,24 +2649,31 @@ impl Const {
26492649
Type::from_value_def(db, self.id)
26502650
}
26512651

2652-
/// Evaluate the constant and return the result as a string.
2653-
///
2654-
/// This function is intended for IDE assistance, different from [`Const::render_eval`].
2655-
pub fn eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
2656-
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
2657-
Ok(format!("{}", c.display(db, self.krate(db).edition(db))))
2652+
/// Evaluate the constant.
2653+
pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> {
2654+
db.const_eval(self.id.into(), Substitution::empty(Interner), None)
2655+
.map(|it| EvaluatedConst { const_: it, def: self.id.into() })
26582656
}
2657+
}
26592658

2660-
/// Evaluate the constant and return the result as a string, with more detailed information.
2661-
///
2662-
/// This function is intended for user-facing display.
2663-
pub fn render_eval(
2664-
self,
2665-
db: &dyn HirDatabase,
2666-
edition: Edition,
2667-
) -> Result<String, ConstEvalError> {
2668-
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
2669-
let data = &c.data(Interner);
2659+
impl HasVisibility for Const {
2660+
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2661+
db.const_visibility(self.id)
2662+
}
2663+
}
2664+
2665+
pub struct EvaluatedConst {
2666+
def: DefWithBodyId,
2667+
const_: hir_ty::Const,
2668+
}
2669+
2670+
impl EvaluatedConst {
2671+
pub fn render(&self, db: &dyn HirDatabase, edition: Edition) -> String {
2672+
format!("{}", self.const_.display(db, edition))
2673+
}
2674+
2675+
pub fn render_debug(&self, db: &dyn HirDatabase) -> Result<String, MirEvalError> {
2676+
let data = self.const_.data(Interner);
26702677
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
26712678
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
26722679
if let hir_ty::ConstValue::Concrete(c) = &data.value {
@@ -2689,17 +2696,7 @@ impl Const {
26892696
}
26902697
}
26912698
}
2692-
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
2693-
Ok(s)
2694-
} else {
2695-
Ok(format!("{}", c.display(db, edition)))
2696-
}
2697-
}
2698-
}
2699-
2700-
impl HasVisibility for Const {
2701-
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2702-
db.const_visibility(self.id)
2699+
mir::render_const_using_debug_impl(db, self.def, &self.const_)
27032700
}
27042701
}
27052702

@@ -2729,51 +2726,10 @@ impl Static {
27292726
Type::from_value_def(db, self.id)
27302727
}
27312728

2732-
/// Evaluate the static and return the result as a string.
2733-
///
2734-
/// This function is intended for IDE assistance, different from [`Static::render_eval`].
2735-
pub fn eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
2736-
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
2737-
Ok(format!("{}", c.display(db, self.krate(db).edition(db))))
2738-
}
2739-
2740-
/// Evaluate the static and return the result as a string, with more detailed information.
2741-
///
2742-
/// This function is intended for user-facing display.
2743-
pub fn render_eval(
2744-
self,
2745-
db: &dyn HirDatabase,
2746-
edition: Edition,
2747-
) -> Result<String, ConstEvalError> {
2748-
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
2749-
let data = &c.data(Interner);
2750-
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
2751-
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
2752-
if let hir_ty::ConstValue::Concrete(c) = &data.value {
2753-
if let hir_ty::ConstScalar::Bytes(b, _) = &c.interned {
2754-
let value = u128::from_le_bytes(mir::pad16(b, false));
2755-
let value_signed =
2756-
i128::from_le_bytes(mir::pad16(b, matches!(s, Scalar::Int(_))));
2757-
let mut result = if let Scalar::Int(_) = s {
2758-
value_signed.to_string()
2759-
} else {
2760-
value.to_string()
2761-
};
2762-
if value >= 10 {
2763-
format_to!(result, " ({value:#X})");
2764-
return Ok(result);
2765-
} else {
2766-
return Ok(result);
2767-
}
2768-
}
2769-
}
2770-
}
2771-
}
2772-
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
2773-
Ok(s)
2774-
} else {
2775-
Ok(format!("{}", c.display(db, edition)))
2776-
}
2729+
/// Evaluate the static initializer.
2730+
pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> {
2731+
db.const_eval(self.id.into(), Substitution::empty(Interner), None)
2732+
.map(|it| EvaluatedConst { const_: it, def: self.id.into() })
27772733
}
27782734
}
27792735

src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_const_as_literal.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir::HasCrate;
12
use syntax::{ast, AstNode};
23

34
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -51,7 +52,10 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>
5152
| ast::Expr::MatchExpr(_)
5253
| ast::Expr::MacroExpr(_)
5354
| ast::Expr::BinExpr(_)
54-
| ast::Expr::CallExpr(_) => konst.eval(ctx.sema.db).ok()?,
55+
| ast::Expr::CallExpr(_) => konst
56+
.eval(ctx.sema.db)
57+
.ok()?
58+
.render(ctx.sema.db, konst.krate(ctx.sema.db).edition(ctx.sema.db)),
5559
_ => return None,
5660
};
5761

src/tools/rust-analyzer/crates/ide/src/hover/render.rs

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Logic for rendering the different hover messages
2-
use std::{mem, ops::Not};
2+
use std::{env, mem, ops::Not};
33

44
use either::Either;
55
use hir::{
@@ -28,6 +28,7 @@ use syntax::{algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxToken, T}
2828
use crate::{
2929
doc_links::{remove_links, rewrite_links},
3030
hover::{notable_traits, walk_and_push_ty},
31+
interpret::render_const_eval_error,
3132
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
3233
MemoryLayoutHoverRenderKind,
3334
};
@@ -464,41 +465,77 @@ pub(super) fn definition(
464465
Ok(it) => {
465466
Some(if it >= 10 { format!("{it} ({it:#X})") } else { format!("{it}") })
466467
}
467-
Err(_) => it.value(db).map(|it| format!("{it:?}")),
468+
Err(err) => {
469+
let res = it.value(db).map(|it| format!("{it:?}"));
470+
if env::var_os("RA_DEV").is_some() {
471+
let res = res.as_deref().unwrap_or("");
472+
Some(format!("{res} ({})", render_const_eval_error(db, err, edition)))
473+
} else {
474+
res
475+
}
476+
}
468477
}
469478
} else {
470479
None
471480
}
472481
}
473482
Definition::Const(it) => {
474-
let body = it.render_eval(db, edition);
475-
match body {
476-
Ok(it) => Some(it),
477-
Err(_) => {
483+
let body = it.eval(db);
484+
Some(match body {
485+
Ok(it) => match it.render_debug(db) {
486+
Ok(it) => it,
487+
Err(err) => {
488+
let it = it.render(db, edition);
489+
if env::var_os("RA_DEV").is_some() {
490+
format!("{it}\n{}", render_const_eval_error(db, err.into(), edition))
491+
} else {
492+
it
493+
}
494+
}
495+
},
496+
Err(err) => {
478497
let source = it.source(db)?;
479498
let mut body = source.value.body()?.syntax().clone();
480499
if let Some(macro_file) = source.file_id.macro_file() {
481500
let span_map = db.expansion_span_map(macro_file);
482501
body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into());
483502
}
484-
Some(body.to_string())
503+
if env::var_os("RA_DEV").is_some() {
504+
format!("{body}\n{}", render_const_eval_error(db, err, edition))
505+
} else {
506+
body.to_string()
507+
}
485508
}
486-
}
509+
})
487510
}
488511
Definition::Static(it) => {
489-
let body = it.render_eval(db, edition);
490-
match body {
491-
Ok(it) => Some(it),
492-
Err(_) => {
512+
let body = it.eval(db);
513+
Some(match body {
514+
Ok(it) => match it.render_debug(db) {
515+
Ok(it) => it,
516+
Err(err) => {
517+
let it = it.render(db, edition);
518+
if env::var_os("RA_DEV").is_some() {
519+
format!("{it}\n{}", render_const_eval_error(db, err.into(), edition))
520+
} else {
521+
it
522+
}
523+
}
524+
},
525+
Err(err) => {
493526
let source = it.source(db)?;
494527
let mut body = source.value.body()?.syntax().clone();
495528
if let Some(macro_file) = source.file_id.macro_file() {
496529
let span_map = db.expansion_span_map(macro_file);
497530
body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into());
498531
}
499-
Some(body.to_string())
532+
if env::var_os("RA_DEV").is_some() {
533+
format!("{body}\n{}", render_const_eval_error(db, err, edition))
534+
} else {
535+
body.to_string()
536+
}
500537
}
501-
}
538+
})
502539
}
503540
_ => None,
504541
};

src/tools/rust-analyzer/crates/ide/src/interpret.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use hir::{DefWithBody, Semantics};
1+
use hir::{ConstEvalError, DefWithBody, Semantics};
22
use ide_db::{base_db::SourceRootDatabase, FilePosition, LineIndexDatabase, RootDatabase};
3+
use span::Edition;
34
use std::time::{Duration, Instant};
45
use stdx::format_to;
56
use syntax::{algo::ancestors_at_offset, ast, AstNode, TextRange};
@@ -47,18 +48,36 @@ fn find_and_interpret(db: &RootDatabase, position: FilePosition) -> Option<(Dura
4748
None => format!("file://{path} range {text_range:?}"),
4849
}
4950
};
51+
let edition = def.module(db).krate().edition(db);
5052
let start_time = Instant::now();
5153
let res = match def {
5254
DefWithBody::Function(it) => it.eval(db, span_formatter),
53-
DefWithBody::Static(it) => it.eval(db),
54-
DefWithBody::Const(it) => it.eval(db),
55+
DefWithBody::Static(it) => it.eval(db).map(|it| it.render(db, edition)),
56+
DefWithBody::Const(it) => it.eval(db).map(|it| it.render(db, edition)),
5557
_ => unreachable!(),
5658
};
57-
let res = res.unwrap_or_else(|e| {
58-
let mut r = String::new();
59-
_ = e.pretty_print(&mut r, db, span_formatter, def.module(db).krate().edition(db));
60-
r
61-
});
59+
let res = res.unwrap_or_else(|e| render_const_eval_error(db, e, edition));
6260
let duration = Instant::now() - start_time;
6361
Some((duration, res))
6462
}
63+
64+
pub(crate) fn render_const_eval_error(
65+
db: &RootDatabase,
66+
e: ConstEvalError,
67+
edition: Edition,
68+
) -> String {
69+
let span_formatter = |file_id, text_range: TextRange| {
70+
let path = &db
71+
.source_root(db.file_source_root(file_id))
72+
.path_for_file(&file_id)
73+
.map(|x| x.to_string());
74+
let path = path.as_deref().unwrap_or("<unknown file>");
75+
match db.line_index(file_id).try_line_col(text_range.start()) {
76+
Some(line_col) => format!("file://{path}:{}:{}", line_col.line + 1, line_col.col),
77+
None => format!("file://{path} range {text_range:?}"),
78+
}
79+
};
80+
let mut r = String::new();
81+
_ = e.pretty_print(&mut r, db, span_formatter, edition);
82+
r
83+
}

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ impl flags::AnalysisStats {
331331
let mut fail = 0;
332332
for &b in bodies {
333333
let res = match b {
334-
DefWithBody::Const(c) => c.render_eval(db, Edition::LATEST),
335-
DefWithBody::Static(s) => s.render_eval(db, Edition::LATEST),
334+
DefWithBody::Const(c) => c.eval(db),
335+
DefWithBody::Static(s) => s.eval(db),
336336
_ => continue,
337337
};
338338
all += 1;

0 commit comments

Comments
 (0)