From 2a6404f4c0b6a66e254026b1643598d12a644f55 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 9 Oct 2024 10:41:10 -0300 Subject: [PATCH] fix: don't warn on unuse global if it has an abi annotation --- .../src/hir/def_collector/dc_mod.rs | 17 ++++++++++------- .../noirc_frontend/src/tests/unused_items.rs | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index 65912023661..b9ce8f361f7 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -1203,6 +1203,7 @@ pub(crate) fn collect_global( let global = global.item; let name = global.pattern.name_ident().clone(); + let is_abi = global.attributes.iter().any(|attribute| attribute.is_abi()); let global_id = interner.push_empty_global( name.clone(), @@ -1217,13 +1218,15 @@ pub(crate) fn collect_global( // Add the statement to the scope so its path can be looked up later let result = def_map.modules[module_id.0].declare_global(name.clone(), visibility, global_id); - let parent_module_id = ModuleId { krate: crate_id, local_id: module_id }; - interner.usage_tracker.add_unused_item( - parent_module_id, - name, - UnusedItem::Global(global_id), - visibility, - ); + if !is_abi { + let parent_module_id = ModuleId { krate: crate_id, local_id: module_id }; + interner.usage_tracker.add_unused_item( + parent_module_id, + name, + UnusedItem::Global(global_id), + visibility, + ); + } let error = result.err().map(|(first_def, second_def)| { let err = diff --git a/compiler/noirc_frontend/src/tests/unused_items.rs b/compiler/noirc_frontend/src/tests/unused_items.rs index c89670a9043..51bdf785688 100644 --- a/compiler/noirc_frontend/src/tests/unused_items.rs +++ b/compiler/noirc_frontend/src/tests/unused_items.rs @@ -211,6 +211,19 @@ fn warns_on_unused_global() { assert_eq!(item.item_type(), "global"); } +#[test] +fn does_not_warn_on_unused_global_if_it_has_an_abi_attribute() { + let src = r#" + contract foo { + #[abi(notes)] + global bar = 1; + } + + fn main() {} + "#; + assert_no_errors(src); +} + #[test] fn no_warning_on_inner_struct_when_parent_is_used() { let src = r#"