[tools] go/ast/inspector: FindByPos returns the first innermost node

2 views
Skip to first unread message

Hongxiang Jiang (Gerrit)

unread,
Dec 26, 2025, 3:20:02 AM (3 days ago) Dec 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Hongxiang Jiang has uploaded the change for review

Commit message

go/ast/inspector: FindByPos returns the first innermost node

Fix golang/go#76872
Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89

Change diff

diff --git a/go/ast/inspector/cursor.go b/go/ast/inspector/cursor.go
index fc9bbc7..75f0983 100644
--- a/go/ast/inspector/cursor.go
+++ b/go/ast/inspector/cursor.go
@@ -449,10 +449,13 @@
return Cursor{}, false
}

-// FindByPos returns the cursor for the innermost node n in the tree
+// FindByPos returns the cursor for the first innermost node n in the tree
// rooted at c such that n.Pos() <= start && end <= n.End().
// (For an *ast.File, it uses the bounds n.FileStart-n.FileEnd.)
//
+// The "first" is specified because if the input is a position, the cursor may
+// be wholly contained by two nodes (e.g. at the boundary "foo|()").
+//
// It returns zero if none is found.
// Precondition: start <= end.
//
@@ -469,10 +472,14 @@

// best is the push-index of the latest (=innermost) node containing range.
// (Beware: latest is not always innermost because FuncDecl.{Name,Type} overlap.)
- best := int32(-1)
+ best, bestLimit := int32(-1), int32(-1)
for i, limit := c.indices(); i < limit; i++ {
ev := events[i]
if ev.index > i { // push?
+ if bestLimit != -1 && i >= bestLimit {
+ break
+ }
+
n := ev.node
var nodeEnd token.Pos
if file, ok := n.(*ast.File); ok {
@@ -501,10 +508,12 @@
break // disjoint, after; stop
}
}
+
// Inv: node.{Pos,FileStart} <= start
if end <= nodeEnd {
// node fully contains target range
best = i
+ bestLimit = ev.index
} else if nodeEnd < start {
i = ev.index // disjoint, before; skip forward
}
diff --git a/gopls/internal/test/marker/testdata/references/issue76872.txt b/gopls/internal/test/marker/testdata/references/issue76872.txt
new file mode 100644
index 0000000..77f1457
--- /dev/null
+++ b/gopls/internal/test/marker/testdata/references/issue76872.txt
@@ -0,0 +1,14 @@
+This test exercises a references query at a position where the cursor belongs to
+two syntax nodes: the node before and the node after. It ensures that we find
+the reference of the syntax node before the cursor.
+
+See https://github.com/golang/go/issues/76872.
+
+-- main.go --
+package main
+
+func foo() {} //@ loc(decl, "foo"), refs(re"foo()", decl, call)
+
+func _() {
+ foo() //@ loc(call, "foo")
+}

Change information

Files:
  • M go/ast/inspector/cursor.go
  • A gopls/internal/test/marker/testdata/references/issue76872.txt
Change size: S
Delta: 2 files changed, 25 insertions(+), 2 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
Gerrit-Change-Number: 732521
Gerrit-PatchSet: 1
Gerrit-Owner: Hongxiang Jiang <hxj...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Hongxiang Jiang (Gerrit)

unread,
Dec 26, 2025, 3:21:30 AM (3 days ago) Dec 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Hongxiang Jiang voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
Gerrit-Change-Number: 732521
Gerrit-PatchSet: 1
Gerrit-Owner: Hongxiang Jiang <hxj...@golang.org>
Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
Gerrit-Comment-Date: Fri, 26 Dec 2025 08:21:23 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Hongxiang Jiang (Gerrit)

unread,
Dec 26, 2025, 10:16:25 AM (3 days ago) Dec 26
to goph...@pubsubhelper.golang.org, Go LUCI, Alan Donovan, Dmitri Shuralyov, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Alan Donovan

New activity on the change

Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
    Gerrit-Change-Number: 732521
    Gerrit-PatchSet: 2
    Gerrit-Owner: Hongxiang Jiang <hxj...@golang.org>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Alan Donovan <adon...@google.com>
    Gerrit-Comment-Date: Fri, 26 Dec 2025 15:16:16 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Hongxiang Jiang (Gerrit)

    unread,
    Dec 28, 2025, 10:13:04 PM (6 hours ago) Dec 28
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Alan Donovan

    Hongxiang Jiang uploaded new patchset

    Hongxiang Jiang uploaded patch set #3 to this change.
    Following approvals got outdated and were removed:
    • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
      Gerrit-Change-Number: 732521
      Gerrit-PatchSet: 3
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Hongxiang Jiang (Gerrit)

      unread,
      Dec 28, 2025, 10:13:54 PM (6 hours ago) Dec 28
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Alan Donovan

      Hongxiang Jiang uploaded new patchset

      Hongxiang Jiang uploaded patch set #4 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Alan Donovan
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
      Gerrit-Change-Number: 732521
      Gerrit-PatchSet: 4
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Hongxiang Jiang (Gerrit)

      unread,
      Dec 28, 2025, 10:14:01 PM (6 hours ago) Dec 28
      to goph...@pubsubhelper.golang.org, Go LUCI, Alan Donovan, Dmitri Shuralyov, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Alan Donovan

      Hongxiang Jiang voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Alan Donovan
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I35d9c6cb0abfe2d380bf326bdd1e547a3f1abc89
      Gerrit-Change-Number: 732521
      Gerrit-PatchSet: 4
      Gerrit-Owner: Hongxiang Jiang <hxj...@golang.org>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
      Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Mon, 29 Dec 2025 03:13:55 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages