diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index a70d9fc72b6..7295e46ea7a 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -10,7 +10,7 @@ use clap::Args; use fm::{FileId, FileManager}; use iter_extended::vecmap; use noirc_abi::{AbiParameter, AbiType, AbiValue}; -use noirc_errors::{CustomDiagnostic, DiagnosticKind, FileDiagnostic}; +use noirc_errors::{CustomDiagnostic, DiagnosticKind}; use noirc_evaluator::brillig::BrilligOptions; use noirc_evaluator::create_program; use noirc_evaluator::errors::RuntimeError; @@ -227,8 +227,8 @@ impl From for CompileError { } } -impl From for FileDiagnostic { - fn from(error: CompileError) -> FileDiagnostic { +impl From for CustomDiagnostic { + fn from(error: CompileError) -> CustomDiagnostic { match error { CompileError::RuntimeError(err) => err.into(), CompileError::MonomorphizationError(err) => err.into(), @@ -237,10 +237,10 @@ impl From for FileDiagnostic { } /// Helper type used to signify where only warnings are expected in file diagnostics -pub type Warnings = Vec; +pub type Warnings = Vec; /// Helper type used to signify where errors or warnings are expected in file diagnostics -pub type ErrorsAndWarnings = Vec; +pub type ErrorsAndWarnings = Vec; /// Helper type for connecting a compilation artifact to the errors or warnings which were produced during compilation. pub type CompilationResult = Result<(T, Warnings), ErrorsAndWarnings>; @@ -350,20 +350,16 @@ pub fn check_crate( ) -> CompilationResult<()> { let diagnostics = CrateDefMap::collect_defs(crate_id, context, options.frontend_options()); let crate_files = context.crate_files(&crate_id); - let warnings_and_errors: Vec = diagnostics - .into_iter() - .map(|error| { - let location = error.location(); - let diagnostic = CustomDiagnostic::from(&error); - diagnostic.in_file(location.file) - }) + let warnings_and_errors: Vec = diagnostics + .iter() + .map(CustomDiagnostic::from) .filter(|diagnostic| { // We filter out any warnings if they're going to be ignored later on to free up memory. - !options.silence_warnings || diagnostic.diagnostic.kind != DiagnosticKind::Warning + !options.silence_warnings || diagnostic.kind != DiagnosticKind::Warning }) .filter(|error| { // Only keep warnings from the crate we are checking - if error.diagnostic.is_warning() { crate_files.contains(&error.file_id) } else { true } + if error.is_warning() { crate_files.contains(&error.file) } else { true } }) .collect(); @@ -401,16 +397,16 @@ pub fn compile_main( // TODO(#2155): This error might be a better to exist in Nargo let err = CustomDiagnostic::from_message( "cannot compile crate into a program as it does not contain a `main` function", - ) - .in_file(FileId::default()); + FileId::default(), + ); vec![err] })?; let compiled_program = compile_no_check(context, options, main, cached_program, options.force_compile) - .map_err(FileDiagnostic::from)?; + .map_err(|error| vec![CustomDiagnostic::from(error)])?; - let compilation_warnings = vecmap(compiled_program.warnings.clone(), FileDiagnostic::from); + let compilation_warnings = vecmap(compiled_program.warnings.clone(), CustomDiagnostic::from); if options.deny_warnings && !compilation_warnings.is_empty() { return Err(compilation_warnings); } @@ -439,14 +435,16 @@ pub fn compile_contract( let mut errors = warnings; if contracts.len() > 1 { - let err = CustomDiagnostic::from_message("Packages are limited to a single contract") - .in_file(FileId::default()); + let err = CustomDiagnostic::from_message( + "Packages are limited to a single contract", + FileId::default(), + ); return Err(vec![err]); } else if contracts.is_empty() { let err = CustomDiagnostic::from_message( "cannot compile crate into a contract as it does not contain any contracts", - ) - .in_file(FileId::default()); + FileId::default(), + ); return Err(vec![err]); }; @@ -483,12 +481,8 @@ pub fn compile_contract( } /// True if there are (non-warning) errors present and we should halt compilation -fn has_errors(errors: &[FileDiagnostic], deny_warnings: bool) -> bool { - if deny_warnings { - !errors.is_empty() - } else { - errors.iter().any(|error| error.diagnostic.is_error()) - } +fn has_errors(errors: &[CustomDiagnostic], deny_warnings: bool) -> bool { + if deny_warnings { !errors.is_empty() } else { errors.iter().any(|error| error.is_error()) } } /// Compile all of the functions associated with a Noir contract. @@ -525,7 +519,7 @@ fn compile_contract_inner( let function = match compile_no_check(context, &options, function_id, None, true) { Ok(function) => function, Err(new_error) => { - errors.push(FileDiagnostic::from(new_error)); + errors.push(new_error.into()); continue; } }; diff --git a/compiler/noirc_driver/tests/contracts.rs b/compiler/noirc_driver/tests/contracts.rs index ea42cb23376..0732a7728ca 100644 --- a/compiler/noirc_driver/tests/contracts.rs +++ b/compiler/noirc_driver/tests/contracts.rs @@ -33,10 +33,10 @@ contract Bar {}"; assert_eq!( errors, - vec![ - CustomDiagnostic::from_message("Packages are limited to a single contract") - .in_file(FileId::default()) - ], + vec![CustomDiagnostic::from_message( + "Packages are limited to a single contract", + FileId::default() + )], "stdlib is producing warnings" ); diff --git a/compiler/noirc_errors/src/lib.rs b/compiler/noirc_errors/src/lib.rs index 146217f91a0..91d121603ba 100644 --- a/compiler/noirc_errors/src/lib.rs +++ b/compiler/noirc_errors/src/lib.rs @@ -8,21 +8,3 @@ mod position; pub mod reporter; pub use position::{Located, Location, Position, Span, Spanned}; pub use reporter::{CustomDiagnostic, DiagnosticKind}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FileDiagnostic { - pub file_id: fm::FileId, - pub diagnostic: CustomDiagnostic, -} - -impl FileDiagnostic { - pub fn new(file_id: fm::FileId, diagnostic: CustomDiagnostic) -> FileDiagnostic { - FileDiagnostic { file_id, diagnostic } - } -} - -impl From for Vec { - fn from(value: FileDiagnostic) -> Self { - vec![value] - } -} diff --git a/compiler/noirc_errors/src/reporter.rs b/compiler/noirc_errors/src/reporter.rs index e516f690ddc..d406e897d65 100644 --- a/compiler/noirc_errors/src/reporter.rs +++ b/compiler/noirc_errors/src/reporter.rs @@ -1,6 +1,6 @@ use std::io::IsTerminal; -use crate::{FileDiagnostic, Location, Span}; +use crate::{Location, Span}; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::Files; use codespan_reporting::term; @@ -8,6 +8,7 @@ use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct CustomDiagnostic { + pub file: fm::FileId, pub message: String, pub secondaries: Vec, pub notes: Vec, @@ -35,8 +36,9 @@ pub struct ReportedErrors { } impl CustomDiagnostic { - pub fn from_message(msg: &str) -> CustomDiagnostic { + pub fn from_message(msg: &str, file: fm::FileId) -> CustomDiagnostic { Self { + file, message: msg.to_owned(), secondaries: Vec::new(), notes: Vec::new(), @@ -54,6 +56,7 @@ impl CustomDiagnostic { kind: DiagnosticKind, ) -> CustomDiagnostic { CustomDiagnostic { + file: secondary_location.file, message: primary_message, secondaries: vec![CustomLabel::new(secondary_message, secondary_location)], notes: Vec::new(), @@ -109,6 +112,7 @@ impl CustomDiagnostic { secondary_location: Location, ) -> CustomDiagnostic { CustomDiagnostic { + file: secondary_location.file, message: primary_message, secondaries: vec![CustomLabel::new(secondary_message, secondary_location)], notes: Vec::new(), @@ -119,10 +123,6 @@ impl CustomDiagnostic { } } - pub fn in_file(self, file_id: fm::FileId) -> FileDiagnostic { - FileDiagnostic::new(file_id, self) - } - pub fn with_call_stack(mut self, call_stack: Vec) -> Self { self.call_stack = call_stack; self @@ -185,16 +185,16 @@ impl CustomLabel { /// of diagnostics that were errors. pub fn report_all<'files>( files: &'files impl Files<'files, FileId = fm::FileId>, - diagnostics: &[FileDiagnostic], + diagnostics: &[CustomDiagnostic], deny_warnings: bool, silence_warnings: bool, ) -> ReportedErrors { // Report warnings before any errors let (warnings_and_bugs, mut errors): (Vec<_>, _) = - diagnostics.iter().partition(|item| !item.diagnostic.is_error()); + diagnostics.iter().partition(|item| !item.is_error()); let (warnings, mut bugs): (Vec<_>, _) = - warnings_and_bugs.iter().partition(|item| item.diagnostic.is_warning()); + warnings_and_bugs.iter().partition(|item| item.is_warning()); let mut diagnostics = if silence_warnings { Vec::new() } else { warnings }; diagnostics.append(&mut bugs); diagnostics.append(&mut errors); @@ -205,14 +205,14 @@ pub fn report_all<'files>( ReportedErrors { error_count } } -impl FileDiagnostic { +impl CustomDiagnostic { /// Print the report; return true if it was an error. pub fn report<'files>( &self, files: &'files impl Files<'files, FileId = fm::FileId>, deny_warnings: bool, ) -> bool { - report(files, &self.diagnostic, deny_warnings) + report(files, self, deny_warnings) } } diff --git a/compiler/noirc_evaluator/src/errors.rs b/compiler/noirc_evaluator/src/errors.rs index 202124f7931..deaefd40ae3 100644 --- a/compiler/noirc_evaluator/src/errors.rs +++ b/compiler/noirc_evaluator/src/errors.rs @@ -8,7 +8,7 @@ //! //! An Error of the latter is an error in the implementation of the compiler use iter_extended::vecmap; -use noirc_errors::{CustomDiagnostic as Diagnostic, FileDiagnostic, Location}; +use noirc_errors::{CustomDiagnostic, Location}; use noirc_frontend::signed_field::SignedField; use thiserror::Error; @@ -73,8 +73,8 @@ pub enum SsaReport { Bug(InternalBug), } -impl From for FileDiagnostic { - fn from(error: SsaReport) -> FileDiagnostic { +impl From for CustomDiagnostic { + fn from(error: SsaReport) -> CustomDiagnostic { match error { SsaReport::Warning(warning) => { let message = warning.to_string(); @@ -87,10 +87,10 @@ impl From for FileDiagnostic { }, }; let call_stack = vecmap(call_stack, |location| location); - let file_id = call_stack.last().map(|location| location.file).unwrap_or_default(); let location = call_stack.last().expect("Expected RuntimeError to have a location"); - let diagnostic = Diagnostic::simple_warning(message, secondary_message, *location); - diagnostic.with_call_stack(call_stack).in_file(file_id) + let diagnostic = + CustomDiagnostic::simple_warning(message, secondary_message, *location); + diagnostic.with_call_stack(call_stack) } SsaReport::Bug(bug) => { let message = bug.to_string(); @@ -104,10 +104,10 @@ impl From for FileDiagnostic { InternalBug::AssertFailed { call_stack } => ("As a result, the compiled circuit is ensured to fail. Other assertions may also fail during execution".to_string(), call_stack) }; let call_stack = vecmap(call_stack, |location| location); - let file_id = call_stack.last().map(|location| location.file).unwrap_or_default(); let location = call_stack.last().expect("Expected RuntimeError to have a location"); - let diagnostic = Diagnostic::simple_bug(message, secondary_message, *location); - diagnostic.with_call_stack(call_stack).in_file(file_id) + let diagnostic = + CustomDiagnostic::simple_bug(message, secondary_message, *location); + diagnostic.with_call_stack(call_stack) } } } @@ -181,20 +181,19 @@ impl RuntimeError { } } -impl From for FileDiagnostic { - fn from(error: RuntimeError) -> FileDiagnostic { +impl From for CustomDiagnostic { + fn from(error: RuntimeError) -> CustomDiagnostic { let call_stack = vecmap(error.call_stack(), |location| *location); - let file_id = call_stack.last().map(|location| location.file).unwrap_or_default(); let diagnostic = error.into_diagnostic(); - diagnostic.with_call_stack(call_stack).in_file(file_id) + diagnostic.with_call_stack(call_stack) } } impl RuntimeError { - fn into_diagnostic(self) -> Diagnostic { + fn into_diagnostic(self) -> CustomDiagnostic { match self { RuntimeError::InternalError(cause) => { - Diagnostic::simple_error( + CustomDiagnostic::simple_error( "Internal Consistency Evaluators Errors: \n This is likely a bug. Consider opening an issue at https://github.com/noir-lang/noir/issues".to_owned(), cause.to_string(), @@ -206,7 +205,7 @@ impl RuntimeError { let location = self.call_stack().last().expect("Expected RuntimeError to have a location"); - Diagnostic::simple_error( + CustomDiagnostic::simple_error( primary_message, "If attempting to fetch the length of a slice, try converting to an array. Slices only use dynamic lengths.".to_string(), *location, @@ -217,7 +216,7 @@ impl RuntimeError { let location = self.call_stack().last().unwrap_or_else(|| panic!("Expected RuntimeError to have a location. Error message: {message}")); - Diagnostic::simple_error(message, String::new(), *location) + CustomDiagnostic::simple_error(message, String::new(), *location) } } } diff --git a/compiler/noirc_frontend/src/hir/def_collector/errors.rs b/compiler/noirc_frontend/src/hir/def_collector/errors.rs index 4e8ed6233c5..239c7a7ee12 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/errors.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/errors.rs @@ -2,7 +2,6 @@ use crate::ast::{Ident, ItemVisibility, Path, UnsupportedNumericGenericType}; use crate::hir::resolution::import::PathResolutionError; use crate::hir::type_check::generics::TraitGenerics; -use noirc_errors::FileDiagnostic; use noirc_errors::{CustomDiagnostic as Diagnostic, Location}; use thiserror::Error; @@ -76,10 +75,6 @@ pub enum DefCollectorErrorKind { } impl DefCollectorErrorKind { - pub fn into_file_diagnostic(&self, file: fm::FileId) -> FileDiagnostic { - Diagnostic::from(self).in_file(file) - } - pub fn location(&self) -> Location { match self { DefCollectorErrorKind::Duplicate { first_def: ident, .. } diff --git a/compiler/noirc_frontend/src/hir/resolution/errors.rs b/compiler/noirc_frontend/src/hir/resolution/errors.rs index 7bd5c79dc90..ce32350b8e2 100644 --- a/compiler/noirc_frontend/src/hir/resolution/errors.rs +++ b/compiler/noirc_frontend/src/hir/resolution/errors.rs @@ -1,6 +1,6 @@ use acvm::FieldElement; pub use noirc_errors::Span; -use noirc_errors::{CustomDiagnostic as Diagnostic, FileDiagnostic, Location}; +use noirc_errors::{CustomDiagnostic as Diagnostic, Location}; use thiserror::Error; use crate::{ @@ -195,10 +195,6 @@ pub enum ResolverError { } impl ResolverError { - pub fn into_file_diagnostic(&self, file: fm::FileId) -> FileDiagnostic { - Diagnostic::from(self).in_file(file) - } - pub fn location(&self) -> Location { match self { ResolverError::DuplicateDefinition { first_location: location, .. } diff --git a/compiler/noirc_frontend/src/hir/type_check/errors.rs b/compiler/noirc_frontend/src/hir/type_check/errors.rs index f3c2c635b04..cba5c1f791d 100644 --- a/compiler/noirc_frontend/src/hir/type_check/errors.rs +++ b/compiler/noirc_frontend/src/hir/type_check/errors.rs @@ -648,15 +648,15 @@ impl<'a> From<&'a TypeCheckError> for Diagnostic { impl<'a> From<&'a NoMatchingImplFoundError> for Diagnostic { fn from(error: &'a NoMatchingImplFoundError) -> Self { let constraints = &error.constraints; - let span = error.location; + let location = error.location; assert!(!constraints.is_empty()); let msg = format!("No matching impl found for `{}: {}`", constraints[0].0, constraints[0].1); - let mut diagnostic = Diagnostic::from_message(&msg); + let mut diagnostic = Diagnostic::from_message(&msg, location.file); let secondary = format!("No impl for `{}: {}`", constraints[0].0, constraints[0].1); - diagnostic.add_secondary(secondary, span); + diagnostic.add_secondary(secondary, location); // These must be notes since secondaries are unordered for (typ, trait_name) in &constraints[1..] { diff --git a/compiler/noirc_frontend/src/monomorphization/errors.rs b/compiler/noirc_frontend/src/monomorphization/errors.rs index 32407f29cd0..93a12a46591 100644 --- a/compiler/noirc_frontend/src/monomorphization/errors.rs +++ b/compiler/noirc_frontend/src/monomorphization/errors.rs @@ -1,4 +1,4 @@ -use noirc_errors::{CustomDiagnostic, FileDiagnostic, Location}; +use noirc_errors::{CustomDiagnostic, Location}; use crate::{ Type, @@ -34,18 +34,9 @@ impl MonomorphizationError { } } -impl From for FileDiagnostic { - fn from(error: MonomorphizationError) -> FileDiagnostic { - let location = error.location(); - let call_stack = vec![location]; - let diagnostic = error.into_diagnostic(); - diagnostic.with_call_stack(call_stack).in_file(location.file) - } -} - -impl MonomorphizationError { - fn into_diagnostic(self) -> CustomDiagnostic { - let message = match &self { +impl From for CustomDiagnostic { + fn from(error: MonomorphizationError) -> CustomDiagnostic { + let message = match &error { MonomorphizationError::UnknownArrayLength { length, err, .. } => { format!("Could not determine array length `{length}`, encountered error: `{err}`") } @@ -78,7 +69,7 @@ impl MonomorphizationError { } }; - let location = self.location(); + let location = error.location(); CustomDiagnostic::simple_error(message, String::new(), location) } } diff --git a/compiler/wasm/src/compile.rs b/compiler/wasm/src/compile.rs index 021462d9f46..d31b7ec47c7 100644 --- a/compiler/wasm/src/compile.rs +++ b/compiler/wasm/src/compile.rs @@ -176,7 +176,7 @@ pub fn compile_program( let compiled_program = noirc_driver::compile_main(&mut context, crate_id, &compile_options, None) .map_err(|errs| { - CompileError::with_file_diagnostics( + CompileError::with_custom_diagnostics( "Failed to compile program", errs, &context.file_manager, @@ -186,7 +186,7 @@ pub fn compile_program( let optimized_program = nargo::ops::transform_program(compiled_program, expression_width); nargo::ops::check_program(&optimized_program).map_err(|errs| { - CompileError::with_file_diagnostics( + CompileError::with_custom_diagnostics( "Compiled program is not solvable", errs, &context.file_manager, @@ -212,8 +212,8 @@ pub fn compile_contract( let compiled_contract = noirc_driver::compile_contract(&mut context, crate_id, &compile_options) - .map_err(|errs: Vec| { - CompileError::with_file_diagnostics( + .map_err(|errs: Vec| { + CompileError::with_custom_diagnostics( "Failed to compile contract", errs, &context.file_manager, diff --git a/compiler/wasm/src/compile_new.rs b/compiler/wasm/src/compile_new.rs index e7c2e94cd84..37065c8f825 100644 --- a/compiler/wasm/src/compile_new.rs +++ b/compiler/wasm/src/compile_new.rs @@ -109,7 +109,7 @@ impl CompilerContext { let compiled_program = compile_main(&mut self.context, root_crate_id, &compile_options, None) .map_err(|errs| { - CompileError::with_file_diagnostics( + CompileError::with_custom_diagnostics( "Failed to compile program", errs, &self.context.file_manager, @@ -119,7 +119,7 @@ impl CompilerContext { let optimized_program = nargo::ops::transform_program(compiled_program, expression_width); nargo::ops::check_program(&optimized_program).map_err(|errs| { - CompileError::with_file_diagnostics( + CompileError::with_custom_diagnostics( "Compiled program is not solvable", errs, &self.context.file_manager, @@ -148,7 +148,7 @@ impl CompilerContext { let compiled_contract = compile_contract(&mut self.context, root_crate_id, &compile_options) .map_err(|errs| { - CompileError::with_file_diagnostics( + CompileError::with_custom_diagnostics( "Failed to compile contract", errs, &self.context.file_manager, diff --git a/compiler/wasm/src/errors.rs b/compiler/wasm/src/errors.rs index c2e51162d3f..47927df1056 100644 --- a/compiler/wasm/src/errors.rs +++ b/compiler/wasm/src/errors.rs @@ -4,7 +4,7 @@ use serde::Serialize; use wasm_bindgen::prelude::*; use fm::FileManager; -use noirc_errors::FileDiagnostic; +use noirc_errors::CustomDiagnostic; #[wasm_bindgen(typescript_custom_section)] const DIAGNOSTICS: &'static str = r#" @@ -87,8 +87,7 @@ pub struct Diagnostic { } impl Diagnostic { - fn new(file_diagnostic: &FileDiagnostic, file: String) -> Diagnostic { - let diagnostic = &file_diagnostic.diagnostic; + fn new(diagnostic: &CustomDiagnostic, file: String) -> Diagnostic { let message = diagnostic.message.clone(); let secondaries = diagnostic @@ -116,16 +115,16 @@ impl CompileError { CompileError { message: message.to_string(), diagnostics: vec![] } } - pub fn with_file_diagnostics( + pub fn with_custom_diagnostics( message: &str, - file_diagnostics: Vec, + custom_diagnostics: Vec, file_manager: &FileManager, ) -> CompileError { - let diagnostics: Vec<_> = file_diagnostics + let diagnostics: Vec<_> = custom_diagnostics .iter() .map(|err| { let file_path = file_manager - .path(err.file_id) + .path(err.file) .expect("File must exist to have caused diagnostics"); Diagnostic::new(err, file_path.to_str().unwrap().to_string()) }) diff --git a/tooling/lsp/src/notifications/mod.rs b/tooling/lsp/src/notifications/mod.rs index dd3315705fc..b7ba8cd4761 100644 --- a/tooling/lsp/src/notifications/mod.rs +++ b/tooling/lsp/src/notifications/mod.rs @@ -11,7 +11,7 @@ use fxhash::FxHashMap as HashMap; use lsp_types::{DiagnosticRelatedInformation, DiagnosticTag, Url}; use noirc_driver::check_crate; use noirc_errors::reporter::CustomLabel; -use noirc_errors::{DiagnosticKind, FileDiagnostic, Location}; +use noirc_errors::{CustomDiagnostic, DiagnosticKind, Location}; use crate::types::{ Diagnostic, DiagnosticSeverity, DidChangeConfigurationParams, DidChangeTextDocumentParams, @@ -191,16 +191,16 @@ fn publish_diagnostics( package_root_dir: &PathBuf, files: &FileMap, fm: &FileManager, - file_diagnostics: Vec, + custom_diagnostics: Vec, ) { let mut diagnostics_per_url: HashMap> = HashMap::default(); - for file_diagnostic in file_diagnostics.into_iter() { - let file_id = file_diagnostic.file_id; - let path = fm.path(file_id).expect("file must exist to have emitted diagnostic"); + for custom_diagnostic in custom_diagnostics.into_iter() { + let file = custom_diagnostic.file; + let path = fm.path(file).expect("file must exist to have emitted diagnostic"); if let Ok(uri) = Url::from_file_path(path) { if let Some(diagnostic) = - file_diagnostic_to_diagnostic(file_diagnostic, files, fm, uri.clone()) + custom_diagnostic_to_diagnostic(custom_diagnostic, files, fm, uri.clone()) { diagnostics_per_url.entry(uri).or_default().push(diagnostic); } @@ -232,21 +232,18 @@ fn publish_diagnostics( state.files_with_errors.insert(package_root_dir.clone(), new_files_with_errors); } -fn file_diagnostic_to_diagnostic( - file_diagnostic: FileDiagnostic, +fn custom_diagnostic_to_diagnostic( + diagnostic: CustomDiagnostic, files: &FileMap, fm: &FileManager, uri: Url, ) -> Option { - let file_id = file_diagnostic.file_id; - let diagnostic = file_diagnostic.diagnostic; - if diagnostic.secondaries.is_empty() { return None; } let span = diagnostic.secondaries.first().unwrap().location.span; - let range = byte_span_to_range(files, file_id, span.into())?; + let range = byte_span_to_range(files, diagnostic.file, span.into())?; let severity = match diagnostic.kind { DiagnosticKind::Error => DiagnosticSeverity::ERROR, diff --git a/tooling/lsp/src/requests/test_run.rs b/tooling/lsp/src/requests/test_run.rs index f0d49c4d864..6b7a53e4596 100644 --- a/tooling/lsp/src/requests/test_run.rs +++ b/tooling/lsp/src/requests/test_run.rs @@ -119,7 +119,7 @@ fn on_test_run_request_inner( TestStatus::CompileError(diag) => NargoTestRunResult { id: params.id.clone(), result: "error".to_string(), - message: Some(diag.diagnostic.message), + message: Some(diag.message), }, }; Ok(result) diff --git a/tooling/nargo/src/errors.rs b/tooling/nargo/src/errors.rs index cdf11a0e9e7..f1743af79ca 100644 --- a/tooling/nargo/src/errors.rs +++ b/tooling/nargo/src/errors.rs @@ -9,9 +9,7 @@ use acvm::{ pwg::{ErrorLocation, OpcodeResolutionError}, }; use noirc_abi::{Abi, AbiErrorType, display_abi_error}; -use noirc_errors::{ - CustomDiagnostic, FileDiagnostic, debug_info::DebugInfo, reporter::ReportedErrors, -}; +use noirc_errors::{CustomDiagnostic, debug_info::DebugInfo, reporter::ReportedErrors}; pub use noirc_errors::Location; @@ -230,7 +228,7 @@ pub fn try_to_diagnose_runtime_error( nargo_err: &NargoError, abi: &Abi, debug: &[DebugInfo], -) -> Option { +) -> Option { let source_locations = match nargo_err { NargoError::ExecutionError(execution_error) => { extract_locations_from_error(execution_error, debug)? @@ -242,5 +240,5 @@ pub fn try_to_diagnose_runtime_error( let location = *source_locations.last()?; let message = extract_message_from_error(&abi.error_types, nargo_err); let error = CustomDiagnostic::simple_error(message, String::new(), location); - Some(error.with_call_stack(source_locations).in_file(location.file)) + Some(error.with_call_stack(source_locations)) } diff --git a/tooling/nargo/src/ops/check.rs b/tooling/nargo/src/ops/check.rs index f22def8bd91..129aa1bd788 100644 --- a/tooling/nargo/src/ops/check.rs +++ b/tooling/nargo/src/ops/check.rs @@ -1,6 +1,6 @@ use acvm::compiler::CircuitSimulator; use noirc_driver::{CompiledProgram, ErrorsAndWarnings}; -use noirc_errors::{CustomDiagnostic, FileDiagnostic}; +use noirc_errors::CustomDiagnostic; /// Run each function through a circuit simulator to check that they are solvable. #[tracing::instrument(level = "trace", skip_all)] @@ -8,13 +8,10 @@ pub fn check_program(compiled_program: &CompiledProgram) -> Result<(), ErrorsAnd for (i, circuit) in compiled_program.program.functions.iter().enumerate() { let mut simulator = CircuitSimulator::default(); if !simulator.check_circuit(circuit) { - let diag = FileDiagnostic { - file_id: fm::FileId::dummy(), - diagnostic: CustomDiagnostic::from_message(&format!( - "Circuit \"{}\" is not solvable", - compiled_program.names[i] - )), - }; + let diag = CustomDiagnostic::from_message( + &format!("Circuit \"{}\" is not solvable", compiled_program.names[i]), + fm::FileId::dummy(), + ); return Err(vec![diag]); } } diff --git a/tooling/nargo/src/ops/test.rs b/tooling/nargo/src/ops/test.rs index 8eba3000f3d..4f0e63564e2 100644 --- a/tooling/nargo/src/ops/test.rs +++ b/tooling/nargo/src/ops/test.rs @@ -8,7 +8,7 @@ use acvm::{ }; use noirc_abi::Abi; use noirc_driver::{CompileError, CompileOptions, DEFAULT_EXPRESSION_WIDTH, compile_no_check}; -use noirc_errors::{FileDiagnostic, debug_info::DebugInfo}; +use noirc_errors::{CustomDiagnostic, debug_info::DebugInfo}; use noirc_frontend::hir::{Context, def_map::TestFunction}; use crate::{ @@ -22,9 +22,9 @@ use super::execute_program; #[derive(Debug)] pub enum TestStatus { Pass, - Fail { message: String, error_diagnostic: Option }, + Fail { message: String, error_diagnostic: Option }, Skipped, - CompileError(FileDiagnostic), + CompileError(CustomDiagnostic), } impl TestStatus { @@ -218,7 +218,7 @@ fn test_status_program_compile_pass( fn check_expected_failure_message( test_function: &TestFunction, failed_assertion: Option, - error_diagnostic: Option, + error_diagnostic: Option, ) -> TestStatus { // Extract the expected failure message, if there was one // @@ -235,9 +235,7 @@ fn check_expected_failure_message( // expected_failure_message let expected_failure_message_matches = failed_assertion .as_ref() - .or_else(|| { - error_diagnostic.as_ref().map(|file_diagnostic| &file_diagnostic.diagnostic.message) - }) + .or_else(|| error_diagnostic.as_ref().map(|file_diagnostic| &file_diagnostic.message)) .map(|message| message.contains(expected_failure_message)) .unwrap_or(false); if expected_failure_message_matches { diff --git a/tooling/nargo_cli/src/cli/export_cmd.rs b/tooling/nargo_cli/src/cli/export_cmd.rs index 9fe682bef7f..373dfce86a9 100644 --- a/tooling/nargo_cli/src/cli/export_cmd.rs +++ b/tooling/nargo_cli/src/cli/export_cmd.rs @@ -1,7 +1,7 @@ use nargo::errors::CompileError; use nargo::ops::report_errors; use noir_artifact_cli::fs::artifact::save_program_to_file; -use noirc_errors::FileDiagnostic; +use noirc_errors::CustomDiagnostic; use noirc_frontend::hir::ParsedFiles; use rayon::prelude::*; @@ -82,7 +82,7 @@ fn compile_exported_functions( |(function_name, function_id)| -> Result<(String, CompiledProgram), CompileError> { // TODO: We should to refactor how to deal with compilation errors to avoid this. let program = compile_no_check(&mut context, compile_options, function_id, None, false) - .map_err(|error| vec![FileDiagnostic::from(error)]); + .map_err(|error| vec![CustomDiagnostic::from(error)]); let program = report_errors( program.map(|program| (program, Vec::new())), diff --git a/tooling/nargo_cli/src/cli/fmt_cmd.rs b/tooling/nargo_cli/src/cli/fmt_cmd.rs index b16ce9d1f7d..53551c13950 100644 --- a/tooling/nargo_cli/src/cli/fmt_cmd.rs +++ b/tooling/nargo_cli/src/cli/fmt_cmd.rs @@ -56,11 +56,8 @@ pub(crate) fn run(args: FormatCommand, workspace: Workspace) -> Result<(), CliEr let is_all_warnings = errors.iter().all(ParserError::is_warning); if !is_all_warnings { let errors = errors - .into_iter() - .map(|error| { - let error = CustomDiagnostic::from(&error); - error.in_file(file_id) - }) + .iter() + .map(CustomDiagnostic::from) .collect(); let _ = report_errors::<()>( diff --git a/tooling/nargo_cli/src/cli/test_cmd/formatters.rs b/tooling/nargo_cli/src/cli/test_cmd/formatters.rs index b62e2e2ad9c..68628129245 100644 --- a/tooling/nargo_cli/src/cli/test_cmd/formatters.rs +++ b/tooling/nargo_cli/src/cli/test_cmd/formatters.rs @@ -2,7 +2,7 @@ use std::{io::Write, panic::RefUnwindSafe, time::Duration}; use fm::FileManager; use nargo::ops::TestStatus; -use noirc_errors::{FileDiagnostic, reporter::stack_trace}; +use noirc_errors::{CustomDiagnostic, reporter::stack_trace}; use serde_json::{Map, json}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, StandardStreamLock, WriteColor}; @@ -438,7 +438,7 @@ impl Formatter for JsonFormatter { stdout.push_str(message.trim()); if let Some(diagnostic) = error_diagnostic { - if !(diagnostic.diagnostic.is_warning() && silence_warnings) { + if !(diagnostic.is_warning() && silence_warnings) { stdout.push('\n'); stdout.push_str(&diagnostic_to_string(diagnostic, file_manager)); } @@ -450,7 +450,7 @@ impl Formatter for JsonFormatter { TestStatus::CompileError(diagnostic) => { json.insert("event".to_string(), json!("failed")); - if !(diagnostic.diagnostic.is_warning() && silence_warnings) { + if !(diagnostic.is_warning() && silence_warnings) { if !stdout.is_empty() { stdout.push('\n'); } @@ -515,12 +515,11 @@ fn package_start(package_name: &str, test_count: usize) -> std::io::Result<()> { } pub(crate) fn diagnostic_to_string( - file_diagnostic: &FileDiagnostic, + custom_diagnostic: &CustomDiagnostic, file_manager: &FileManager, ) -> String { let file_map = file_manager.as_file_map(); - let custom_diagnostic = &file_diagnostic.diagnostic; let mut message = String::new(); message.push_str(custom_diagnostic.message.trim()); @@ -529,7 +528,7 @@ pub(crate) fn diagnostic_to_string( message.push_str(note.trim()); } - if let Ok(name) = file_map.get_name(file_diagnostic.file_id) { + if let Ok(name) = file_map.get_name(custom_diagnostic.file) { message.push('\n'); message.push_str(&format!("at {name}")); }