Source map returns a result

cc #2236
This commit is contained in:
Aleksey Kladov 2020-03-06 14:44:44 +01:00
parent ea0c124219
commit fb5891c433
6 changed files with 26 additions and 18 deletions

View file

@ -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
}

View file

@ -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;

View file

@ -156,6 +156,9 @@ pub struct BodySourceMap {
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, 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<PatId> for Body {
}
impl BodySourceMap {
pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> {
self.expr_map_back.get(expr).copied()
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax)
}
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
@ -238,8 +241,8 @@ impl BodySourceMap {
self.expr_map.get(&src).cloned()
}
pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
self.pat_map_back.get(pat).copied()
pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
self.pat_map_back.get(pat).copied().ok_or(SyntheticSyntax)
}
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {

View file

@ -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 });
}

View file

@ -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) {

View file

@ -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<T> -> (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();