Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

クローズ
#127 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
28/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
java, kotlin
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Java
スター
1
フォーク
1
平均マージ
1時間 58分
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CodeLaser/maddi のほかの issue

CodeLaser/maddi の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。