Files
rust/crates/ra_hir/src/diagnostics.rs

107 lines
2.6 KiB
Rust
Raw Normal View History

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