Skip to content

Commit

Permalink
Merge pull request #18470 from Veykril/push-rxmtkvpulotn
Browse files Browse the repository at this point in the history
Allow interpreting consts and statics with interpret function command
  • Loading branch information
Veykril authored Nov 3, 2024
2 parents 20ab970 + 78f3112 commit e44691a
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 86 deletions.
1 change: 1 addition & 0 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct FunctionData {
pub name: Name,
pub params: Box<[TypeRefId]>,
pub ret_type: TypeRefId,
// FIXME: why are these stored here? They should be accessed via the query
pub attrs: Attrs,
pub visibility: RawVisibility,
pub abi: Option<Symbol>,
Expand Down
23 changes: 19 additions & 4 deletions crates/hir-ty/src/consteval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ pub enum ConstEvalError {
MirEvalError(MirEvalError),
}

impl ConstEvalError {
pub fn pretty_print(
&self,
f: &mut String,
db: &dyn HirDatabase,
span_formatter: impl Fn(span::FileId, span::TextRange) -> String,
edition: span::Edition,
) -> std::result::Result<(), std::fmt::Error> {
match self {
ConstEvalError::MirLowerError(e) => e.pretty_print(f, db, span_formatter, edition),
ConstEvalError::MirEvalError(e) => e.pretty_print(f, db, span_formatter, edition),
}
}
}

impl From<MirLowerError> for ConstEvalError {
fn from(value: MirLowerError) -> Self {
match value {
Expand Down Expand Up @@ -253,7 +268,7 @@ pub(crate) fn const_eval_query(
}
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,
};
let c = interpret_mir(db, body, false, trait_env).0?;
let c = interpret_mir(db, body, false, trait_env)?.0?;
Ok(c)
}

Expand All @@ -266,7 +281,7 @@ pub(crate) fn const_eval_static_query(
Substitution::empty(Interner),
db.trait_environment_for_body(def.into()),
)?;
let c = interpret_mir(db, body, false, None).0?;
let c = interpret_mir(db, body, false, None)?.0?;
Ok(c)
}

Expand Down Expand Up @@ -298,7 +313,7 @@ pub(crate) fn const_eval_discriminant_variant(
Substitution::empty(Interner),
db.trait_environment_for_body(def),
)?;
let c = interpret_mir(db, mir_body, false, None).0?;
let c = interpret_mir(db, mir_body, false, None)?.0?;
let c = if is_signed {
try_const_isize(db, &c).unwrap()
} else {
Expand Down Expand Up @@ -339,7 +354,7 @@ pub(crate) fn eval_to_const(
}
}
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, ctx.body, &infer, expr) {
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true, None).0 {
if let Ok((Ok(result), _)) = interpret_mir(db, Arc::new(mir_body), true, None) {
return result;
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,9 @@ pub fn interpret_mir(
// (and probably should) do better here, for example by excluding bindings outside of the target expression.
assert_placeholder_ty_is_unused: bool,
trait_env: Option<Arc<TraitEnvironment>>,
) -> (Result<Const>, MirOutput) {
) -> Result<(Result<Const>, MirOutput)> {
let ty = body.locals[return_slot()].ty.clone();
let mut evaluator =
match Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env) {
Ok(it) => it,
Err(e) => return (Err(e), MirOutput { stdout: vec![], stderr: vec![] }),
};
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env)?;
let it: Result<Const> = (|| {
if evaluator.ptr_size() != std::mem::size_of::<usize>() {
not_supported!("targets with different pointer size from host");
Expand All @@ -613,7 +609,7 @@ pub fn interpret_mir(
};
Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty))
})();
(it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr })
Ok((it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr }))
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn eval_main(db: &TestDB, file_id: EditionedFileId) -> Result<(String, String),
)
.map_err(|e| MirEvalError::MirLowerError(func_id, e))?;

let (result, output) = interpret_mir(db, body, false, None);
let (result, output) = interpret_mir(db, body, false, None)?;
result?;
Ok((output.stdout().into_owned(), output.stderr().into_owned()))
}
Expand Down
31 changes: 16 additions & 15 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,22 +2306,15 @@ impl Function {
self,
db: &dyn HirDatabase,
span_formatter: impl Fn(FileId, TextRange) -> String,
) -> String {
) -> Result<String, ConstEvalError> {
let krate = HasModule::krate(&self.id, db.upcast());
let edition = db.crate_graph()[krate].edition;
let body = match db.monomorphized_mir_body(
let body = db.monomorphized_mir_body(
self.id.into(),
Substitution::empty(Interner),
db.trait_environment(self.id.into()),
) {
Ok(body) => body,
Err(e) => {
let mut r = String::new();
_ = e.pretty_print(&mut r, db, &span_formatter, edition);
return r;
}
};
let (result, output) = interpret_mir(db, body, false, None);
)?;
let (result, output) = interpret_mir(db, body, false, None)?;
let mut text = match result {
Ok(_) => "pass".to_owned(),
Err(e) => {
Expand All @@ -2340,7 +2333,7 @@ impl Function {
text += "\n--------- stderr ---------\n";
text += &stderr;
}
text
Ok(text)
}
}

Expand Down Expand Up @@ -2563,9 +2556,9 @@ impl Const {
/// 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, edition: Edition) -> Result<String, ConstEvalError> {
pub fn eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
Ok(format!("{}", c.display(db, edition)))
Ok(format!("{}", c.display(db, self.krate(db).edition(db))))
}

/// Evaluate the constant and return the result as a string, with more detailed information.
Expand Down Expand Up @@ -2640,7 +2633,15 @@ impl Static {
Type::from_value_def(db, self.id)
}

/// Evaluate the constant and return the result as a string, with more detailed information.
/// 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<String, ConstEvalError> {
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(
Expand Down
5 changes: 1 addition & 4 deletions crates/ide-assists/src/handlers/inline_const_as_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>
| ast::Expr::MatchExpr(_)
| ast::Expr::MacroExpr(_)
| ast::Expr::BinExpr(_)
| ast::Expr::CallExpr(_) => {
let edition = ctx.sema.scope(variable.syntax())?.krate().edition(ctx.db());
konst.eval(ctx.sema.db, edition).ok()?
}
| ast::Expr::CallExpr(_) => konst.eval(ctx.sema.db).ok()?,
_ => return None,
};

Expand Down
64 changes: 64 additions & 0 deletions crates/ide/src/interpret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use hir::{DefWithBody, Semantics};
use ide_db::{base_db::SourceRootDatabase, FilePosition, LineIndexDatabase, RootDatabase};
use std::time::{Duration, Instant};
use stdx::format_to;
use syntax::{algo::ancestors_at_offset, ast, AstNode, TextRange};

// Feature: Interpret a function, static or const.
//
// |===
// | Editor | Action Name
//
// | VS Code | **rust-analyzer: Interpret**
// |===
pub(crate) fn interpret(db: &RootDatabase, position: FilePosition) -> String {
match find_and_interpret(db, position) {
Some((duration, mut result)) => {
result.push('\n');
format_to!(result, "----------------------\n");
format_to!(result, " Finished in {}s\n", duration.as_secs_f32());
result
}
_ => "Not inside a function, const or static".to_owned(),
}
}

fn find_and_interpret(db: &RootDatabase, position: FilePosition) -> Option<(Duration, String)> {
let sema = Semantics::new(db);
let source_file = sema.parse_guess_edition(position.file_id);

let item = ancestors_at_offset(source_file.syntax(), position.offset)
.filter(|it| !ast::MacroCall::can_cast(it.kind()))
.find_map(ast::Item::cast)?;
let def: DefWithBody = match item {
ast::Item::Fn(it) => sema.to_def(&it)?.into(),
ast::Item::Const(it) => sema.to_def(&it)?.into(),
ast::Item::Static(it) => sema.to_def(&it)?.into(),
_ => return None,
};
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 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),
_ => 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 duration = Instant::now() - start_time;
Some((duration, res))
}
47 changes: 0 additions & 47 deletions crates/ide/src/interpret_function.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod goto_type_definition;
mod highlight_related;
mod hover;
mod inlay_hints;
mod interpret_function;
mod interpret;
mod join_lines;
mod markdown_remove;
mod matching_brace;
Expand Down Expand Up @@ -350,7 +350,7 @@ impl Analysis {
}

pub fn interpret_function(&self, position: FilePosition) -> Cancellable<String> {
self.with_db(|db| interpret_function::interpret_function(db, position))
self.with_db(|db| interpret::interpret(db, position))
}

pub fn view_item_tree(&self, file_id: FileId) -> Cancellable<String> {
Expand Down
2 changes: 2 additions & 0 deletions crates/intern/src/symbol/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ define_symbols! {
rustc_const_panic_str,
rustc_deprecated_safe_2024,
rustc_has_incoherent_inherent_impls,
rustc_intrinsic,
rustc_intrinsic_must_be_overridden,
rustc_layout_scalar_valid_range_end,
rustc_layout_scalar_valid_range_start,
rustc_legacy_const_generics,
Expand Down
9 changes: 4 additions & 5 deletions crates/rust-analyzer/src/cli/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ impl flags::RunTests {
}
let mut sw_one = StopWatch::start();
let result = test.eval(db, span_formatter);
if result.trim() == "pass" {
pass_count += 1;
} else {
fail_count += 1;
match &result {
Ok(result) if result.trim() == "pass" => pass_count += 1,
_ => fail_count += 1,
}
println!("{result}");
println!("{result:?}");
eprintln!("{:<20} {}", format!("test {}", full_name), sw_one.elapsed());
}
println!("{pass_count} passed, {fail_count} failed, {ignore_count} ignored");
Expand Down
2 changes: 1 addition & 1 deletion editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
},
{
"command": "rust-analyzer.interpretFunction",
"title": "Interpret Function",
"title": "Interpret",
"category": "rust-analyzer (debug command)"
},
{
Expand Down

0 comments on commit e44691a

Please sign in to comment.