diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index 1b311504b5c..d124376ee3c 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -311,13 +311,9 @@ pub fn check_crate( crate_id: CrateId, options: &CompileOptions, ) -> CompilationResult<()> { - let error_on_unused_imports = true; - let diagnostics = CrateDefMap::collect_defs( - crate_id, - context, - options.debug_comptime_in_file.as_deref(), - error_on_unused_imports, - ); + let diagnostics = + CrateDefMap::collect_defs(crate_id, context, options.debug_comptime_in_file.as_deref()); + let crate_files = context.crate_files(&crate_id); let warnings_and_errors: Vec = diagnostics .into_iter() .map(|(error, file_id)| { @@ -328,6 +324,14 @@ pub fn check_crate( // 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 }) + .filter(|error| { + // Only keep warnings from the crate we are checking + if error.diagnostic.is_warning() { + crate_files.contains(&error.file_id) + } else { + true + } + }) .collect(); if has_errors(&warnings_and_errors, options.deny_warnings) { diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 33dab802b21..b59c09eccaa 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -275,7 +275,6 @@ impl DefCollector { ast: SortedModule, root_file_id: FileId, debug_comptime_in_file: Option<&str>, - error_on_unused_items: bool, ) -> Vec<(CompilationError, FileId)> { let mut errors: Vec<(CompilationError, FileId)> = vec![]; let crate_id = def_map.krate; @@ -288,13 +287,7 @@ impl DefCollector { let crate_graph = &context.crate_graph[crate_id]; for dep in crate_graph.dependencies.clone() { - let error_on_usage_tracker = false; - errors.extend(CrateDefMap::collect_defs( - dep.crate_id, - context, - debug_comptime_in_file, - error_on_usage_tracker, - )); + errors.extend(CrateDefMap::collect_defs(dep.crate_id, context, debug_comptime_in_file)); let dep_def_map = context.def_map(&dep.crate_id).expect("ice: def map was just created"); @@ -464,9 +457,7 @@ impl DefCollector { errors.append(&mut more_errors); - if error_on_unused_items { - Self::check_unused_items(context, crate_id, &mut errors); - } + Self::check_unused_items(context, crate_id, &mut errors); errors } diff --git a/compiler/noirc_frontend/src/hir/def_map/mod.rs b/compiler/noirc_frontend/src/hir/def_map/mod.rs index d9d6e150a7a..cc956b3530f 100644 --- a/compiler/noirc_frontend/src/hir/def_map/mod.rs +++ b/compiler/noirc_frontend/src/hir/def_map/mod.rs @@ -8,7 +8,7 @@ use crate::token::{FunctionAttribute, SecondaryAttribute, TestScope}; use fm::{FileId, FileManager}; use noirc_arena::{Arena, Index}; use noirc_errors::Location; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, HashMap, HashSet}; mod module_def; pub use module_def::*; mod item_scope; @@ -78,7 +78,6 @@ impl CrateDefMap { crate_id: CrateId, context: &mut Context, debug_comptime_in_file: Option<&str>, - error_on_unused_imports: bool, ) -> Vec<(CompilationError, FileId)> { // Check if this Crate has already been compiled // XXX: There is probably a better alternative for this. @@ -121,7 +120,6 @@ impl CrateDefMap { ast, root_file_id, debug_comptime_in_file, - error_on_unused_imports, )); errors.extend( @@ -159,6 +157,10 @@ impl CrateDefMap { self.modules[module_id.0].location.file } + pub fn file_ids(&self) -> HashSet { + self.modules.iter().map(|(_, module_data)| module_data.location.file).collect() + } + /// Go through all modules in this crate, and find all functions in /// each module with the #[test] attribute pub fn get_all_test_functions<'a>( diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index 2bd1a852f05..41c63486b25 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -19,7 +19,7 @@ use fm::{FileId, FileManager}; use iter_extended::vecmap; use noirc_errors::Location; use std::borrow::Cow; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::PathBuf; use std::rc::Rc; @@ -301,6 +301,10 @@ impl Context<'_, '_> { }) } + pub fn crate_files(&self, crate_id: &CrateId) -> HashSet { + self.def_maps.get(crate_id).map(|def_map| def_map.file_ids()).unwrap_or_default() + } + /// Activates LSP mode, which will track references for all definitions. pub fn activate_lsp_mode(&mut self) { self.def_interner.lsp_mode = true; diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 3d908d1aa0c..720130ae813 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -114,7 +114,6 @@ pub(crate) fn get_program_with_maybe_parser_errors( }; let debug_comptime_in_file = None; - let error_on_unused_imports = true; // Now we want to populate the CrateDefMap using the DefCollector errors.extend(DefCollector::collect_crate_and_dependencies( @@ -123,7 +122,6 @@ pub(crate) fn get_program_with_maybe_parser_errors( program.clone().into_sorted(), root_file_id, debug_comptime_in_file, - error_on_unused_imports, )); } (program, context, errors)