From fcca35969dd7c63a83ee34c4ce7d54cefdb72bbe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Mar 2019 16:28:47 +0300 Subject: allow dyn diagnostics --- crates/ra_hir/src/diagnostics.rs | 62 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src/diagnostics.rs') diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index 82aff9cee..46a3fdd47 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs @@ -1,6 +1,60 @@ -use crate::{expr::ExprId}; +use std::{fmt, any::Any}; -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum FunctionDiagnostic { - NoSuchField { expr: ExprId, field: usize }, +use ra_syntax::{SyntaxNodePtr, AstPtr, ast}; + +use crate::HirFileId; + +pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { + fn file(&self) -> HirFileId; + fn syntax_node(&self) -> SyntaxNodePtr; + fn message(&self) -> String; + fn as_any(&self) -> &(Any + Send + 'static); +} + +impl dyn Diagnostic { + pub fn downcast_ref(&self) -> Option<&D> { + self.as_any().downcast_ref() + } +} + +#[derive(Debug, Default)] +pub struct Diagnostics { + data: Vec>, +} + +impl Diagnostics { + pub fn push(&mut self, d: impl Diagnostic) { + self.data.push(Box::new(d)) + } + + pub fn iter<'a>(&'a self) -> impl Iterator + 'a { + self.data.iter().map(|it| it.as_ref()) + } +} + +#[derive(Debug)] +pub struct NoSuchField { + pub(crate) file: HirFileId, + pub(crate) field: AstPtr, +} + +impl NoSuchField { + pub fn field(&self) -> AstPtr { + self.field + } +} + +impl Diagnostic for NoSuchField { + fn file(&self) -> HirFileId { + self.file + } + fn syntax_node(&self) -> SyntaxNodePtr { + self.field.into() + } + fn message(&self) -> String { + "no such field".to_string() + } + fn as_any(&self) -> &(Any + Send + 'static) { + self + } } -- cgit v1.2.3