Add unused items pass#29431
Draft
mohammadfawaz wants to merge 1 commit into
Draft
Conversation
c9ad543 to
1d530b0
Compare
212cdfa to
5cd4474
Compare
73fd3ba to
548b070
Compare
f1baea5 to
7c246b6
Compare
Emit warnings for items that are never used, mirroring rustc's `dead_code`, `unused_variables`, `unused_imports`, and `unused_const` lints: - Unused functions (non-entry, non-`view`, non-`@test`), structs, top-level and local `const`s, local bindings (`let`, params, loop vars), and imports. - A leading `_` marks an item intentionally unused and silences the warning; `_`-prefixed `fn`s are force-inlined so the name never reaches the VM. - Names emitted into Aleo bytecode (program, entry-point and `view fn`, struct/record and their fields, mappings, storage variables) are rejected if they start with `_`, and bindings colliding with a bare-callable intrinsic are rejected. Dead program functions no longer double-report their unused params/locals on top of the function-unused warning, and local-binding lookups use an O(1) per-name index instead of an O(n) reverse scan.
7c246b6 to
8fa54ac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an
UnusedItemswarning pass to the compiler. Runs afterTypeCheckingand emitsrustc-styledead_code/unused_variables/unused_importsdiagnostics under theUNUcode prefix.What's checked
view, non-@test)const(any scope)let/ param / loop iter / const generic_-prefixed binding that was readFields, mappings, and storage variables share one future unlock: data-flow-aware "is this ever read/written" tracking + an
@allow_unusedattribute for the legitimate false-positive cases.Leading-
_rulesMirroring
rustc's_xconvention. Whether_is permitted depends on whether the name reaches the Aleo VM — if it does, snarkVM rejects identifiers that do not start with a letter, so we reject at compile time too. Allowed positions silence the correspondingunused_*warning.letbindingunused_variableunused_variableconstunused_constconstunused_constunused_variableview, non-@test)unused_variable. Param names don't reach the VM (renamed tor0,r1, …).unused_variable. Monomorphized away.Variant::Fn(free fn) nameunused_function. Force-inlined so the name never reaches the VM.Variant::FinalFn(final fn) nameunused_function. Always inlined.fninsideprogram { … })NameValidationrejects →ENV03711002.view fnnameNameValidationrejects →ENV03711002. Externally callable and emitted verbatim (never inlined), like an entry point.NameValidationrejects →ENV03711002.NameValidationrejects →ENV03711002.NameValidationrejects →ENV03711002.NameValidationrejects →ENV03711002.program X.aleo)NameValidationrejects →ENV03711002._self_caller,_block_height, etc.)EPAR0370056. The parser dispatches_self_caller()to the intrinsic before any scope lookup, so a same-named local would be silently shadowed.@no_inlineon a_-prefixedVariant::FnETYC0372192. The two annotations directly contradict.Bonus: reading a
_-prefixed local (which would defeat the silencing marker) emitsWUNU03714006—used binding \x` whose name begins with ```.Architecture
The pass is structured as three phases, all driven by the standard
AstVisitor/UnitVisitortraits:UseCollectorwalks the AST once: populatesused_imports/used_globals, tracks lexical scopes, emits body-level warnings as each scope drains, and records composite member-dependency edges into a local graph (composite_deps/composite_roots).UnusedCheckerwalks the AST again (without descending into bodies) and emits warnings for unused top-level items and imports.The two visitors share state via an owned
CollectedUsesstruct passed by reference.NameValidation(a sibling pass that runs before TypeChecking) handles all leading-_rejections in positions where the name reaches the Aleo VM.