Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide/src/interpret.rs')
| -rw-r--r-- | crates/ide/src/interpret.rs | 35 |
1 files changed, 27 insertions, 8 deletions
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("<unknown file>"); + 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 +} |