Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LSP diagnostics now have "unnecessary" and "deprecated" tags #5878

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct CustomDiagnostic {
pub secondaries: Vec<CustomLabel>,
notes: Vec<String>,
pub kind: DiagnosticKind,
pub deprecated: bool,
pub unnecessary: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -35,6 +37,8 @@ impl CustomDiagnostic {
secondaries: Vec::new(),
notes: Vec::new(),
kind: DiagnosticKind::Error,
deprecated: false,
unnecessary: false,
}
}

Expand All @@ -49,6 +53,8 @@ impl CustomDiagnostic {
secondaries: vec![CustomLabel::new(secondary_message, secondary_span, None)],
notes: Vec::new(),
kind,
deprecated: false,
unnecessary: false,
}
}

Expand Down Expand Up @@ -101,6 +107,8 @@ impl CustomDiagnostic {
secondaries: vec![CustomLabel::new(secondary_message, secondary_span, None)],
notes: Vec::new(),
kind: DiagnosticKind::Bug,
deprecated: false,
unnecessary: false,
}
}

Expand Down
12 changes: 8 additions & 4 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,24 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
ResolverError::UnusedVariable { ident } => {
let name = &ident.0.contents;

Diagnostic::simple_warning(
let mut diagnostic = Diagnostic::simple_warning(
format!("unused variable {name}"),
"unused variable ".to_string(),
ident.span(),
)
);
diagnostic.unnecessary = true;
diagnostic
}
ResolverError::UnusedImport { ident } => {
let name = &ident.0.contents;

Diagnostic::simple_warning(
let mut diagnostic = Diagnostic::simple_warning(
format!("unused import {name}"),
"unused import ".to_string(),
ident.span(),
)
);
diagnostic.unnecessary = true;
diagnostic
}
ResolverError::VariableNotDeclared { name, span } => Diagnostic::simple_error(
format!("cannot find `{name}` in this scope "),
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ impl<'a> From<&'a TypeCheckError> for Diagnostic {
let primary_message = error.to_string();
let secondary_message = note.clone().unwrap_or_default();

Diagnostic::simple_warning(primary_message, secondary_message, *span)
let mut diagnostic = Diagnostic::simple_warning(primary_message, secondary_message, *span);
diagnostic.deprecated = true;
diagnostic
}
TypeCheckError::UnusedResultError { expr_type, expr_span } => {
let msg = format!("Unused expression result of type {expr_type}");
Expand Down
65 changes: 37 additions & 28 deletions compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,42 +165,51 @@ impl std::fmt::Display for ParserError {
impl<'a> From<&'a ParserError> for Diagnostic {
fn from(error: &'a ParserError) -> Diagnostic {
match &error.reason {
Some(reason) => {
match reason {
ParserErrorReason::ConstrainDeprecated => Diagnostic::simple_error(
Some(reason) => match reason {
ParserErrorReason::ConstrainDeprecated => {
let mut diagnostic = Diagnostic::simple_error(
"Use of deprecated keyword 'constrain'".into(),
"The 'constrain' keyword is deprecated. Please use the 'assert' function instead.".into(),
error.span,
),
ParserErrorReason::ComptimeDeprecated => Diagnostic::simple_warning(
);
diagnostic.deprecated = true;
diagnostic
}
ParserErrorReason::ComptimeDeprecated => {
let mut diagnostic = Diagnostic::simple_warning(
"Use of deprecated keyword 'comptime'".into(),
"The 'comptime' keyword has been deprecated. It can be removed without affecting your program".into(),
error.span,
) ;
diagnostic.deprecated = true;
diagnostic
}
ParserErrorReason::InvalidBitSize(bit_size) => Diagnostic::simple_error(
format!("Use of invalid bit size {}", bit_size),
format!(
"Allowed bit sizes for integers are {}",
IntegerBitSize::allowed_sizes()
.iter()
.map(|n| n.to_string())
.collect::<Vec<_>>()
.join(", ")
),
ParserErrorReason::InvalidBitSize(bit_size) => Diagnostic::simple_error(
format!("Use of invalid bit size {}", bit_size),
format!("Allowed bit sizes for integers are {}", IntegerBitSize::allowed_sizes().iter().map(|n| n.to_string()).collect::<Vec<_>>().join(", ")),
error.span,
),
ParserErrorReason::ExperimentalFeature(_) => Diagnostic::simple_warning(
reason.to_string(),
"".into(),
error.span,
),
ParserErrorReason::TraitImplFunctionModifiers => Diagnostic::simple_warning(
reason.to_string(),
"".into(),
error.span,
),
ParserErrorReason::ExpectedPatternButFoundType(ty) => {
Diagnostic::simple_error("Expected a ; separating these two statements".into(), format!("{ty} is a type and cannot be used as a variable name"), error.span)
}
ParserErrorReason::Lexer(error) => error.into(),
other => {
Diagnostic::simple_error(format!("{other}"), String::new(), error.span)
}
error.span,
),
ParserErrorReason::ExperimentalFeature(_) => {
Diagnostic::simple_warning(reason.to_string(), "".into(), error.span)
}
}
ParserErrorReason::TraitImplFunctionModifiers => {
Diagnostic::simple_warning(reason.to_string(), "".into(), error.span)
}
ParserErrorReason::ExpectedPatternButFoundType(ty) => Diagnostic::simple_error(
"Expected a ; separating these two statements".into(),
format!("{ty} is a type and cannot be used as a variable name"),
error.span,
),
ParserErrorReason::Lexer(error) => error.into(),
other => Diagnostic::simple_error(format!("{other}"), String::new(), error.span),
},
None => {
let primary = error.to_string();
Diagnostic::simple_error(primary, String::new(), error.span)
Expand Down
11 changes: 11 additions & 0 deletions tooling/lsp/src/notifications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ops::ControlFlow;

use crate::insert_all_files_for_workspace_into_file_manager;
use async_lsp::{ErrorCode, LanguageClient, ResponseError};
use lsp_types::DiagnosticTag;
use noirc_driver::{check_crate, file_manager_with_stdlib, CheckOptions};
use noirc_errors::{DiagnosticKind, FileDiagnostic};

Expand Down Expand Up @@ -189,10 +190,20 @@ pub(crate) fn process_workspace_for_noir_document(
DiagnosticKind::Info => DiagnosticSeverity::INFORMATION,
DiagnosticKind::Bug => DiagnosticSeverity::WARNING,
};

let mut tags = Vec::new();
if diagnostic.unnecessary {
tags.push(DiagnosticTag::UNNECESSARY);
}
if diagnostic.deprecated {
tags.push(DiagnosticTag::DEPRECATED);
}

Some(Diagnostic {
range,
severity: Some(severity),
message: diagnostic.message,
tags: if tags.is_empty() { None } else { Some(tags) },
..Default::default()
})
})
Expand Down
Loading