Skip to content

Commit 302bf97

Browse files
committed
Don't expose impl details of SyntaxPtr
1 parent 69f0cb6 commit 302bf97

File tree

8 files changed

+55
-14
lines changed

8 files changed

+55
-14
lines changed

crates/ra_hir_def/src/body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub struct BodySourceMap {
210210
expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>,
211211
pat_map: FxHashMap<PatSource, PatId>,
212212
pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>,
213-
field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>,
213+
field_map: FxHashMap<(ExprId, usize), InFile<AstPtr<ast::RecordField>>>,
214214
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
215215
}
216216

@@ -303,7 +303,7 @@ impl BodySourceMap {
303303
self.pat_map.get(&src).cloned()
304304
}
305305

306-
pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> {
306+
pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile<AstPtr<ast::RecordField>> {
307307
self.field_map[&(expr, field)].clone()
308308
}
309309
}

crates/ra_hir_def/src/body/lower.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ impl ExprCollector<'_> {
320320

321321
let res = self.alloc_expr(record_lit, syntax_ptr);
322322
for (i, ptr) in field_ptrs.into_iter().enumerate() {
323-
self.source_map.field_map.insert((res, i), ptr);
323+
let src = self.expander.to_source(ptr);
324+
self.source_map.field_map.insert((res, i), src);
324325
}
325326
res
326327
}

crates/ra_hir_def/src/diagnostics.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ use std::any::Any;
44

55
use hir_expand::diagnostics::Diagnostic;
66
use ra_db::RelativePathBuf;
7-
use ra_syntax::{ast, AstPtr, SyntaxNodePtr};
7+
use ra_syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
88

99
use hir_expand::{HirFileId, InFile};
1010

1111
#[derive(Debug)]
1212
pub struct UnresolvedModule {
1313
pub file: HirFileId,
1414
pub decl: AstPtr<ast::Module>,
15+
pub highlight_range: TextRange,
1516
pub candidate: RelativePathBuf,
1617
}
1718

1819
impl Diagnostic for UnresolvedModule {
1920
fn message(&self) -> String {
2021
"unresolved module".to_string()
2122
}
23+
fn highlight_range(&self) -> TextRange {
24+
self.highlight_range
25+
}
2226
fn source(&self) -> InFile<SyntaxNodePtr> {
2327
InFile { file_id: self.file, value: self.decl.clone().into() }
2428
}

crates/ra_hir_def/src/nameres.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub enum ModuleSource {
297297
mod diagnostics {
298298
use hir_expand::diagnostics::DiagnosticSink;
299299
use ra_db::RelativePathBuf;
300-
use ra_syntax::{ast, AstPtr};
300+
use ra_syntax::{ast, AstNode, AstPtr};
301301

302302
use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
303303

@@ -326,6 +326,7 @@ mod diagnostics {
326326
sink.push(UnresolvedModule {
327327
file: declaration.file_id,
328328
decl: AstPtr::new(&decl),
329+
highlight_range: decl.syntax().text_range(),
329330
candidate: candidate.clone(),
330331
})
331332
}

crates/ra_hir_expand/src/diagnostics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ use crate::{db::AstDatabase, InFile};
2222

2323
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
2424
fn message(&self) -> String;
25+
fn highlight_range(&self) -> TextRange;
2526
fn source(&self) -> InFile<SyntaxNodePtr>;
26-
fn highlight_range(&self) -> TextRange {
27-
self.source().value.range()
28-
}
2927
fn as_any(&self) -> &(dyn Any + Send + 'static);
3028
}
3129

crates/ra_hir_ty/src/diagnostics.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::any::Any;
44

55
use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
6-
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
6+
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr, TextRange};
77
use stdx::format_to;
88

99
pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm};
@@ -13,13 +13,18 @@ pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
1313
pub struct NoSuchField {
1414
pub file: HirFileId,
1515
pub field: AstPtr<ast::RecordField>,
16+
pub highlight_range: TextRange,
1617
}
1718

1819
impl Diagnostic for NoSuchField {
1920
fn message(&self) -> String {
2021
"no such field".to_string()
2122
}
2223

24+
fn highlight_range(&self) -> TextRange {
25+
self.highlight_range
26+
}
27+
2328
fn source(&self) -> InFile<SyntaxNodePtr> {
2429
InFile { file_id: self.file, value: self.field.clone().into() }
2530
}
@@ -33,6 +38,7 @@ impl Diagnostic for NoSuchField {
3338
pub struct MissingFields {
3439
pub file: HirFileId,
3540
pub field_list: AstPtr<ast::RecordFieldList>,
41+
pub highlight_range: TextRange,
3642
pub missed_fields: Vec<Name>,
3743
}
3844

@@ -44,6 +50,10 @@ impl Diagnostic for MissingFields {
4450
}
4551
buf
4652
}
53+
fn highlight_range(&self) -> TextRange {
54+
self.highlight_range
55+
}
56+
4757
fn source(&self) -> InFile<SyntaxNodePtr> {
4858
InFile { file_id: self.file, value: self.field_list.clone().into() }
4959
}
@@ -66,6 +76,7 @@ impl AstDiagnostic for MissingFields {
6676
pub struct MissingPatFields {
6777
pub file: HirFileId,
6878
pub field_list: AstPtr<ast::RecordFieldPatList>,
79+
pub highlight_range: TextRange,
6980
pub missed_fields: Vec<Name>,
7081
}
7182

@@ -77,6 +88,9 @@ impl Diagnostic for MissingPatFields {
7788
}
7889
buf
7990
}
91+
fn highlight_range(&self) -> TextRange {
92+
self.highlight_range
93+
}
8094
fn source(&self) -> InFile<SyntaxNodePtr> {
8195
InFile { file_id: self.file, value: self.field_list.clone().into() }
8296
}
@@ -90,12 +104,16 @@ pub struct MissingMatchArms {
90104
pub file: HirFileId,
91105
pub match_expr: AstPtr<ast::Expr>,
92106
pub arms: AstPtr<ast::MatchArmList>,
107+
pub highlight_range: TextRange,
93108
}
94109

95110
impl Diagnostic for MissingMatchArms {
96111
fn message(&self) -> String {
97112
String::from("Missing match arm")
98113
}
114+
fn highlight_range(&self) -> TextRange {
115+
self.highlight_range
116+
}
99117
fn source(&self) -> InFile<SyntaxNodePtr> {
100118
InFile { file_id: self.file, value: self.match_expr.clone().into() }
101119
}
@@ -108,12 +126,16 @@ impl Diagnostic for MissingMatchArms {
108126
pub struct MissingOkInTailExpr {
109127
pub file: HirFileId,
110128
pub expr: AstPtr<ast::Expr>,
129+
pub highlight_range: TextRange,
111130
}
112131

113132
impl Diagnostic for MissingOkInTailExpr {
114133
fn message(&self) -> String {
115134
"wrap return expression in Ok".to_string()
116135
}
136+
fn highlight_range(&self) -> TextRange {
137+
self.highlight_range
138+
}
117139
fn source(&self) -> InFile<SyntaxNodePtr> {
118140
InFile { file_id: self.file, value: self.expr.clone().into() }
119141
}

crates/ra_hir_ty/src/expr.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44

55
use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId};
66
use hir_expand::diagnostics::DiagnosticSink;
7-
use ra_syntax::{ast, AstPtr};
7+
use ra_syntax::{ast, AstNode, AstPtr};
88
use rustc_hash::FxHashSet;
99

1010
use crate::{
@@ -100,6 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
100100
self.sink.push(MissingFields {
101101
file: source_ptr.file_id,
102102
field_list: AstPtr::new(&field_list),
103+
highlight_range: field_list.syntax().text_range(),
103104
missed_fields,
104105
})
105106
}
@@ -130,6 +131,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
130131
self.sink.push(MissingPatFields {
131132
file: source_ptr.file_id,
132133
field_list: AstPtr::new(&field_list),
134+
highlight_range: field_list.syntax().text_range(),
133135
missed_fields,
134136
})
135137
}
@@ -213,6 +215,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
213215
file: source_ptr.file_id,
214216
match_expr: AstPtr::new(&match_expr),
215217
arms: AstPtr::new(&arms),
218+
highlight_range: match_expr.syntax().text_range(),
216219
})
217220
}
218221
}
@@ -244,8 +247,13 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
244247
let (_, source_map) = db.body_with_source_map(self.func.into());
245248

246249
if let Ok(source_ptr) = source_map.expr_syntax(id) {
247-
self.sink
248-
.push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value });
250+
let root = source_ptr.file_syntax(db.upcast());
251+
let highlight_range = source_ptr.value.to_node(&root).syntax().text_range();
252+
self.sink.push(MissingOkInTailExpr {
253+
file: source_ptr.file_id,
254+
expr: source_ptr.value,
255+
highlight_range,
256+
});
249257
}
250258
}
251259
}

crates/ra_hir_ty/src/infer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ impl Expectation {
665665
mod diagnostics {
666666
use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup};
667667
use hir_expand::diagnostics::DiagnosticSink;
668+
use ra_syntax::AstNode;
668669

669670
use crate::{db::HirDatabase, diagnostics::NoSuchField};
670671

@@ -682,10 +683,16 @@ mod diagnostics {
682683
) {
683684
match self {
684685
InferenceDiagnostic::NoSuchField { expr, field } => {
685-
let file = owner.lookup(db.upcast()).source(db.upcast()).file_id;
686+
let source = owner.lookup(db.upcast()).source(db.upcast());
686687
let (_, source_map) = db.body_with_source_map(owner.into());
687688
let field = source_map.field_syntax(*expr, *field);
688-
sink.push(NoSuchField { file, field })
689+
let root = field.file_syntax(db.upcast());
690+
let highlight_range = field.value.to_node(&root).syntax().text_range();
691+
sink.push(NoSuchField {
692+
file: source.file_id,
693+
field: field.value,
694+
highlight_range,
695+
})
689696
}
690697
}
691698
}

0 commit comments

Comments
 (0)