2021-06-14 11:32:39 -05:00
|
|
|
//! Diagnostics rendering and fixits.
|
2021-06-14 05:15:05 -05:00
|
|
|
//!
|
2021-06-14 11:32:39 -05:00
|
|
|
//! Most of the diagnostics originate from the dark depth of the compiler, and
|
|
|
|
//! are originally expressed in term of IR. When we emit the diagnostic, we are
|
|
|
|
//! usually not in the position to decide how to best "render" it in terms of
|
|
|
|
//! user-authored source code. We are especially not in the position to offer
|
|
|
|
//! fixits, as the compiler completely lacks the infrastructure to edit the
|
|
|
|
//! source code.
|
|
|
|
//!
|
|
|
|
//! Instead, we "bubble up" raw, structured diagnostics until the `hir` crate,
|
|
|
|
//! where we "cook" them so that each diagnostic is formulated in terms of `hir`
|
|
|
|
//! types. Well, at least that's the aspiration, the "cooking" is somewhat
|
|
|
|
//! ad-hoc at the moment. Anyways, we get a bunch of ide-friendly diagnostic
|
|
|
|
//! structs from hir, and we want to render them to unified serializable
|
|
|
|
//! representation (span, level, message) here. If we can, we also provide
|
|
|
|
//! fixits. By the way, that's why we want to keep diagnostics structured
|
|
|
|
//! internally -- so that we have all the info to make fixes.
|
|
|
|
//!
|
|
|
|
//! We have one "handler" module per diagnostic code. Such a module contains
|
|
|
|
//! rendering, optional fixes and tests. It's OK if some low-level compiler
|
|
|
|
//! functionality ends up being tested via a diagnostic.
|
|
|
|
//!
|
|
|
|
//! There are also a couple of ad-hoc diagnostics implemented directly here, we
|
|
|
|
//! don't yet have a great pattern for how to do them properly.
|
|
|
|
|
2022-07-20 07:59:42 -05:00
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
|
|
|
|
|
2021-06-14 11:32:39 -05:00
|
|
|
mod handlers {
|
2023-04-06 08:37:53 -05:00
|
|
|
pub(crate) mod break_outside_of_loop;
|
2023-03-03 11:04:24 -06:00
|
|
|
pub(crate) mod expected_function;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod inactive_code;
|
2023-01-20 16:09:35 -06:00
|
|
|
pub(crate) mod incoherent_impl;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod incorrect_case;
|
2021-11-18 15:17:22 -06:00
|
|
|
pub(crate) mod invalid_derive_target;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod macro_error;
|
2021-11-19 06:17:35 -06:00
|
|
|
pub(crate) mod malformed_derive;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod mismatched_arg_count;
|
|
|
|
pub(crate) mod missing_fields;
|
|
|
|
pub(crate) mod missing_match_arms;
|
|
|
|
pub(crate) mod missing_unsafe;
|
2023-02-21 13:00:38 -06:00
|
|
|
pub(crate) mod mutability_errors;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod no_such_field;
|
2023-01-01 06:24:48 -06:00
|
|
|
pub(crate) mod private_assoc_item;
|
2022-12-31 07:20:59 -06:00
|
|
|
pub(crate) mod private_field;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod replace_filter_map_next_with_find_map;
|
2022-03-21 13:16:58 -05:00
|
|
|
pub(crate) mod type_mismatch;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod unimplemented_builtin_macro;
|
|
|
|
pub(crate) mod unresolved_extern_crate;
|
2023-03-03 12:32:18 -06:00
|
|
|
pub(crate) mod unresolved_field;
|
2023-03-03 13:41:17 -06:00
|
|
|
pub(crate) mod unresolved_method;
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) mod unresolved_import;
|
|
|
|
pub(crate) mod unresolved_macro_call;
|
|
|
|
pub(crate) mod unresolved_module;
|
|
|
|
pub(crate) mod unresolved_proc_macro;
|
2023-04-06 05:50:16 -05:00
|
|
|
pub(crate) mod undeclared_label;
|
|
|
|
pub(crate) mod unreachable_label;
|
2021-06-14 05:15:05 -05:00
|
|
|
|
2021-07-26 03:12:41 -05:00
|
|
|
// The handlers below are unusual, the implement the diagnostics as well.
|
2021-06-14 13:23:59 -05:00
|
|
|
pub(crate) mod field_shorthand;
|
|
|
|
pub(crate) mod useless_braces;
|
|
|
|
pub(crate) mod unlinked_file;
|
2022-07-28 13:08:44 -05:00
|
|
|
pub(crate) mod json_is_not_rust;
|
2021-06-14 13:23:59 -05:00
|
|
|
}
|
2021-06-14 05:15:05 -05:00
|
|
|
|
2021-07-03 14:11:03 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2022-07-23 16:16:36 -05:00
|
|
|
use hir::{diagnostics::AnyDiagnostic, InFile, Semantics};
|
2021-06-14 05:15:05 -05:00
|
|
|
use ide_db::{
|
2021-06-14 05:27:11 -05:00
|
|
|
assists::{Assist, AssistId, AssistKind, AssistResolveStrategy},
|
2022-07-23 16:16:36 -05:00
|
|
|
base_db::{FileId, FileRange, SourceDatabase},
|
2022-08-06 09:51:51 -05:00
|
|
|
imports::insert_use::InsertUseConfig,
|
2021-06-14 05:15:05 -05:00
|
|
|
label::Label,
|
|
|
|
source_change::SourceChange,
|
2022-04-25 11:51:59 -05:00
|
|
|
FxHashSet, RootDatabase,
|
2021-06-14 05:15:05 -05:00
|
|
|
};
|
2022-07-23 16:16:36 -05:00
|
|
|
use syntax::{algo::find_node_at_range, ast::AstNode, SyntaxNodePtr, TextRange};
|
2021-06-14 05:15:05 -05:00
|
|
|
|
2023-03-16 10:26:19 -05:00
|
|
|
// FIXME: Make this an enum
|
2021-06-14 05:15:05 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub struct DiagnosticCode(pub &'static str);
|
|
|
|
|
|
|
|
impl DiagnosticCode {
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Diagnostic {
|
|
|
|
pub code: DiagnosticCode,
|
|
|
|
pub message: String,
|
|
|
|
pub range: TextRange,
|
|
|
|
pub severity: Severity,
|
|
|
|
pub unused: bool,
|
|
|
|
pub experimental: bool,
|
|
|
|
pub fixes: Option<Vec<Assist>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Diagnostic {
|
|
|
|
fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic {
|
|
|
|
let message = message.into();
|
|
|
|
Diagnostic {
|
|
|
|
code: DiagnosticCode(code),
|
|
|
|
message,
|
|
|
|
range,
|
|
|
|
severity: Severity::Error,
|
|
|
|
unused: false,
|
|
|
|
experimental: false,
|
|
|
|
fixes: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn experimental(mut self) -> Diagnostic {
|
|
|
|
self.experimental = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn severity(mut self, severity: Severity) -> Diagnostic {
|
|
|
|
self.severity = severity;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic {
|
|
|
|
self.fixes = fixes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_unused(mut self, unused: bool) -> Diagnostic {
|
|
|
|
self.unused = unused;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum Severity {
|
|
|
|
Error,
|
2021-06-14 13:23:59 -05:00
|
|
|
// We don't actually emit this one yet, but we should at some point.
|
|
|
|
// Warning,
|
2021-06-14 05:15:05 -05:00
|
|
|
WeakWarning,
|
|
|
|
}
|
|
|
|
|
2021-12-31 09:11:17 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum ExprFillDefaultMode {
|
|
|
|
Todo,
|
2022-01-07 07:13:34 -06:00
|
|
|
Default,
|
2021-12-31 09:11:17 -06:00
|
|
|
}
|
|
|
|
impl Default for ExprFillDefaultMode {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Todo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 09:51:51 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2021-06-14 05:15:05 -05:00
|
|
|
pub struct DiagnosticsConfig {
|
2022-06-14 03:40:57 -05:00
|
|
|
pub proc_macros_enabled: bool,
|
|
|
|
pub proc_attr_macros_enabled: bool,
|
2021-06-14 05:15:05 -05:00
|
|
|
pub disable_experimental: bool,
|
|
|
|
pub disabled: FxHashSet<String>,
|
2021-12-31 09:11:17 -06:00
|
|
|
pub expr_fill_default: ExprFillDefaultMode,
|
2022-08-06 09:51:51 -05:00
|
|
|
// FIXME: We may want to include a whole `AssistConfig` here
|
|
|
|
pub insert_use: InsertUseConfig,
|
2022-09-13 08:09:40 -05:00
|
|
|
pub prefer_no_std: bool,
|
2022-08-06 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticsConfig {
|
|
|
|
pub fn test_sample() -> Self {
|
|
|
|
use hir::PrefixKind;
|
|
|
|
use ide_db::imports::insert_use::ImportGranularity;
|
|
|
|
|
|
|
|
Self {
|
|
|
|
proc_macros_enabled: Default::default(),
|
|
|
|
proc_attr_macros_enabled: Default::default(),
|
|
|
|
disable_experimental: Default::default(),
|
|
|
|
disabled: Default::default(),
|
|
|
|
expr_fill_default: Default::default(),
|
|
|
|
insert_use: InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Preserve,
|
|
|
|
enforce_granularity: false,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: false,
|
|
|
|
},
|
2022-09-13 08:09:40 -05:00
|
|
|
prefer_no_std: false,
|
2022-08-06 09:51:51 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-14 05:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DiagnosticsContext<'a> {
|
|
|
|
config: &'a DiagnosticsConfig,
|
|
|
|
sema: Semantics<'a, RootDatabase>,
|
|
|
|
resolve: &'a AssistResolveStrategy,
|
|
|
|
}
|
|
|
|
|
2022-11-19 03:32:32 -06:00
|
|
|
impl<'a> DiagnosticsContext<'a> {
|
|
|
|
fn resolve_precise_location(
|
|
|
|
&self,
|
|
|
|
node: &InFile<SyntaxNodePtr>,
|
|
|
|
precise_location: Option<TextRange>,
|
|
|
|
) -> TextRange {
|
|
|
|
let sema = &self.sema;
|
|
|
|
(|| {
|
|
|
|
let precise_location = precise_location?;
|
|
|
|
let root = sema.parse_or_expand(node.file_id)?;
|
|
|
|
match root.covering_element(precise_location) {
|
|
|
|
syntax::NodeOrToken::Node(it) => Some(sema.original_range(&it)),
|
|
|
|
syntax::NodeOrToken::Token(it) => {
|
|
|
|
node.with_value(it).original_file_range_opt(sema.db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
.unwrap_or_else(|| sema.diagnostics_display_range(node.clone()))
|
|
|
|
.range
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:15:05 -05:00
|
|
|
pub fn diagnostics(
|
|
|
|
db: &RootDatabase,
|
|
|
|
config: &DiagnosticsConfig,
|
|
|
|
resolve: &AssistResolveStrategy,
|
|
|
|
file_id: FileId,
|
|
|
|
) -> Vec<Diagnostic> {
|
|
|
|
let _p = profile::span("diagnostics");
|
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let parse = db.parse(file_id);
|
|
|
|
let mut res = Vec::new();
|
|
|
|
|
|
|
|
// [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
|
|
|
|
res.extend(
|
|
|
|
parse.errors().iter().take(128).map(|err| {
|
2022-12-23 12:42:58 -06:00
|
|
|
Diagnostic::new("syntax-error", format!("Syntax Error: {err}"), err.range())
|
2021-06-14 05:15:05 -05:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-08-06 09:51:51 -05:00
|
|
|
let parse = sema.parse(file_id);
|
|
|
|
|
|
|
|
for node in parse.syntax().descendants() {
|
2021-06-14 13:23:59 -05:00
|
|
|
handlers::useless_braces::useless_braces(&mut res, file_id, &node);
|
|
|
|
handlers::field_shorthand::field_shorthand(&mut res, file_id, &node);
|
2022-12-30 02:05:03 -06:00
|
|
|
handlers::json_is_not_rust::json_in_items(&sema, &mut res, file_id, &node, config);
|
2021-06-14 05:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let module = sema.to_module_def(file_id);
|
|
|
|
|
|
|
|
let ctx = DiagnosticsContext { config, sema, resolve };
|
|
|
|
if module.is_none() {
|
2021-06-14 13:23:59 -05:00
|
|
|
handlers::unlinked_file::unlinked_file(&ctx, &mut res, file_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut diags = Vec::new();
|
|
|
|
if let Some(m) = module {
|
|
|
|
m.diagnostics(db, &mut diags)
|
2021-06-14 05:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for diag in diags {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let d = match diag {
|
2023-03-03 11:04:24 -06:00
|
|
|
AnyDiagnostic::ExpectedFunction(d) => handlers::expected_function::expected_function(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => continue,
|
|
|
|
}
|
2023-01-20 16:09:35 -06:00
|
|
|
AnyDiagnostic::IncoherentImpl(d) => handlers::incoherent_impl::incoherent_impl(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::IncorrectCase(d) => handlers::incorrect_case::incorrect_case(&ctx, &d),
|
|
|
|
AnyDiagnostic::InvalidDeriveTarget(d) => handlers::invalid_derive_target::invalid_derive_target(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::MacroError(d) => handlers::macro_error::macro_error(&ctx, &d),
|
2021-11-19 06:17:35 -06:00
|
|
|
AnyDiagnostic::MalformedDerive(d) => handlers::malformed_derive::malformed_derive(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::MismatchedArgCount(d) => handlers::mismatched_arg_count::mismatched_arg_count(&ctx, &d),
|
|
|
|
AnyDiagnostic::MissingFields(d) => handlers::missing_fields::missing_fields(&ctx, &d),
|
|
|
|
AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d),
|
|
|
|
AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::NeedMut(d) => handlers::mutability_errors::need_mut(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d),
|
2023-01-01 06:24:48 -06:00
|
|
|
AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d),
|
2022-12-31 07:20:59 -06:00
|
|
|
AnyDiagnostic::PrivateField(d) => handlers::private_field::private_field(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
|
2022-03-20 10:26:48 -05:00
|
|
|
AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::UnimplementedBuiltinMacro(d) => handlers::unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::UnreachableLabel(d) => handlers::unreachable_label:: unreachable_label(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::UnresolvedExternCrate(d) => handlers::unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::UnresolvedField(d) => handlers::unresolved_field::unresolved_field(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
|
|
|
|
AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
|
2023-04-06 05:50:16 -05:00
|
|
|
AnyDiagnostic::UnresolvedMethodCall(d) => handlers::unresolved_method::unresolved_method(&ctx, &d),
|
2021-06-14 11:32:39 -05:00
|
|
|
AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
|
2022-06-14 03:40:57 -05:00
|
|
|
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled),
|
2023-02-21 13:00:38 -06:00
|
|
|
AnyDiagnostic::UnusedMut(d) => handlers::mutability_errors::unused_mut(&ctx, &d),
|
2023-04-06 08:37:53 -05:00
|
|
|
AnyDiagnostic::BreakOutsideOfLoop(d) => handlers::break_outside_of_loop::break_outside_of_loop(&ctx, &d),
|
2021-06-14 05:15:05 -05:00
|
|
|
};
|
|
|
|
res.push(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.retain(|d| {
|
|
|
|
!ctx.config.disabled.contains(d.code.as_str())
|
|
|
|
&& !(ctx.config.disable_experimental && d.experimental)
|
|
|
|
});
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fix(id: &'static str, label: &str, source_change: SourceChange, target: TextRange) -> Assist {
|
|
|
|
let mut res = unresolved_fix(id, label, target);
|
|
|
|
res.source_change = Some(source_change);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
|
|
|
|
assert!(!id.contains(' '));
|
|
|
|
Assist {
|
|
|
|
id: AssistId(id, AssistKind::QuickFix),
|
2021-08-29 04:31:42 -05:00
|
|
|
label: Label::new(label.to_string()),
|
2021-06-14 05:15:05 -05:00
|
|
|
group: None,
|
|
|
|
target,
|
|
|
|
source_change: None,
|
2022-04-19 11:37:18 -05:00
|
|
|
trigger_signature_help: false,
|
2021-06-14 05:15:05 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-23 16:16:36 -05:00
|
|
|
|
|
|
|
fn adjusted_display_range<N: AstNode>(
|
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
diag_ptr: InFile<SyntaxNodePtr>,
|
|
|
|
adj: &dyn Fn(N) -> Option<TextRange>,
|
|
|
|
) -> TextRange {
|
|
|
|
let FileRange { file_id, range } = ctx.sema.diagnostics_display_range(diag_ptr);
|
|
|
|
|
|
|
|
let source_file = ctx.sema.db.parse(file_id);
|
|
|
|
find_node_at_range::<N>(&source_file.syntax_node(), range)
|
|
|
|
.filter(|it| it.syntax().text_range() == range)
|
|
|
|
.and_then(adj)
|
|
|
|
.unwrap_or(range)
|
|
|
|
}
|