From d3621eeb02652038a8185f60d78fb4791a732dc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 19:35:30 +0300 Subject: internal: refactor unimplemented builtin macro diagnostic --- crates/hir/src/diagnostics.rs | 30 +++++----------------- crates/hir/src/lib.rs | 8 ++++-- crates/ide/src/diagnostics.rs | 7 ++--- .../src/diagnostics/unimplemented_builtin_macro.rs | 19 ++++++++++++++ 4 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 crates/ide/src/diagnostics/unimplemented_builtin_macro.rs diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 28580eeb4..3908e67a2 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -32,14 +32,15 @@ macro_rules! diagnostics { } 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 { - InFile::new(self.file, self.node.clone()) - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub node: InFile, } // Diagnostic: no-such-field diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d891d0ec1..a361158e0 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -606,8 +606,12 @@ impl Module { 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(), + ); } } } diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index aeccf1164..3fbd21c30 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -9,6 +9,7 @@ mod unresolved_extern_crate; 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::(|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), diff --git a/crates/ide/src/diagnostics/unimplemented_builtin_macro.rs b/crates/ide/src/diagnostics/unimplemented_builtin_macro.rs new file mode 100644 index 000000000..09faa3bbc --- /dev/null +++ b/crates/ide/src/diagnostics/unimplemented_builtin_macro.rs @@ -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) +} -- cgit v1.2.3