Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

LocalVariable: no identity for the declaration, so same-named locals in sibling scopes are one variable

Chiusa
#127 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
28/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java, kotlin
Ambito
backend

Direzione di ricerca

Start by reading LocalVariableImpl and the Variable interface, especially the existing assignmentExpression() contract and the equality assumptions described in the issue. Then trace how the parser represents local declarations and how variable reads are resolved, including for-each, catch, and pattern declarations. The issue proposes declaration identity or a scope-qualified name; done means same-named locals from separate declarations can be distinguished by callers without changing equals.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement
What is missing

LocalVariableImpl documents it: a local's FQN is its bare name, so equals/hashCode make two same-named locals from different scopes equal. The note gives the two invariants under which that is safe for the analyzers (scope-disjoint locals never coexist in a per-statement structure; graphs are per-method).

Rewriting tools work outside those invariants all the time. The natural question "is the local that this expression reads assigned anywhere in the method?" is asked over a whole method body, and with name equality the answer is wrong whenever another block declares the same name:

if (ref != null) {
    Map<String, Fingerprint> m = ref.get();
    if (m != null)
        return m;
}
Map<String, Fingerprint> m = new TreeMap<>();
for (Map.Entry<String, String> r : record.entrySet()) {
    m.put(r.getKey(), r.getValue());        // reads the second m, never assigned after its declaration
}

Collecting Assignment.variableTarget() over the body and testing assigned.contains(ve.variable()) reports the read of the second m as a read of an assigned local (the first m has an assignment-like initializer elsewhere in some variants of this shape, and any m = … anywhere taints both). The same conflation reaches Set<Variable> of locals declared inside a block, "is this element variable assigned", and name-based taken sets — three finders in one tool had to be rewritten around it.

Proposal

Give a LocalVariable an identity for its declaration, without changing equals:

  • LocalVariable.declaration() (or declarationId()): the LocalVariableCreation / ForEachStatement initializer / TryStatement.CatchClause / pattern that declares it, or a stable per-method index assigned by the parser, as the anonymous-type index is; two reads of one declaration share it, two declarations of one name do not.
  • Alternatively a Variable.scopeQualifiedName() that includes the declaring statement's index path, so that a caller that needs scope-aware equality can key on that instead.

The assignmentExpression() already on the interface is a per-declaration fact that the reads carry around; the declaration identity is the same kind of fact and would retire the caller-side scope walk below.

Workaround used

A Visitor over the method body that keeps a stack of scopes (beforeStatement pushes, afterStatement pops; a LocalVariableCreation declares into the enclosing scope, a for-each or for initializer and a catch variable into the statement's scope), resolves each VariableExpression to the declaring element by identity, and falls back to name equality for what it cannot place (parameters, lambda parameters, pattern variables).

Seen on 0.9.5-alpha.13; the note in LocalVariableImpl is unchanged on devel.

Lingua principale
Java
Stelle
1
Fork
1
Merge medio
2h 22m
PR unite (30g)
4

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di CodeLaser/maddi

Tutte le issue di CodeLaser/maddi

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.