From 302bf97bbf1855e3c7def9ab4f9f3d338be5e3b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 11:38:51 +0200 Subject: Don't expose impl details of SyntaxPtr --- crates/ra_hir_ty/src/diagnostics.rs | 24 +++++++++++++++++++++++- crates/ra_hir_ty/src/expr.rs | 14 +++++++++++--- crates/ra_hir_ty/src/infer.rs | 11 +++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_ty/src') 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 @@ use std::any::Any; use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; -use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; +use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr, TextRange}; use stdx::format_to; pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm}; @@ -13,6 +13,7 @@ pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; pub struct NoSuchField { pub file: HirFileId, pub field: AstPtr, + pub highlight_range: TextRange, } impl Diagnostic for NoSuchField { @@ -20,6 +21,10 @@ impl Diagnostic for NoSuchField { "no such field".to_string() } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } + fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field.clone().into() } } @@ -33,6 +38,7 @@ impl Diagnostic for NoSuchField { pub struct MissingFields { pub file: HirFileId, pub field_list: AstPtr, + pub highlight_range: TextRange, pub missed_fields: Vec, } @@ -44,6 +50,10 @@ impl Diagnostic for MissingFields { } buf } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } + fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } @@ -66,6 +76,7 @@ impl AstDiagnostic for MissingFields { pub struct MissingPatFields { pub file: HirFileId, pub field_list: AstPtr, + pub highlight_range: TextRange, pub missed_fields: Vec, } @@ -77,6 +88,9 @@ impl Diagnostic for MissingPatFields { } buf } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } @@ -90,12 +104,16 @@ pub struct MissingMatchArms { pub file: HirFileId, pub match_expr: AstPtr, pub arms: AstPtr, + pub highlight_range: TextRange, } impl Diagnostic for MissingMatchArms { fn message(&self) -> String { String::from("Missing match arm") } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.match_expr.clone().into() } } @@ -108,12 +126,16 @@ impl Diagnostic for MissingMatchArms { pub struct MissingOkInTailExpr { pub file: HirFileId, pub expr: AstPtr, + pub highlight_range: TextRange, } impl Diagnostic for MissingOkInTailExpr { fn message(&self) -> String { "wrap return expression in Ok".to_string() } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } } 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; use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId}; use hir_expand::diagnostics::DiagnosticSink; -use ra_syntax::{ast, AstPtr}; +use ra_syntax::{ast, AstNode, AstPtr}; use rustc_hash::FxHashSet; use crate::{ @@ -100,6 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { self.sink.push(MissingFields { file: source_ptr.file_id, field_list: AstPtr::new(&field_list), + highlight_range: field_list.syntax().text_range(), missed_fields, }) } @@ -130,6 +131,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { self.sink.push(MissingPatFields { file: source_ptr.file_id, field_list: AstPtr::new(&field_list), + highlight_range: field_list.syntax().text_range(), missed_fields, }) } @@ -213,6 +215,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { file: source_ptr.file_id, match_expr: AstPtr::new(&match_expr), arms: AstPtr::new(&arms), + highlight_range: match_expr.syntax().text_range(), }) } } @@ -244,8 +247,13 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let (_, source_map) = db.body_with_source_map(self.func.into()); if let Ok(source_ptr) = source_map.expr_syntax(id) { - self.sink - .push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value }); + let root = source_ptr.file_syntax(db.upcast()); + let highlight_range = source_ptr.value.to_node(&root).syntax().text_range(); + self.sink.push(MissingOkInTailExpr { + file: source_ptr.file_id, + expr: source_ptr.value, + highlight_range, + }); } } } 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 { mod diagnostics { use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup}; use hir_expand::diagnostics::DiagnosticSink; + use ra_syntax::AstNode; use crate::{db::HirDatabase, diagnostics::NoSuchField}; @@ -682,10 +683,16 @@ mod diagnostics { ) { match self { InferenceDiagnostic::NoSuchField { expr, field } => { - let file = owner.lookup(db.upcast()).source(db.upcast()).file_id; + let source = owner.lookup(db.upcast()).source(db.upcast()); let (_, source_map) = db.body_with_source_map(owner.into()); let field = source_map.field_syntax(*expr, *field); - sink.push(NoSuchField { file, field }) + let root = field.file_syntax(db.upcast()); + let highlight_range = field.value.to_node(&root).syntax().text_range(); + sink.push(NoSuchField { + file: source.file_id, + field: field.value, + highlight_range, + }) } } } -- cgit v1.2.3 From a8196ffe8466aa60dec56e77c2da717793c0debe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 13:06:02 +0200 Subject: Correctly highlight ranges of diagnostics from macros closes #2799 --- crates/ra_hir_ty/src/diagnostics.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index da85bd082..018c2ad3f 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -21,12 +21,12 @@ impl Diagnostic for NoSuchField { "no such field".to_string() } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { - InFile { file_id: self.file, value: self.field.clone().into() } + InFile::new(self.file, self.field.clone().into()) } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -50,8 +50,8 @@ impl Diagnostic for MissingFields { } buf } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { @@ -88,8 +88,8 @@ impl Diagnostic for MissingPatFields { } buf } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } @@ -111,8 +111,8 @@ impl Diagnostic for MissingMatchArms { fn message(&self) -> String { String::from("Missing match arm") } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.match_expr.clone().into() } @@ -133,8 +133,8 @@ impl Diagnostic for MissingOkInTailExpr { fn message(&self) -> String { "wrap return expression in Ok".to_string() } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } -- cgit v1.2.3 From 146f6f5a45a4bfd98ab0eb54bb30610d784433c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 13:55:05 +0200 Subject: Simplify Diagnostic structure It's not entirely clear what subnode ranges should mean in the presence of macros, so let's leave them out for now. We are not using them heavily anyway. --- crates/ra_hir_ty/src/diagnostics.rs | 24 +----------------------- crates/ra_hir_ty/src/expr.rs | 14 +++----------- crates/ra_hir_ty/src/infer.rs | 9 +-------- 3 files changed, 5 insertions(+), 42 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 018c2ad3f..c8fd54861 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -3,7 +3,7 @@ use std::any::Any; use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; -use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr, TextRange}; +use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; use stdx::format_to; pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm}; @@ -13,7 +13,6 @@ pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; pub struct NoSuchField { pub file: HirFileId, pub field: AstPtr, - pub highlight_range: TextRange, } impl Diagnostic for NoSuchField { @@ -21,10 +20,6 @@ impl Diagnostic for NoSuchField { "no such field".to_string() } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } - fn source(&self) -> InFile { InFile::new(self.file, self.field.clone().into()) } @@ -38,7 +33,6 @@ impl Diagnostic for NoSuchField { pub struct MissingFields { pub file: HirFileId, pub field_list: AstPtr, - pub highlight_range: TextRange, pub missed_fields: Vec, } @@ -50,10 +44,6 @@ impl Diagnostic for MissingFields { } buf } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } - fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } @@ -76,7 +66,6 @@ impl AstDiagnostic for MissingFields { pub struct MissingPatFields { pub file: HirFileId, pub field_list: AstPtr, - pub highlight_range: TextRange, pub missed_fields: Vec, } @@ -88,9 +77,6 @@ impl Diagnostic for MissingPatFields { } buf } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } @@ -104,16 +90,12 @@ pub struct MissingMatchArms { pub file: HirFileId, pub match_expr: AstPtr, pub arms: AstPtr, - pub highlight_range: TextRange, } impl Diagnostic for MissingMatchArms { fn message(&self) -> String { String::from("Missing match arm") } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.match_expr.clone().into() } } @@ -126,16 +108,12 @@ impl Diagnostic for MissingMatchArms { pub struct MissingOkInTailExpr { pub file: HirFileId, pub expr: AstPtr, - pub highlight_range: TextRange, } impl Diagnostic for MissingOkInTailExpr { fn message(&self) -> String { "wrap return expression in Ok".to_string() } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } } diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs index 1d3950b70..fd59f4320 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; use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId}; use hir_expand::diagnostics::DiagnosticSink; -use ra_syntax::{ast, AstNode, AstPtr}; +use ra_syntax::{ast, AstPtr}; use rustc_hash::FxHashSet; use crate::{ @@ -100,7 +100,6 @@ impl<'a, 'b> ExprValidator<'a, 'b> { self.sink.push(MissingFields { file: source_ptr.file_id, field_list: AstPtr::new(&field_list), - highlight_range: field_list.syntax().text_range(), missed_fields, }) } @@ -131,7 +130,6 @@ impl<'a, 'b> ExprValidator<'a, 'b> { self.sink.push(MissingPatFields { file: source_ptr.file_id, field_list: AstPtr::new(&field_list), - highlight_range: field_list.syntax().text_range(), missed_fields, }) } @@ -215,7 +213,6 @@ impl<'a, 'b> ExprValidator<'a, 'b> { file: source_ptr.file_id, match_expr: AstPtr::new(&match_expr), arms: AstPtr::new(&arms), - highlight_range: match_expr.syntax().text_range(), }) } } @@ -247,13 +244,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let (_, source_map) = db.body_with_source_map(self.func.into()); if let Ok(source_ptr) = source_map.expr_syntax(id) { - let root = source_ptr.file_syntax(db.upcast()); - let highlight_range = source_ptr.value.to_node(&root).syntax().text_range(); - self.sink.push(MissingOkInTailExpr { - file: source_ptr.file_id, - expr: source_ptr.value, - highlight_range, - }); + self.sink + .push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value }); } } } diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index 7e6cdefe4..b6d9b3438 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs @@ -665,7 +665,6 @@ impl Expectation { mod diagnostics { use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup}; use hir_expand::diagnostics::DiagnosticSink; - use ra_syntax::AstNode; use crate::{db::HirDatabase, diagnostics::NoSuchField}; @@ -686,13 +685,7 @@ mod diagnostics { let source = owner.lookup(db.upcast()).source(db.upcast()); let (_, source_map) = db.body_with_source_map(owner.into()); let field = source_map.field_syntax(*expr, *field); - let root = field.file_syntax(db.upcast()); - let highlight_range = field.value.to_node(&root).syntax().text_range(); - sink.push(NoSuchField { - file: source.file_id, - field: field.value, - highlight_range, - }) + sink.push(NoSuchField { file: source.file_id, field: field.value }) } } } -- cgit v1.2.3