From 5c9f31d4c28478b4373e6cf5ec155745c840ee3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 23 May 2021 23:31:59 +0300 Subject: internal: move diagnostics to hir The idea here is to eventually get rid of `dyn Diagnostic` and `DiagnosticSink` infrastructure altogether, and just have a `enum hir::Diagnostic` instead. The problem with `dyn Diagnostic` is that it is defined in the lowest level of the stack (hir_expand), but is used by the highest level (ide). As a first step, we free hir_expand and hir_def from `dyn Diagnostic` and kick the can up to `hir_ty`, as an intermediate state. The plan is then to move DiagnosticSink similarly to the hir crate, and, as final third step, remove its usage from the ide. One currently unsolved problem is testing. You can notice that the test which checks precise diagnostic ranges, unresolved_import_in_use_tree, was moved to the ide layer. Logically, only IDE should have the infra to render a specific range. At the same time, the range is determined with the data produced in hir_def and hir crates, so this layering is rather unfortunate. Working on hir_def shouldn't require compiling `ide` for testing. --- crates/ide/src/diagnostics.rs | 65 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 27d347dbd..dcac7c76d 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -299,10 +299,10 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist { #[cfg(test)] mod tests { - use expect_test::{expect, Expect}; + use expect_test::Expect; use ide_assists::AssistResolveStrategy; use stdx::trim_indent; - use test_utils::assert_eq_text; + use test_utils::{assert_eq_text, extract_annotations}; use crate::{fixture, DiagnosticsConfig}; @@ -396,26 +396,51 @@ mod tests { expect.assert_debug_eq(&diagnostics) } + pub(crate) fn check_diagnostics(ra_fixture: &str) { + let (analysis, file_id) = fixture::file(ra_fixture); + let diagnostics = analysis + .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id) + .unwrap(); + + let expected = extract_annotations(&*analysis.file_text(file_id).unwrap()); + let actual = diagnostics.into_iter().map(|d| (d.range, d.message)).collect::>(); + assert_eq!(expected, actual); + } + #[test] fn test_unresolved_macro_range() { - check_expect( - r#"foo::bar!(92);"#, - expect![[r#" - [ - Diagnostic { - message: "unresolved macro `foo::bar!`", - range: 5..8, - severity: Error, - fixes: None, - unused: false, - code: Some( - DiagnosticCode( - "unresolved-macro-call", - ), - ), - }, - ] - "#]], + check_diagnostics( + r#" +foo::bar!(92); + //^^^ unresolved macro `foo::bar!` +"#, + ); + } + + #[test] + fn unresolved_import_in_use_tree() { + // Only the relevant part of a nested `use` item should be highlighted. + check_diagnostics( + r#" +use does_exist::{Exists, DoesntExist}; + //^^^^^^^^^^^ unresolved import + +use {does_not_exist::*, does_exist}; + //^^^^^^^^^^^^^^^^^ unresolved import + +use does_not_exist::{ + a, + //^ unresolved import + b, + //^ unresolved import + c, + //^ unresolved import +}; + +mod does_exist { + pub struct Exists; +} +"#, ); } -- cgit v1.2.3