Skip to content

Commit a0eca0c

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: fix crash in refactor.inline.variable
Fixes golang/go#74347 Change-Id: I19c38eec92c61e611c5112ae83e2c084c7954a14 Reviewed-on: https://go-review.googlesource.com/c/tools/+/683535 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent 4c0fd1d commit a0eca0c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

gopls/internal/golang/inline.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"golang.org/x/tools/gopls/internal/cache"
2222
"golang.org/x/tools/gopls/internal/cache/parsego"
2323
"golang.org/x/tools/gopls/internal/protocol"
24+
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
2425
"golang.org/x/tools/gopls/internal/util/safetoken"
2526
"golang.org/x/tools/internal/diff"
2627
"golang.org/x/tools/internal/event"
@@ -196,6 +197,12 @@ func inlineVariableOne(pkg *cache.Package, pgf *parsego.File, start, end token.P
196197
}
197198
id := curIdent.Node().(*ast.Ident)
198199
obj1 := info.Uses[id]
200+
if obj1 == nil {
201+
continue // undefined; or a def, not a use
202+
}
203+
if goplsastutil.NodeContains(curRHS.Node(), obj1.Pos()) {
204+
continue // not free (id is defined within RHS)
205+
}
199206
_, obj2 := scope.LookupParent(id.Name, pos)
200207
if obj1 != obj2 {
201208
return nil, nil, fmt.Errorf("cannot inline variable: its initializer expression refers to %q, which is shadowed by the declaration at line %d", id.Name, safetoken.Position(pgf.Tok, obj2.Pos()).Line)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
This is a regressoon test of a crash in refactor.inline.variable.
2+
3+
-- go.mod --
4+
module example.com/a
5+
go 1.18
6+
7+
-- a/a.go --
8+
package a
9+
10+
func _() {
11+
x := func(notfree int) { _ = notfree }
12+
println(x) //@codeaction("x", "refactor.inline.variable", result=out)
13+
}
14+
-- @out/a/a.go --
15+
package a
16+
17+
func _() {
18+
x := func(notfree int) { _ = notfree }
19+
println(func(notfree int) { _ = notfree }) //@codeaction("x", "refactor.inline.variable", result=out)
20+
}

0 commit comments

Comments
 (0)