internal: refactor unimplemented builtin macro diagnostic

This commit is contained in:
Aleksey Kladov 2021-06-13 19:35:30 +03:00
parent dec207f56a
commit d3621eeb02
4 changed files with 33 additions and 31 deletions

View File

@ -32,14 +32,15 @@ fn from(d: $diag) -> AnyDiagnostic {
}
diagnostics![
UnresolvedModule,
InactiveCode,
MacroError,
MissingFields,
UnimplementedBuiltinMacro,
UnresolvedExternCrate,
UnresolvedImport,
UnresolvedMacroCall,
UnresolvedModule,
UnresolvedProcMacro,
MacroError,
MissingFields,
InactiveCode,
];
#[derive(Debug)]
@ -88,26 +89,7 @@ pub struct MacroError {
#[derive(Debug)]
pub struct UnimplementedBuiltinMacro {
pub file: HirFileId,
pub node: SyntaxNodePtr,
}
impl Diagnostic for UnimplementedBuiltinMacro {
fn code(&self) -> DiagnosticCode {
DiagnosticCode("unimplemented-builtin-macro")
}
fn message(&self) -> String {
"unimplemented built-in macro".to_string()
}
fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.node.clone())
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
pub node: InFile<SyntaxNodePtr>,
}
// Diagnostic: no-such-field

View File

@ -606,8 +606,12 @@ pub fn diagnostics(
let node = ast.to_node(db.upcast());
// Must have a name, otherwise we wouldn't emit it.
let name = node.name().expect("unimplemented builtin macro with no name");
let ptr = SyntaxNodePtr::from(AstPtr::new(&name));
sink.push(UnimplementedBuiltinMacro { file: ast.file_id, node: ptr });
acc.push(
UnimplementedBuiltinMacro {
node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&name))),
}
.into(),
);
}
}
}

View File

@ -9,6 +9,7 @@
mod unresolved_import;
mod unresolved_macro_call;
mod unresolved_proc_macro;
mod unimplemented_builtin_macro;
mod macro_error;
mod inactive_code;
mod missing_fields;
@ -185,11 +186,6 @@ pub(crate) fn diagnostics(
.with_code(Some(d.code())),
);
})
.on::<hir::diagnostics::UnimplementedBuiltinMacro, _>(|d| {
let display_range = sema.diagnostics_display_range(d.display_source()).range;
res.borrow_mut()
.push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code())));
})
// Only collect experimental diagnostics when they're enabled.
.filter(|diag| !(diag.is_experimental() && config.disable_experimental))
.filter(|diag| !config.disabled.contains(diag.code().as_str()));
@ -229,6 +225,7 @@ pub(crate) fn diagnostics(
AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d),
AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),

View File

@ -0,0 +1,19 @@
use crate::{
diagnostics::{Diagnostic, DiagnosticsContext},
Severity,
};
// Diagnostic: unimplemented-builtin-macro
//
// This diagnostic is shown for builtin macros which are not yet implemented by rust-analyzer
pub(super) fn unimplemented_builtin_macro(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnimplementedBuiltinMacro,
) -> Diagnostic {
Diagnostic::new(
"unimplemented-builtin-macro",
"unimplemented built-in macro".to_string(),
ctx.sema.diagnostics_display_range(d.node.clone()).range,
)
.severity(Severity::WeakWarning)
}