internal: start new diagnostics API

At the moment, this moves only a single diagnostic, but the idea is
reafactor the rest to use the same pattern. We are going to have a
single file per diagnostic. This file will define diagnostics code,
rendering range and fixes, if any. It'll also have all of the tests.
This is similar to how we deal with assists.

After we refactor all diagnostics to follow this pattern, we'll probably
move them to a new `ide_diagnostics` crate.

Not that we intentionally want to test all diagnostics on this layer,
despite the fact that they are generally emitted in the guts on the
compiler. Diagnostics care to much about the end presentation
details/fixes to be worth-while "unit" testing. So, we'll unit-test only
the primary output of compilation process (types and name res tables),
and will use integrated UI tests for diagnostics.
This commit is contained in:
Aleksey Kladov 2021-06-13 14:41:19 +03:00
parent 546be18e3a
commit efa069d288
7 changed files with 124 additions and 84 deletions

View file

@ -15,29 +15,28 @@ pub use crate::diagnostics_sink::{
Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder,
};
// Diagnostic: unresolved-module
//
// This diagnostic is triggered if rust-analyzer is unable to discover referred module.
#[derive(Debug)]
pub struct UnresolvedModule {
pub file: HirFileId,
pub decl: AstPtr<ast::Module>,
pub candidate: String,
macro_rules! diagnostics {
($($diag:ident)*) => {
pub enum AnyDiagnostic {$(
$diag(Box<$diag>),
)*}
$(
impl From<$diag> for AnyDiagnostic {
fn from(d: $diag) -> AnyDiagnostic {
AnyDiagnostic::$diag(Box::new(d))
}
}
)*
};
}
impl Diagnostic for UnresolvedModule {
fn code(&self) -> DiagnosticCode {
DiagnosticCode("unresolved-module")
}
fn message(&self) -> String {
"unresolved module".to_string()
}
fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.decl.clone().into())
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
diagnostics![UnresolvedModule];
#[derive(Debug)]
pub struct UnresolvedModule {
pub decl: InFile<AstPtr<ast::Module>>,
pub candidate: String,
}
// Diagnostic: unresolved-extern-crate