Add unused items pass#29431
Draft
mohammadfawaz wants to merge 1 commit into
Draft
Conversation
c9ad543 to
1d530b0
Compare
212cdfa to
5cd4474
Compare
81a0e92 to
73fd3ba
Compare
Introduce a new compiler pass that emits warnings for unused items (functions, structs, consts, imports, local bindings) modeled on rustc's `dead_code` and `unused_variables` lints. Also relax the parser's underscore-prefix rejection for binding-position identifiers, reject binding names that collide with compiler intrinsics, and forbid `@no_inline` on functions whose name starts with `_` (those are force-inlined). Document the new warnings and the identifier rules under `documentation/language/`.
73fd3ba to
548b070
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
@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_variable@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.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/potential_global_uses, tracks lexical scopes, emits body-level warnings as each scope drains, and records composite member-dependency edges into a local graph.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.