mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 22:13:39 +00:00
Handle diagnostics with multiple primary spans
This commit is contained in:
parent
637c795b3c
commit
98e8ad5e60
11 changed files with 564 additions and 537 deletions
|
@ -180,13 +180,13 @@ pub(crate) struct MappedRustDiagnostic {
|
||||||
pub(crate) fn map_rust_diagnostic_to_lsp(
|
pub(crate) fn map_rust_diagnostic_to_lsp(
|
||||||
rd: &RustDiagnostic,
|
rd: &RustDiagnostic,
|
||||||
workspace_root: &PathBuf,
|
workspace_root: &PathBuf,
|
||||||
) -> Option<MappedRustDiagnostic> {
|
) -> Vec<MappedRustDiagnostic> {
|
||||||
let primary_span = rd.spans.iter().find(|s| s.is_primary)?;
|
let primary_spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
|
||||||
|
if primary_spans.is_empty() {
|
||||||
let location = map_span_to_location(&primary_span, workspace_root);
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
let severity = map_level_to_severity(rd.level);
|
let severity = map_level_to_severity(rd.level);
|
||||||
let mut primary_span_label = primary_span.label.as_ref();
|
|
||||||
|
|
||||||
let mut source = String::from("rustc");
|
let mut source = String::from("rustc");
|
||||||
let mut code = rd.code.as_ref().map(|c| c.code.clone());
|
let mut code = rd.code.as_ref().map(|c| c.code.clone());
|
||||||
|
@ -199,19 +199,10 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut needs_primary_span_label = true;
|
||||||
let mut related_information = vec![];
|
let mut related_information = vec![];
|
||||||
let mut tags = vec![];
|
let mut tags = vec![];
|
||||||
|
|
||||||
// If error occurs from macro expansion, add related info pointing to
|
|
||||||
// where the error originated
|
|
||||||
if !is_from_macro(&primary_span.file_name) && primary_span.expansion.is_some() {
|
|
||||||
let def_loc = map_span_to_location_naive(&primary_span, workspace_root);
|
|
||||||
related_information.push(DiagnosticRelatedInformation {
|
|
||||||
location: def_loc,
|
|
||||||
message: "Error originated from macro here".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
|
for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
|
||||||
let related = map_secondary_span_to_related(secondary_span, workspace_root);
|
let related = map_secondary_span_to_related(secondary_span, workspace_root);
|
||||||
if let Some(related) = related {
|
if let Some(related) = related {
|
||||||
|
@ -231,15 +222,11 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
|
||||||
|
|
||||||
// These secondary messages usually duplicate the content of the
|
// These secondary messages usually duplicate the content of the
|
||||||
// primary span label.
|
// primary span label.
|
||||||
primary_span_label = None;
|
needs_primary_span_label = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(primary_span_label) = primary_span_label {
|
|
||||||
write!(&mut message, "\n{}", primary_span_label).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_unused_or_unnecessary(rd) {
|
if is_unused_or_unnecessary(rd) {
|
||||||
tags.push(DiagnosticTag::Unnecessary);
|
tags.push(DiagnosticTag::Unnecessary);
|
||||||
}
|
}
|
||||||
|
@ -248,21 +235,45 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
|
||||||
tags.push(DiagnosticTag::Deprecated);
|
tags.push(DiagnosticTag::Deprecated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
primary_spans
|
||||||
|
.iter()
|
||||||
|
.map(|primary_span| {
|
||||||
|
let location = map_span_to_location(&primary_span, workspace_root);
|
||||||
|
|
||||||
|
let mut message = message.clone();
|
||||||
|
if needs_primary_span_label {
|
||||||
|
if let Some(primary_span_label) = &primary_span.label {
|
||||||
|
write!(&mut message, "\n{}", primary_span_label).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If error occurs from macro expansion, add related info pointing to
|
||||||
|
// where the error originated
|
||||||
|
if !is_from_macro(&primary_span.file_name) && primary_span.expansion.is_some() {
|
||||||
|
let def_loc = map_span_to_location_naive(&primary_span, workspace_root);
|
||||||
|
related_information.push(DiagnosticRelatedInformation {
|
||||||
|
location: def_loc,
|
||||||
|
message: "Error originated from macro here".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let diagnostic = Diagnostic {
|
let diagnostic = Diagnostic {
|
||||||
range: location.range,
|
range: location.range,
|
||||||
severity,
|
severity,
|
||||||
code: code.map(NumberOrString::String),
|
code: code.clone().map(NumberOrString::String),
|
||||||
source: Some(source),
|
source: Some(source.clone()),
|
||||||
message,
|
message,
|
||||||
related_information: if !related_information.is_empty() {
|
related_information: if !related_information.is_empty() {
|
||||||
Some(related_information)
|
Some(related_information.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
tags: if !tags.is_empty() { Some(tags) } else { None },
|
tags: if !tags.is_empty() { Some(tags.clone()) } else { None },
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(MappedRustDiagnostic { location, diagnostic, fixes })
|
MappedRustDiagnostic { location, diagnostic, fixes: fixes.clone() }
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a `Url` object from a given path, will lowercase drive letters if present.
|
/// Returns a `Url` object from a given path, will lowercase drive letters if present.
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/compiler/mir/tagset.rs",
|
uri: "file:///test/compiler/mir/tagset.rs",
|
||||||
|
@ -96,4 +97,5 @@ MappedRustDiagnostic {
|
||||||
is_preferred: None,
|
is_preferred: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/src/main.rs",
|
uri: "file:///test/src/main.rs",
|
||||||
|
@ -43,4 +44,5 @@ MappedRustDiagnostic {
|
||||||
tags: None,
|
tags: None,
|
||||||
},
|
},
|
||||||
fixes: [],
|
fixes: [],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/crates/ra_hir_def/src/data.rs",
|
uri: "file:///test/crates/ra_hir_def/src/data.rs",
|
||||||
|
@ -58,4 +59,5 @@ MappedRustDiagnostic {
|
||||||
tags: None,
|
tags: None,
|
||||||
},
|
},
|
||||||
fixes: [],
|
fixes: [],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/src/main.rs",
|
uri: "file:///test/src/main.rs",
|
||||||
|
@ -109,4 +110,5 @@ MappedRustDiagnostic {
|
||||||
is_preferred: None,
|
is_preferred: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/compiler/ty/list_iter.rs",
|
uri: "file:///test/compiler/ty/list_iter.rs",
|
||||||
|
@ -43,4 +44,5 @@ MappedRustDiagnostic {
|
||||||
tags: None,
|
tags: None,
|
||||||
},
|
},
|
||||||
fixes: [],
|
fixes: [],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/runtime/compiler_support.rs",
|
uri: "file:///test/runtime/compiler_support.rs",
|
||||||
|
@ -43,4 +44,5 @@ MappedRustDiagnostic {
|
||||||
tags: None,
|
tags: None,
|
||||||
},
|
},
|
||||||
fixes: [],
|
fixes: [],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/driver/subcommand/repl.rs",
|
uri: "file:///test/driver/subcommand/repl.rs",
|
||||||
|
@ -81,4 +82,5 @@ MappedRustDiagnostic {
|
||||||
is_preferred: None,
|
is_preferred: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
source: crates/ra_cargo_watch/src/conv/test.rs
|
source: crates/ra_cargo_watch/src/conv/test.rs
|
||||||
expression: diag
|
expression: diag
|
||||||
---
|
---
|
||||||
|
[
|
||||||
MappedRustDiagnostic {
|
MappedRustDiagnostic {
|
||||||
location: Location {
|
location: Location {
|
||||||
uri: "file:///test/compiler/ty/select.rs",
|
uri: "file:///test/compiler/ty/select.rs",
|
||||||
|
@ -62,4 +63,5 @@ MappedRustDiagnostic {
|
||||||
tags: None,
|
tags: None,
|
||||||
},
|
},
|
||||||
fixes: [],
|
fixes: [],
|
||||||
}
|
},
|
||||||
|
]
|
||||||
|
|
|
@ -58,7 +58,7 @@ fn snap_rustc_incompatible_type_for_trait() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ fn snap_rustc_unused_variable() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ fn snap_rustc_wrong_number_of_parameters() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,7 +387,7 @@ fn snap_clippy_pass_by_ref() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,7 +431,7 @@ fn snap_rustc_mismatched_type() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -703,7 +703,7 @@ fn snap_handles_macro_location() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -933,7 +933,7 @@ fn snap_macro_compiler_error() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1067,6 +1067,6 @@ fn snap_multi_line_fix() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let workspace_root = PathBuf::from("/test/");
|
let workspace_root = PathBuf::from("/test/");
|
||||||
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic");
|
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
|
||||||
insta::assert_debug_snapshot!(diag);
|
insta::assert_debug_snapshot!(diag);
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,13 +197,12 @@ impl CheckWatcherThread {
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckEvent::Msg(Message::CompilerMessage(msg)) => {
|
CheckEvent::Msg(Message::CompilerMessage(msg)) => {
|
||||||
let map_result =
|
let map_result = map_rust_diagnostic_to_lsp(&msg.message, &self.workspace_root);
|
||||||
match map_rust_diagnostic_to_lsp(&msg.message, &self.workspace_root) {
|
if map_result.is_empty() {
|
||||||
Some(map_result) => map_result,
|
return;
|
||||||
None => return,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let MappedRustDiagnostic { location, diagnostic, fixes } = map_result;
|
for MappedRustDiagnostic { location, diagnostic, fixes } in map_result {
|
||||||
let fixes = fixes
|
let fixes = fixes
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|fix| {
|
.map(|fix| {
|
||||||
|
@ -215,6 +214,7 @@ impl CheckWatcherThread {
|
||||||
.send(CheckTask::AddDiagnostic { url: location.uri, diagnostic, fixes })
|
.send(CheckTask::AddDiagnostic { url: location.uri, diagnostic, fixes })
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {}
|
CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {}
|
||||||
CheckEvent::Msg(Message::Unknown) => {}
|
CheckEvent::Msg(Message::Unknown) => {}
|
||||||
|
|
Loading…
Reference in a new issue