From 15239f612da4ed1b8d52330a6ffd679759afd63f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 15 Dec 2024 16:35:17 +0100 Subject: [PATCH] internal: Show mir eval errors on hover with debug env var set --- crates/hir/src/lib.rs | 100 +++++------------- .../src/handlers/inline_const_as_literal.rs | 6 +- crates/ide/src/hover/render.rs | 65 +++++++++--- crates/ide/src/interpret.rs | 35 ++++-- .../rust-analyzer/src/cli/analysis_stats.rs | 4 +- 5 files changed, 113 insertions(+), 97 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 83d72dfcf1..3bc2eee1e7 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -2649,24 +2649,31 @@ impl Const { Type::from_value_def(db, self.id) } - /// Evaluate the constant and return the result as a string. - /// - /// This function is intended for IDE assistance, different from [`Const::render_eval`]. - pub fn eval(self, db: &dyn HirDatabase) -> Result { - let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?; - Ok(format!("{}", c.display(db, self.krate(db).edition(db)))) + /// Evaluate the constant. + pub fn eval(self, db: &dyn HirDatabase) -> Result { + db.const_eval(self.id.into(), Substitution::empty(Interner), None) + .map(|it| EvaluatedConst { const_: it, def: self.id.into() }) + } +} + +impl HasVisibility for Const { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { + db.const_visibility(self.id) + } +} + +pub struct EvaluatedConst { + def: DefWithBodyId, + const_: hir_ty::Const, +} + +impl EvaluatedConst { + pub fn render(&self, db: &dyn HirDatabase, edition: Edition) -> String { + format!("{}", self.const_.display(db, edition)) } - /// 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 { - let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?; - let data = &c.data(Interner); + pub fn render_debug(&self, db: &dyn HirDatabase) -> Result { + let data = self.const_.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 { @@ -2689,17 +2696,7 @@ impl Const { } } } - 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 Const { - fn visibility(&self, db: &dyn HirDatabase) -> Visibility { - db.const_visibility(self.id) + mir::render_const_using_debug_impl(db, self.def, &self.const_) } } @@ -2729,51 +2726,10 @@ impl Static { Type::from_value_def(db, self.id) } - /// Evaluate the static and return the result as a string. - /// - /// This function is intended for IDE assistance, different from [`Static::render_eval`]. - pub fn eval(self, db: &dyn HirDatabase) -> Result { - let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?; - Ok(format!("{}", c.display(db, self.krate(db).edition(db)))) - } - - /// Evaluate the static 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 { - 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))) - } + /// Evaluate the static initializer. + pub fn eval(self, db: &dyn HirDatabase) -> Result { + db.const_eval(self.id.into(), Substitution::empty(Interner), None) + .map(|it| EvaluatedConst { const_: it, def: self.id.into() }) } } diff --git a/crates/ide-assists/src/handlers/inline_const_as_literal.rs b/crates/ide-assists/src/handlers/inline_const_as_literal.rs index 2bd4c4da1e..c92c22378f 100644 --- a/crates/ide-assists/src/handlers/inline_const_as_literal.rs +++ b/crates/ide-assists/src/handlers/inline_const_as_literal.rs @@ -1,3 +1,4 @@ +use hir::HasCrate; use syntax::{ast, AstNode}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -51,7 +52,10 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_> | ast::Expr::MatchExpr(_) | ast::Expr::MacroExpr(_) | ast::Expr::BinExpr(_) - | ast::Expr::CallExpr(_) => konst.eval(ctx.sema.db).ok()?, + | ast::Expr::CallExpr(_) => konst + .eval(ctx.sema.db) + .ok()? + .render(ctx.sema.db, konst.krate(ctx.sema.db).edition(ctx.sema.db)), _ => return None, }; diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 5a00d63569..e617d462ec 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -1,5 +1,5 @@ //! Logic for rendering the different hover messages -use std::{mem, ops::Not}; +use std::{env, mem, ops::Not}; use either::Either; use hir::{ @@ -28,6 +28,7 @@ use syntax::{algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxToken, T} use crate::{ doc_links::{remove_links, rewrite_links}, hover::{notable_traits, walk_and_push_ty}, + interpret::render_const_eval_error, HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig, MemoryLayoutHoverRenderKind, }; @@ -464,41 +465,77 @@ pub(super) fn definition( Ok(it) => { Some(if it >= 10 { format!("{it} ({it:#X})") } else { format!("{it}") }) } - Err(_) => it.value(db).map(|it| format!("{it:?}")), + Err(err) => { + let res = it.value(db).map(|it| format!("{it:?}")); + if env::var_os("RA_DEV").is_some() { + let res = res.as_deref().unwrap_or(""); + Some(format!("{res} ({})", render_const_eval_error(db, err, edition))) + } else { + res + } + } } } else { None } } Definition::Const(it) => { - let body = it.render_eval(db, edition); - match body { - Ok(it) => Some(it), - Err(_) => { + let body = it.eval(db); + Some(match body { + Ok(it) => match it.render_debug(db) { + Ok(it) => it, + Err(err) => { + let it = it.render(db, edition); + if env::var_os("RA_DEV").is_some() { + format!("{it}\n{}", render_const_eval_error(db, err.into(), edition)) + } else { + it + } + } + }, + Err(err) => { let source = it.source(db)?; let mut body = source.value.body()?.syntax().clone(); if let Some(macro_file) = source.file_id.macro_file() { let span_map = db.expansion_span_map(macro_file); body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into()); } - Some(body.to_string()) + if env::var_os("RA_DEV").is_some() { + format!("{body}\n{}", render_const_eval_error(db, err, edition)) + } else { + body.to_string() + } } - } + }) } Definition::Static(it) => { - let body = it.render_eval(db, edition); - match body { - Ok(it) => Some(it), - Err(_) => { + let body = it.eval(db); + Some(match body { + Ok(it) => match it.render_debug(db) { + Ok(it) => it, + Err(err) => { + let it = it.render(db, edition); + if env::var_os("RA_DEV").is_some() { + format!("{it}\n{}", render_const_eval_error(db, err.into(), edition)) + } else { + it + } + } + }, + Err(err) => { let source = it.source(db)?; let mut body = source.value.body()?.syntax().clone(); if let Some(macro_file) = source.file_id.macro_file() { let span_map = db.expansion_span_map(macro_file); body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into()); } - Some(body.to_string()) + if env::var_os("RA_DEV").is_some() { + format!("{body}\n{}", render_const_eval_error(db, err, edition)) + } else { + body.to_string() + } } - } + }) } _ => None, }; diff --git a/crates/ide/src/interpret.rs b/crates/ide/src/interpret.rs index 5fa6f4e484..e0fdc3dd6f 100644 --- a/crates/ide/src/interpret.rs +++ b/crates/ide/src/interpret.rs @@ -1,5 +1,6 @@ -use hir::{DefWithBody, Semantics}; +use hir::{ConstEvalError, DefWithBody, Semantics}; use ide_db::{base_db::SourceRootDatabase, FilePosition, LineIndexDatabase, RootDatabase}; +use span::Edition; use std::time::{Duration, Instant}; use stdx::format_to; use syntax::{algo::ancestors_at_offset, ast, AstNode, TextRange}; @@ -47,18 +48,36 @@ fn find_and_interpret(db: &RootDatabase, position: FilePosition) -> Option<(Dura None => format!("file://{path} range {text_range:?}"), } }; + let edition = def.module(db).krate().edition(db); let start_time = Instant::now(); let res = match def { DefWithBody::Function(it) => it.eval(db, span_formatter), - DefWithBody::Static(it) => it.eval(db), - DefWithBody::Const(it) => it.eval(db), + DefWithBody::Static(it) => it.eval(db).map(|it| it.render(db, edition)), + DefWithBody::Const(it) => it.eval(db).map(|it| it.render(db, edition)), _ => unreachable!(), }; - let res = res.unwrap_or_else(|e| { - let mut r = String::new(); - _ = e.pretty_print(&mut r, db, span_formatter, def.module(db).krate().edition(db)); - r - }); + let res = res.unwrap_or_else(|e| render_const_eval_error(db, e, edition)); let duration = Instant::now() - start_time; Some((duration, res)) } + +pub(crate) fn render_const_eval_error( + db: &RootDatabase, + e: ConstEvalError, + edition: Edition, +) -> String { + let span_formatter = |file_id, text_range: TextRange| { + let path = &db + .source_root(db.file_source_root(file_id)) + .path_for_file(&file_id) + .map(|x| x.to_string()); + let path = path.as_deref().unwrap_or(""); + match db.line_index(file_id).try_line_col(text_range.start()) { + Some(line_col) => format!("file://{path}:{}:{}", line_col.line + 1, line_col.col), + None => format!("file://{path} range {text_range:?}"), + } + }; + let mut r = String::new(); + _ = e.pretty_print(&mut r, db, span_formatter, edition); + r +} diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index e3ea441f3a..66cd2e424e 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -331,8 +331,8 @@ impl flags::AnalysisStats { let mut fail = 0; for &b in bodies { let res = match b { - DefWithBody::Const(c) => c.render_eval(db, Edition::LATEST), - DefWithBody::Static(s) => s.render_eval(db, Edition::LATEST), + DefWithBody::Const(c) => c.eval(db), + DefWithBody::Static(s) => s.eval(db), _ => continue, }; all += 1;