aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src')
-rw-r--r--crates/ra_hir_ty/src/diagnostics.rs38
1 files changed, 21 insertions, 17 deletions
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs
index a5b00ed48..73d241434 100644
--- a/crates/ra_hir_ty/src/diagnostics.rs
+++ b/crates/ra_hir_ty/src/diagnostics.rs
@@ -9,7 +9,7 @@ use hir_def::DefWithBodyId;
9use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; 9use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
10use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; 10use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
11use ra_prof::profile; 11use ra_prof::profile;
12use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr}; 12use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
13use stdx::format_to; 13use stdx::format_to;
14 14
15use crate::db::HirDatabase; 15use crate::db::HirDatabase;
@@ -64,16 +64,6 @@ pub struct MissingFields {
64 pub list_parent_path: Option<AstPtr<ast::Path>>, 64 pub list_parent_path: Option<AstPtr<ast::Path>>,
65} 65}
66 66
67impl MissingFields {
68 fn root(&self, db: &dyn AstDatabase) -> SyntaxNode {
69 db.parse_or_expand(self.file).unwrap()
70 }
71
72 pub fn list_parent_ast(&self, db: &dyn AstDatabase) -> Option<ast::Path> {
73 self.list_parent_path.as_ref().map(|path| path.to_node(&self.root(db)))
74 }
75}
76
77impl Diagnostic for MissingFields { 67impl Diagnostic for MissingFields {
78 fn message(&self) -> String { 68 fn message(&self) -> String {
79 let mut buf = String::from("Missing structure fields:\n"); 69 let mut buf = String::from("Missing structure fields:\n");
@@ -85,16 +75,25 @@ impl Diagnostic for MissingFields {
85 fn source(&self) -> InFile<SyntaxNodePtr> { 75 fn source(&self) -> InFile<SyntaxNodePtr> {
86 InFile { file_id: self.file, value: self.field_list.clone().into() } 76 InFile { file_id: self.file, value: self.field_list.clone().into() }
87 } 77 }
78
88 fn as_any(&self) -> &(dyn Any + Send + 'static) { 79 fn as_any(&self) -> &(dyn Any + Send + 'static) {
89 self 80 self
90 } 81 }
82
83 fn highlighting_source(&self) -> InFile<SyntaxNodePtr> {
84 self.list_parent_path
85 .clone()
86 .map(|path| InFile { file_id: self.file, value: path.into() })
87 .unwrap_or_else(|| self.source())
88 }
91} 89}
92 90
93impl AstDiagnostic for MissingFields { 91impl AstDiagnostic for MissingFields {
94 type AST = ast::RecordExprFieldList; 92 type AST = ast::RecordExprFieldList;
95 93
96 fn ast(&self, db: &dyn AstDatabase) -> Self::AST { 94 fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
97 self.field_list.to_node(&self.root(db)) 95 let root = db.parse_or_expand(self.file).unwrap();
96 self.field_list.to_node(&root)
98 } 97 }
99} 98}
100 99
@@ -260,7 +259,10 @@ impl AstDiagnostic for MismatchedArgCount {
260#[cfg(test)] 259#[cfg(test)]
261mod tests { 260mod tests {
262 use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId}; 261 use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId};
263 use hir_expand::diagnostics::{Diagnostic, DiagnosticSinkBuilder}; 262 use hir_expand::{
263 db::AstDatabase,
264 diagnostics::{Diagnostic, DiagnosticSinkBuilder},
265 };
264 use ra_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; 266 use ra_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt};
265 use ra_syntax::{TextRange, TextSize}; 267 use ra_syntax::{TextRange, TextSize};
266 use rustc_hash::FxHashMap; 268 use rustc_hash::FxHashMap;
@@ -307,7 +309,9 @@ mod tests {
307 db.diagnostics(|d| { 309 db.diagnostics(|d| {
308 // FXIME: macros... 310 // FXIME: macros...
309 let file_id = d.source().file_id.original_file(&db); 311 let file_id = d.source().file_id.original_file(&db);
310 let range = d.syntax_node(&db).text_range(); 312 let highlighting_source = d.highlighting_source();
313 let node = db.parse_or_expand(highlighting_source.file_id).unwrap();
314 let range = highlighting_source.value.to_node(&node).text_range();
311 let message = d.message().to_owned(); 315 let message = d.message().to_owned();
312 actual.entry(file_id).or_default().push((range, message)); 316 actual.entry(file_id).or_default().push((range, message));
313 }); 317 });
@@ -345,7 +349,7 @@ struct Beefy {
345} 349}
346fn baz() { 350fn baz() {
347 let zz = Beefy { 351 let zz = Beefy {
348 //^^^^^... Missing structure fields: 352 //^^^^^ Missing structure fields:
349 // | - seven 353 // | - seven
350 one: (), 354 one: (),
351 two: (), 355 two: (),
@@ -370,8 +374,8 @@ struct S { foo: i32, bar: () }
370impl S { 374impl S {
371 fn new() -> S { 375 fn new() -> S {
372 S { 376 S {
373 //^... Missing structure fields: 377 //^ Missing structure fields:
374 //| - bar 378 //| - bar
375 foo: 92, 379 foo: 92,
376 baz: 62, 380 baz: 62,
377 //^^^^^^^ no such field 381 //^^^^^^^ no such field