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

Removes glob import of SnapshotKind #34333

Merged
merged 1 commit into from
Dec 6, 2023

Conversation

brooksprumo
Copy link
Contributor

Problem

Well... today I learned about glob imports of enums and how they interact with match statements. Basically, glob imports of an enum that are used with a match can be a footgun. Refer to this playground:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9f0ce5f074f7b4a71f487ae8c6a1902d

Summary of Changes

Removes glob import of SnapshotKind and AccountsPackageKind.

@brooksprumo brooksprumo self-assigned this Dec 6, 2023
@brooksprumo brooksprumo marked this pull request as ready for review December 6, 2023 06:13
@brooksprumo brooksprumo requested a review from apfitzge December 6, 2023 06:17
Copy link

codecov bot commented Dec 6, 2023

Codecov Report

Merging #34333 (c700439) into master (5c00a33) will increase coverage by 0.0%.
Report is 2 commits behind head on master.
The diff coverage is 100.0%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #34333   +/-   ##
=======================================
  Coverage    81.9%    81.9%           
=======================================
  Files         819      819           
  Lines      220422   220422           
=======================================
+ Hits       180652   180707   +55     
+ Misses      39770    39715   -55     

Copy link
Contributor

@apfitzge apfitzge left a comment

Choose a reason for hiding this comment

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

Not opposed to the changes but think that naming conventions and the warnings from violating those conventions are generally sufficient to protect against this kind of mistake.

If I read your example correctly, it's not so much to do with global imports but just that 'C' is acting as the catch all case. Explicit namespacing guarantees we don't make that mistake, but it would still generate a warning since 'C' is both unused and a non snake_case variable name.

@brooksprumo
Copy link
Contributor Author

If I read your example correctly [..]

Yep, you did!

Not opposed to the changes but think that naming conventions and the warnings from violating those conventions are generally sufficient to protect against this kind of mistake.

I'm mostly thinking about preventing incorrect refactorings. In the example, if I change the match to only resolve the warnings, I could have something like this:

        match self {
            A => write!(f, "A"),
            B => write!(f, "B"),
            _c => write!(f, "C"),
        }

Which is still broken. Hopefully this code change would never make it past code review. I feel safer to make this entire class of problems impossible, by removing the glob import.

Is there another implementation and/or tweak that you would be in favor of?

@brooksprumo brooksprumo requested a review from apfitzge December 6, 2023 13:26
@brooksprumo brooksprumo merged commit 61fa130 into solana-labs:master Dec 6, 2023
33 checks passed
@brooksprumo brooksprumo deleted the use-enum/snapshot-kind branch December 6, 2023 18:46
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.

2 participants