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

fix: don't warn on unuse global if it has an abi annotation #6258

Merged
merged 1 commit into from
Oct 9, 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
17 changes: 10 additions & 7 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

Check warning on line 843 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(
Expand Down Expand Up @@ -1203,6 +1203,7 @@
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(),
Expand All @@ -1217,13 +1218,15 @@
// 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 =
Expand Down
13 changes: 13 additions & 0 deletions compiler/noirc_frontend/src/tests/unused_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down
Loading