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.rs24
-rw-r--r--crates/ra_hir_ty/src/expr.rs14
-rw-r--r--crates/ra_hir_ty/src/infer.rs11
3 files changed, 43 insertions, 6 deletions
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs
index 927896d6f..da85bd082 100644
--- a/crates/ra_hir_ty/src/diagnostics.rs
+++ b/crates/ra_hir_ty/src/diagnostics.rs
@@ -3,7 +3,7 @@
3use std::any::Any; 3use std::any::Any;
4 4
5use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; 5use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
6use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; 6use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr, TextRange};
7use stdx::format_to; 7use stdx::format_to;
8 8
9pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm}; 9pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm};
@@ -13,6 +13,7 @@ pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
13pub struct NoSuchField { 13pub struct NoSuchField {
14 pub file: HirFileId, 14 pub file: HirFileId,
15 pub field: AstPtr<ast::RecordField>, 15 pub field: AstPtr<ast::RecordField>,
16 pub highlight_range: TextRange,
16} 17}
17 18
18impl Diagnostic for NoSuchField { 19impl Diagnostic for NoSuchField {
@@ -20,6 +21,10 @@ impl Diagnostic for NoSuchField {
20 "no such field".to_string() 21 "no such field".to_string()
21 } 22 }
22 23
24 fn highlight_range(&self) -> TextRange {
25 self.highlight_range
26 }
27
23 fn source(&self) -> InFile<SyntaxNodePtr> { 28 fn source(&self) -> InFile<SyntaxNodePtr> {
24 InFile { file_id: self.file, value: self.field.clone().into() } 29 InFile { file_id: self.file, value: self.field.clone().into() }
25 } 30 }
@@ -33,6 +38,7 @@ impl Diagnostic for NoSuchField {
33pub struct MissingFields { 38pub struct MissingFields {
34 pub file: HirFileId, 39 pub file: HirFileId,
35 pub field_list: AstPtr<ast::RecordFieldList>, 40 pub field_list: AstPtr<ast::RecordFieldList>,
41 pub highlight_range: TextRange,
36 pub missed_fields: Vec<Name>, 42 pub missed_fields: Vec<Name>,
37} 43}
38 44
@@ -44,6 +50,10 @@ impl Diagnostic for MissingFields {
44 } 50 }
45 buf 51 buf
46 } 52 }
53 fn highlight_range(&self) -> TextRange {
54 self.highlight_range
55 }
56
47 fn source(&self) -> InFile<SyntaxNodePtr> { 57 fn source(&self) -> InFile<SyntaxNodePtr> {
48 InFile { file_id: self.file, value: self.field_list.clone().into() } 58 InFile { file_id: self.file, value: self.field_list.clone().into() }
49 } 59 }
@@ -66,6 +76,7 @@ impl AstDiagnostic for MissingFields {
66pub struct MissingPatFields { 76pub struct MissingPatFields {
67 pub file: HirFileId, 77 pub file: HirFileId,
68 pub field_list: AstPtr<ast::RecordFieldPatList>, 78 pub field_list: AstPtr<ast::RecordFieldPatList>,
79 pub highlight_range: TextRange,
69 pub missed_fields: Vec<Name>, 80 pub missed_fields: Vec<Name>,
70} 81}
71 82
@@ -77,6 +88,9 @@ impl Diagnostic for MissingPatFields {
77 } 88 }
78 buf 89 buf
79 } 90 }
91 fn highlight_range(&self) -> TextRange {
92 self.highlight_range
93 }
80 fn source(&self) -> InFile<SyntaxNodePtr> { 94 fn source(&self) -> InFile<SyntaxNodePtr> {
81 InFile { file_id: self.file, value: self.field_list.clone().into() } 95 InFile { file_id: self.file, value: self.field_list.clone().into() }
82 } 96 }
@@ -90,12 +104,16 @@ pub struct MissingMatchArms {
90 pub file: HirFileId, 104 pub file: HirFileId,
91 pub match_expr: AstPtr<ast::Expr>, 105 pub match_expr: AstPtr<ast::Expr>,
92 pub arms: AstPtr<ast::MatchArmList>, 106 pub arms: AstPtr<ast::MatchArmList>,
107 pub highlight_range: TextRange,
93} 108}
94 109
95impl Diagnostic for MissingMatchArms { 110impl Diagnostic for MissingMatchArms {
96 fn message(&self) -> String { 111 fn message(&self) -> String {
97 String::from("Missing match arm") 112 String::from("Missing match arm")
98 } 113 }
114 fn highlight_range(&self) -> TextRange {
115 self.highlight_range
116 }
99 fn source(&self) -> InFile<SyntaxNodePtr> { 117 fn source(&self) -> InFile<SyntaxNodePtr> {
100 InFile { file_id: self.file, value: self.match_expr.clone().into() } 118 InFile { file_id: self.file, value: self.match_expr.clone().into() }
101 } 119 }
@@ -108,12 +126,16 @@ impl Diagnostic for MissingMatchArms {
108pub struct MissingOkInTailExpr { 126pub struct MissingOkInTailExpr {
109 pub file: HirFileId, 127 pub file: HirFileId,
110 pub expr: AstPtr<ast::Expr>, 128 pub expr: AstPtr<ast::Expr>,
129 pub highlight_range: TextRange,
111} 130}
112 131
113impl Diagnostic for MissingOkInTailExpr { 132impl Diagnostic for MissingOkInTailExpr {
114 fn message(&self) -> String { 133 fn message(&self) -> String {
115 "wrap return expression in Ok".to_string() 134 "wrap return expression in Ok".to_string()
116 } 135 }
136 fn highlight_range(&self) -> TextRange {
137 self.highlight_range
138 }
117 fn source(&self) -> InFile<SyntaxNodePtr> { 139 fn source(&self) -> InFile<SyntaxNodePtr> {
118 InFile { file_id: self.file, value: self.expr.clone().into() } 140 InFile { file_id: self.file, value: self.expr.clone().into() }
119 } 141 }
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index fd59f4320..1d3950b70 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
4 4
5use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId}; 5use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId};
6use hir_expand::diagnostics::DiagnosticSink; 6use hir_expand::diagnostics::DiagnosticSink;
7use ra_syntax::{ast, AstPtr}; 7use ra_syntax::{ast, AstNode, AstPtr};
8use rustc_hash::FxHashSet; 8use rustc_hash::FxHashSet;
9 9
10use crate::{ 10use crate::{
@@ -100,6 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
100 self.sink.push(MissingFields { 100 self.sink.push(MissingFields {
101 file: source_ptr.file_id, 101 file: source_ptr.file_id,
102 field_list: AstPtr::new(&field_list), 102 field_list: AstPtr::new(&field_list),
103 highlight_range: field_list.syntax().text_range(),
103 missed_fields, 104 missed_fields,
104 }) 105 })
105 } 106 }
@@ -130,6 +131,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
130 self.sink.push(MissingPatFields { 131 self.sink.push(MissingPatFields {
131 file: source_ptr.file_id, 132 file: source_ptr.file_id,
132 field_list: AstPtr::new(&field_list), 133 field_list: AstPtr::new(&field_list),
134 highlight_range: field_list.syntax().text_range(),
133 missed_fields, 135 missed_fields,
134 }) 136 })
135 } 137 }
@@ -213,6 +215,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
213 file: source_ptr.file_id, 215 file: source_ptr.file_id,
214 match_expr: AstPtr::new(&match_expr), 216 match_expr: AstPtr::new(&match_expr),
215 arms: AstPtr::new(&arms), 217 arms: AstPtr::new(&arms),
218 highlight_range: match_expr.syntax().text_range(),
216 }) 219 })
217 } 220 }
218 } 221 }
@@ -244,8 +247,13 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
244 let (_, source_map) = db.body_with_source_map(self.func.into()); 247 let (_, source_map) = db.body_with_source_map(self.func.into());
245 248
246 if let Ok(source_ptr) = source_map.expr_syntax(id) { 249 if let Ok(source_ptr) = source_map.expr_syntax(id) {
247 self.sink 250 let root = source_ptr.file_syntax(db.upcast());
248 .push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value }); 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 });
249 } 257 }
250 } 258 }
251 } 259 }
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index 246b0e9be..7e6cdefe4 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -665,6 +665,7 @@ impl Expectation {
665mod diagnostics { 665mod diagnostics {
666 use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup}; 666 use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup};
667 use hir_expand::diagnostics::DiagnosticSink; 667 use hir_expand::diagnostics::DiagnosticSink;
668 use ra_syntax::AstNode;
668 669
669 use crate::{db::HirDatabase, diagnostics::NoSuchField}; 670 use crate::{db::HirDatabase, diagnostics::NoSuchField};
670 671
@@ -682,10 +683,16 @@ mod diagnostics {
682 ) { 683 ) {
683 match self { 684 match self {
684 InferenceDiagnostic::NoSuchField { expr, field } => { 685 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());
686 let (_, source_map) = db.body_with_source_map(owner.into()); 687 let (_, source_map) = db.body_with_source_map(owner.into());
687 let field = source_map.field_syntax(*expr, *field); 688 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 })
689 } 696 }
690 } 697 }
691 } 698 }