rust-analyzer/crates/ra_hir/src/diagnostics.rs

107 lines
2.6 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-11-02 20:42:38 +00:00
use std::any::Any;
2019-03-21 19:13:11 +00:00
2019-11-02 20:42:38 +00:00
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
2019-03-24 07:21:36 +00:00
use relative_path::RelativePathBuf;
2019-03-23 13:28:47 +00:00
2019-11-02 20:42:38 +00:00
use crate::{db::AstDatabase, HirFileId, Name, Source};
2019-03-23 13:28:47 +00:00
2019-11-02 20:42:38 +00:00
pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
2019-03-23 13:28:47 +00:00
#[derive(Debug)]
pub struct NoSuchField {
2019-03-23 15:35:14 +00:00
pub file: HirFileId,
2019-08-23 12:55:21 +00:00
pub field: AstPtr<ast::RecordField>,
2019-03-23 13:28:47 +00:00
}
impl Diagnostic for NoSuchField {
2019-03-23 17:41:59 +00:00
fn message(&self) -> String {
"no such field".to_string()
}
2019-08-12 16:06:08 +00:00
fn source(&self) -> Source<SyntaxNodePtr> {
Source { file_id: self.file, ast: self.field.into() }
2019-03-23 13:28:47 +00:00
}
2019-08-12 16:06:08 +00:00
fn as_any(&self) -> &(dyn Any + Send + 'static) {
2019-03-23 13:28:47 +00:00
self
}
2019-03-21 19:13:11 +00:00
}
2019-03-23 15:35:14 +00:00
#[derive(Debug)]
pub struct UnresolvedModule {
pub file: HirFileId,
pub decl: AstPtr<ast::Module>,
pub candidate: RelativePathBuf,
}
impl Diagnostic for UnresolvedModule {
2019-03-23 17:41:59 +00:00
fn message(&self) -> String {
"unresolved module".to_string()
}
2019-08-12 16:06:08 +00:00
fn source(&self) -> Source<SyntaxNodePtr> {
Source { file_id: self.file, ast: self.decl.into() }
2019-03-23 15:35:14 +00:00
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
2019-03-23 15:35:14 +00:00
self
}
}
2019-04-10 21:00:56 +00:00
#[derive(Debug)]
pub struct MissingFields {
pub file: HirFileId,
2019-08-23 12:55:21 +00:00
pub field_list: AstPtr<ast::RecordFieldList>,
2019-04-10 21:00:56 +00:00
pub missed_fields: Vec<Name>,
}
impl Diagnostic for MissingFields {
fn message(&self) -> String {
"fill structure fields".to_string()
}
2019-08-12 16:06:08 +00:00
fn source(&self) -> Source<SyntaxNodePtr> {
Source { file_id: self.file, ast: self.field_list.into() }
2019-04-10 21:00:56 +00:00
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
}
impl AstDiagnostic for MissingFields {
2019-08-23 12:55:21 +00:00
type AST = ast::RecordFieldList;
2019-11-02 20:42:38 +00:00
fn ast(&self, db: &impl AstDatabase) -> Self::AST {
2019-08-12 16:06:08 +00:00
let root = db.parse_or_expand(self.source().file_id).unwrap();
let node = self.source().ast.to_node(&root);
2019-08-23 12:55:21 +00:00
ast::RecordFieldList::cast(node).unwrap()
}
}
2019-08-10 15:40:48 +00:00
#[derive(Debug)]
pub struct MissingOkInTailExpr {
pub file: HirFileId,
pub expr: AstPtr<ast::Expr>,
}
impl Diagnostic for MissingOkInTailExpr {
fn message(&self) -> String {
"wrap return expression in Ok".to_string()
}
2019-08-13 06:31:06 +00:00
fn source(&self) -> Source<SyntaxNodePtr> {
Source { file_id: self.file, ast: self.expr.into() }
2019-08-10 15:40:48 +00:00
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
}
impl AstDiagnostic for MissingOkInTailExpr {
type AST = ast::Expr;
2019-11-02 20:42:38 +00:00
fn ast(&self, db: &impl AstDatabase) -> Self::AST {
2019-08-13 06:31:06 +00:00
let root = db.parse_or_expand(self.file).unwrap();
let node = self.source().ast.to_node(&root);
2019-08-10 15:40:48 +00:00
ast::Expr::cast(node).unwrap()
}
}