From fb5891c4330e577f5d6891be529141695ce36d4f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Mar 2020 14:44:44 +0100 Subject: Source map returns a result cc #2236 --- crates/ra_db/src/fixture.rs | 4 ++-- crates/ra_hir/src/source_analyzer.rs | 4 ++-- crates/ra_hir_def/src/body.rs | 11 +++++++---- crates/ra_hir_ty/src/expr.rs | 4 ++-- crates/ra_hir_ty/src/tests.rs | 17 +++++++++++------ crates/rust-analyzer/src/cli/analysis_stats.rs | 4 ++-- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 17cd138c2..da7af110c 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -21,9 +21,9 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { (db, file_id) } - fn with_files(fixture: &str) -> Self { + fn with_files(ra_fixture: &str) -> Self { let mut db = Self::default(); - let pos = with_files(&mut db, fixture); + let pos = with_files(&mut db, ra_fixture); assert!(pos.is_none()); db } diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index f3f1ed05a..331ecdd9c 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -261,7 +261,7 @@ fn scope_for_offset( .scope_by_expr() .iter() .filter_map(|(id, scope)| { - let source = source_map.expr_syntax(*id)?; + let source = source_map.expr_syntax(*id).ok()?; // FIXME: correctly handle macro expansion if source.file_id != offset.file_id { return None; @@ -337,7 +337,7 @@ fn adjust( .scope_by_expr() .iter() .filter_map(|(id, scope)| { - let source = source_map.expr_syntax(*id)?; + let source = source_map.expr_syntax(*id).ok()?; // FIXME: correctly handle macro expansion if source.file_id != file_id { return None; diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 010d35e55..cd9e22c42 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -156,6 +156,9 @@ pub struct BodySourceMap { expansions: FxHashMap>, HirFileId>, } +#[derive(Debug)] +pub struct SyntheticSyntax; + impl Body { pub(crate) fn body_with_source_map_query( db: &impl DefDatabase, @@ -219,8 +222,8 @@ impl Index for Body { } impl BodySourceMap { - pub fn expr_syntax(&self, expr: ExprId) -> Option { - self.expr_map_back.get(expr).copied() + pub fn expr_syntax(&self, expr: ExprId) -> Result { + self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax) } pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option { @@ -238,8 +241,8 @@ impl BodySourceMap { self.expr_map.get(&src).cloned() } - pub fn pat_syntax(&self, pat: PatId) -> Option { - self.pat_map_back.get(pat).copied() + pub fn pat_syntax(&self, pat: PatId) -> Result { + self.pat_map_back.get(pat).copied().ok_or(SyntheticSyntax) } pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option { diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs index 22f24890d..d8cdf5266 100644 --- a/crates/ra_hir_ty/src/expr.rs +++ b/crates/ra_hir_ty/src/expr.rs @@ -100,7 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } let (_, source_map) = db.body_with_source_map(self.func.into()); - if let Some(source_ptr) = source_map.expr_syntax(id) { + if let Ok(source_ptr) = source_map.expr_syntax(id) { if let Some(expr) = source_ptr.value.left() { let root = source_ptr.file_syntax(db); if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) { @@ -145,7 +145,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { if params.len() == 2 && params[0] == mismatch.actual { let (_, source_map) = db.body_with_source_map(self.func.into()); - if let Some(source_ptr) = source_map.expr_syntax(id) { + if let Ok(source_ptr) = source_map.expr_syntax(id) { if let Some(expr) = source_ptr.value.left() { self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr }); } diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 087edcc92..7e9547340 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -11,8 +11,13 @@ use std::fmt::Write; use std::sync::Arc; use hir_def::{ - body::BodySourceMap, child_by_source::ChildBySource, db::DefDatabase, item_scope::ItemScope, - keys, nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, + body::{BodySourceMap, SyntheticSyntax}, + child_by_source::ChildBySource, + db::DefDatabase, + item_scope::ItemScope, + keys, + nameres::CrateDefMap, + AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, }; use hir_expand::InFile; use insta::assert_snapshot; @@ -67,20 +72,20 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { for (pat, ty) in inference_result.type_of_pat.iter() { let syntax_ptr = match body_source_map.pat_syntax(pat) { - Some(sp) => { + Ok(sp) => { sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr())) } - None => continue, + Err(SyntheticSyntax) => continue, }; types.push((syntax_ptr, ty)); } for (expr, ty) in inference_result.type_of_expr.iter() { let syntax_ptr = match body_source_map.expr_syntax(expr) { - Some(sp) => { + Ok(sp) => { sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr())) } - None => continue, + Err(SyntheticSyntax) => continue, }; types.push((syntax_ptr, ty)); if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) { diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 6bf0be565..643c54a9d 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -158,7 +158,7 @@ pub fn analysis_stats( // in super-verbose mode for just one function, we print every single expression let (_, sm) = db.body_with_source_map(f_id.into()); let src = sm.expr_syntax(expr_id); - if let Some(src) = src { + if let Ok(src) = src { let original_file = src.file_id.original_file(db); let line_index = host.analysis().file_line_index(original_file).unwrap(); let text_range = src.value.either( @@ -186,7 +186,7 @@ pub fn analysis_stats( if verbosity.is_verbose() { let (_, sm) = db.body_with_source_map(f_id.into()); let src = sm.expr_syntax(expr_id); - if let Some(src) = src { + if let Ok(src) = src { // FIXME: it might be nice to have a function (on Analysis?) that goes from Source -> (LineCol, LineCol) directly // But also, we should just turn the type mismatches into diagnostics and provide these let root = db.parse_or_expand(src.file_id).unwrap(); -- cgit v1.2.3