Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đã đóng
#127 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
28/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java, kotlin
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Java
Star
1
Fork
1
Merge trung bình
1 giờ 58 phút
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của CodeLaser/maddi

Tất cả issue của CodeLaser/maddi

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.