Don't expose impl details of SyntaxPtr

This commit is contained in:
Aleksey Kladov
2020-04-17 11:38:51 +02:00
parent 69f0cb6cd7
commit 302bf97bbf
8 changed files with 55 additions and 14 deletions
+2 -2
View File
@@ -210,7 +210,7 @@ pub struct BodySourceMap {
expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>,
pat_map: FxHashMap<PatSource, PatId>,
pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>,
field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>,
field_map: FxHashMap<(ExprId, usize), InFile<AstPtr<ast::RecordField>>>,
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
}
@@ -303,7 +303,7 @@ pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
self.pat_map.get(&src).cloned()
}
pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> {
pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile<AstPtr<ast::RecordField>> {
self.field_map[&(expr, field)].clone()
}
}
+2 -1
View File
@@ -320,7 +320,8 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
let res = self.alloc_expr(record_lit, syntax_ptr);
for (i, ptr) in field_ptrs.into_iter().enumerate() {
self.source_map.field_map.insert((res, i), ptr);
let src = self.expander.to_source(ptr);
self.source_map.field_map.insert((res, i), src);
}
res
}
+5 -1
View File
@@ -4,7 +4,7 @@
use hir_expand::diagnostics::Diagnostic;
use ra_db::RelativePathBuf;
use ra_syntax::{ast, AstPtr, SyntaxNodePtr};
use ra_syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
use hir_expand::{HirFileId, InFile};
@@ -12,6 +12,7 @@
pub struct UnresolvedModule {
pub file: HirFileId,
pub decl: AstPtr<ast::Module>,
pub highlight_range: TextRange,
pub candidate: RelativePathBuf,
}
@@ -19,6 +20,9 @@ impl Diagnostic for UnresolvedModule {
fn message(&self) -> String {
"unresolved module".to_string()
}
fn highlight_range(&self) -> TextRange {
self.highlight_range
}
fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.decl.clone().into() }
}
+2 -1
View File
@@ -297,7 +297,7 @@ pub enum ModuleSource {
mod diagnostics {
use hir_expand::diagnostics::DiagnosticSink;
use ra_db::RelativePathBuf;
use ra_syntax::{ast, AstPtr};
use ra_syntax::{ast, AstNode, AstPtr};
use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
@@ -326,6 +326,7 @@ pub(super) fn add_to(
sink.push(UnresolvedModule {
file: declaration.file_id,
decl: AstPtr::new(&decl),
highlight_range: decl.syntax().text_range(),
candidate: candidate.clone(),
})
}
+1 -3
View File
@@ -22,10 +22,8 @@
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
fn message(&self) -> String;
fn highlight_range(&self) -> TextRange;
fn source(&self) -> InFile<SyntaxNodePtr>;
fn highlight_range(&self) -> TextRange {
self.source().value.range()
}
fn as_any(&self) -> &(dyn Any + Send + 'static);
}
+23 -1
View File
@@ -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 struct NoSuchField {
pub file: HirFileId,
pub field: AstPtr<ast::RecordField>,
pub highlight_range: TextRange,
}
impl Diagnostic for NoSuchField {
@@ -20,6 +21,10 @@ fn message(&self) -> String {
"no such field".to_string()
}
fn highlight_range(&self) -> TextRange {
self.highlight_range
}
fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.field.clone().into() }
}
@@ -33,6 +38,7 @@ fn as_any(&self) -> &(dyn Any + Send + 'static) {
pub struct MissingFields {
pub file: HirFileId,
pub field_list: AstPtr<ast::RecordFieldList>,
pub highlight_range: TextRange,
pub missed_fields: Vec<Name>,
}
@@ -44,6 +50,10 @@ fn message(&self) -> String {
}
buf
}
fn highlight_range(&self) -> TextRange {
self.highlight_range
}
fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.field_list.clone().into() }
}
@@ -66,6 +76,7 @@ fn ast(&self, db: &impl AstDatabase) -> Self::AST {
pub struct MissingPatFields {
pub file: HirFileId,
pub field_list: AstPtr<ast::RecordFieldPatList>,
pub highlight_range: TextRange,
pub missed_fields: Vec<Name>,
}
@@ -77,6 +88,9 @@ fn message(&self) -> String {
}
buf
}
fn highlight_range(&self) -> TextRange {
self.highlight_range
}
fn source(&self) -> InFile<SyntaxNodePtr> {
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<ast::Expr>,
pub arms: AstPtr<ast::MatchArmList>,
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<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.match_expr.clone().into() }
}
@@ -108,12 +126,16 @@ fn as_any(&self) -> &(dyn Any + Send + 'static) {
pub struct MissingOkInTailExpr {
pub file: HirFileId,
pub expr: AstPtr<ast::Expr>,
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<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.expr.clone().into() }
}
+11 -3
View File
@@ -4,7 +4,7 @@
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 @@ fn create_record_literal_missing_fields_diagnostic(
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 @@ fn create_record_pattern_missing_fields_diagnostic(
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 @@ fn validate_match(
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 @@ fn validate_results_in_tail_expr(&mut self, body_id: ExprId, id: ExprId, db: &dy
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,
});
}
}
}
+9 -2
View File
@@ -665,6 +665,7 @@ fn coercion_target(&self) -> &Ty {
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 @@ pub(super) fn add_to(
) {
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,
})
}
}
}