Skip to content

Commit fb5891c

Browse files
committed
Source map returns a result
cc #2236
1 parent ea0c124 commit fb5891c

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
2121
(db, file_id)
2222
}
2323

24-
fn with_files(fixture: &str) -> Self {
24+
fn with_files(ra_fixture: &str) -> Self {
2525
let mut db = Self::default();
26-
let pos = with_files(&mut db, fixture);
26+
let pos = with_files(&mut db, ra_fixture);
2727
assert!(pos.is_none());
2828
db
2929
}

crates/ra_hir/src/source_analyzer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn scope_for_offset(
261261
.scope_by_expr()
262262
.iter()
263263
.filter_map(|(id, scope)| {
264-
let source = source_map.expr_syntax(*id)?;
264+
let source = source_map.expr_syntax(*id).ok()?;
265265
// FIXME: correctly handle macro expansion
266266
if source.file_id != offset.file_id {
267267
return None;
@@ -337,7 +337,7 @@ fn adjust(
337337
.scope_by_expr()
338338
.iter()
339339
.filter_map(|(id, scope)| {
340-
let source = source_map.expr_syntax(*id)?;
340+
let source = source_map.expr_syntax(*id).ok()?;
341341
// FIXME: correctly handle macro expansion
342342
if source.file_id != file_id {
343343
return None;

crates/ra_hir_def/src/body.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ pub struct BodySourceMap {
156156
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
157157
}
158158

159+
#[derive(Debug)]
160+
pub struct SyntheticSyntax;
161+
159162
impl Body {
160163
pub(crate) fn body_with_source_map_query(
161164
db: &impl DefDatabase,
@@ -219,8 +222,8 @@ impl Index<PatId> for Body {
219222
}
220223

221224
impl BodySourceMap {
222-
pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> {
223-
self.expr_map_back.get(expr).copied()
225+
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
226+
self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax)
224227
}
225228

226229
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
@@ -238,8 +241,8 @@ impl BodySourceMap {
238241
self.expr_map.get(&src).cloned()
239242
}
240243

241-
pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
242-
self.pat_map_back.get(pat).copied()
244+
pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
245+
self.pat_map_back.get(pat).copied().ok_or(SyntheticSyntax)
243246
}
244247

245248
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {

crates/ra_hir_ty/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
100100
}
101101
let (_, source_map) = db.body_with_source_map(self.func.into());
102102

103-
if let Some(source_ptr) = source_map.expr_syntax(id) {
103+
if let Ok(source_ptr) = source_map.expr_syntax(id) {
104104
if let Some(expr) = source_ptr.value.left() {
105105
let root = source_ptr.file_syntax(db);
106106
if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
@@ -145,7 +145,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
145145
if params.len() == 2 && params[0] == mismatch.actual {
146146
let (_, source_map) = db.body_with_source_map(self.func.into());
147147

148-
if let Some(source_ptr) = source_map.expr_syntax(id) {
148+
if let Ok(source_ptr) = source_map.expr_syntax(id) {
149149
if let Some(expr) = source_ptr.value.left() {
150150
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
151151
}

crates/ra_hir_ty/src/tests.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ use std::fmt::Write;
1111
use std::sync::Arc;
1212

1313
use hir_def::{
14-
body::BodySourceMap, child_by_source::ChildBySource, db::DefDatabase, item_scope::ItemScope,
15-
keys, nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
14+
body::{BodySourceMap, SyntheticSyntax},
15+
child_by_source::ChildBySource,
16+
db::DefDatabase,
17+
item_scope::ItemScope,
18+
keys,
19+
nameres::CrateDefMap,
20+
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
1621
};
1722
use hir_expand::InFile;
1823
use insta::assert_snapshot;
@@ -67,20 +72,20 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
6772

6873
for (pat, ty) in inference_result.type_of_pat.iter() {
6974
let syntax_ptr = match body_source_map.pat_syntax(pat) {
70-
Some(sp) => {
75+
Ok(sp) => {
7176
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
7277
}
73-
None => continue,
78+
Err(SyntheticSyntax) => continue,
7479
};
7580
types.push((syntax_ptr, ty));
7681
}
7782

7883
for (expr, ty) in inference_result.type_of_expr.iter() {
7984
let syntax_ptr = match body_source_map.expr_syntax(expr) {
80-
Some(sp) => {
85+
Ok(sp) => {
8186
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
8287
}
83-
None => continue,
88+
Err(SyntheticSyntax) => continue,
8489
};
8590
types.push((syntax_ptr, ty));
8691
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn analysis_stats(
158158
// in super-verbose mode for just one function, we print every single expression
159159
let (_, sm) = db.body_with_source_map(f_id.into());
160160
let src = sm.expr_syntax(expr_id);
161-
if let Some(src) = src {
161+
if let Ok(src) = src {
162162
let original_file = src.file_id.original_file(db);
163163
let line_index = host.analysis().file_line_index(original_file).unwrap();
164164
let text_range = src.value.either(
@@ -186,7 +186,7 @@ pub fn analysis_stats(
186186
if verbosity.is_verbose() {
187187
let (_, sm) = db.body_with_source_map(f_id.into());
188188
let src = sm.expr_syntax(expr_id);
189-
if let Some(src) = src {
189+
if let Ok(src) = src {
190190
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
191191
// But also, we should just turn the type mismatches into diagnostics and provide these
192192
let root = db.parse_or_expand(src.file_id).unwrap();

0 commit comments

Comments
 (0)