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 unused imports checker #1205

Closed
wants to merge 1 commit into from
Closed

Fix unused imports checker #1205

wants to merge 1 commit into from

Conversation

Fumuran
Copy link
Contributor

@Fumuran Fumuran commented Jan 22, 2024

This PR fixes the the unused import checker. After this update it will check not only invoked procedures, but also reexported procedures in modules.

@Fumuran Fumuran requested a review from bobbinth January 22, 2024 17:28
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left a couple of small comments inline, but I also wonder if a slightly different approach would be better.

Specifically, instead of modifying ProcReExport struct, we could add reexported_procs field to the ModuleImports struct. This would work similarly to invoked_procs field. That is, inside ParserContext::parse_reexported_procedure() we could add the re-exported procedure to module imports.

A potential benefit of this approach is that all info about imports and their usage is encapsulated in the ModuleImports struct, and we can determine if there are any unused imports by looking at only that struct.

let import_lib_paths = import_info.import_paths();
let invoked_procs_paths: Vec<&LibraryPath> =
import_info.invoked_procs().iter().map(|(_id, (_name, path))| path).collect();
fn check_unused_imports(context: &ParserContext) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably specify the type for context as &super::ParserContext. This way, we won't get a warning under no-std compilation.

@Fumuran
Copy link
Contributor Author

Fumuran commented Feb 13, 2024

I made a new version of unused imports checker, but it contains serialization functions which work only with a Winterfell 0.8, so I'll push it after we merge #1234

@Fumuran Fumuran force-pushed the andrew-unused-checker-fix branch from a921638 to ed5f10e Compare February 14, 2024 21:21
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few comments line.

Comment on lines 100 to 109
let import_lib_paths = import_info.import_paths();
let invoked_procs_paths: Vec<&LibraryPath> =
import_info.invoked_procs().iter().map(|(_id, (_name, path))| path).collect();
let reexported_procs_paths = import_info.reexported_procs_paths();

for lib in import_lib_paths {
if !invoked_procs_paths.contains(&lib) {
if !invoked_procs_paths.contains(&lib) && !reexported_procs_paths.contains(lib) {
event!(Level::WARN, "unused import: \"{}\"", lib);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this function could go into ModuleImports, right? For example, we could have something like:

pub fn hash_unused_imports(&self) -> bool

in ModuleImports, and then here, we'd check and print the warning.

pub fn new(
imports: ImportedModulesMap,
invoked_procs: InvokedProcsMap,
reexported_procs_paths: Vec<LibraryPath>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a BTreeSet?

Also, this contains module paths rather than procedure paths, right? If so, I'd call it reexported_modules.

Comment on lines 99 to 102
pub fn get_module_path(&self, module_name: &str) -> Option<LibraryPath> {
self.imports.get(&module_name.to_string()).cloned()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to clone the module path to be able to insert in to the reexported_modules, otherwise I will try to mutate import_info, having immutable reference to it. I use it in the context.rs file

Comment on lines 138 to 149
/// Returns a reference to the vector of module paths from which some procedures were reexported.
pub(super) fn reexported_procs_paths(&self) -> &Vec<LibraryPath> {
&self.reexported_procs_paths
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add hash_unsued_imports() method, we probably won't need this one.

@Fumuran Fumuran force-pushed the andrew-unused-checker-fix branch 2 times, most recently from fecf9ad to a84b81d Compare February 19, 2024 21:22
@Fumuran Fumuran requested a review from bobbinth February 19, 2024 21:25
@Fumuran Fumuran force-pushed the andrew-unused-checker-fix branch 2 times, most recently from defcd26 to a992fe6 Compare February 23, 2024 08:24
@Fumuran Fumuran force-pushed the andrew-unused-checker-fix branch from a992fe6 to fe51dad Compare March 1, 2024 14:32
@plafer
Copy link
Contributor

plafer commented Apr 29, 2024

@Overcastan are you still working on this?

@bobbinth
Copy link
Contributor

I believe this has been implemented in #1277. cc @bitwalker

@bitwalker
Copy link
Contributor

Yes, this should be superseded by the changes in #1277, which will emit warnings for this. I haven't evaluated how well diagnostics get rendered via the CLI after merging that PR, but that would be a separate issue anyway.

@bobbinth
Copy link
Contributor

bobbinth commented May 1, 2024

Superseded by #1277.

@bobbinth bobbinth closed this May 1, 2024
@bobbinth bobbinth deleted the andrew-unused-checker-fix branch May 1, 2024 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants