Skip to content

Commit a54bd37

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/golang: don't try to inline dynamic calls
Gopls previously reported a bug when encountering a dynamic method reference. Instead, we should just skip this reference when inlining all calls. Fixes golang/go#69896 Change-Id: Id6971e2a3eb79a94e76eecbfcefc44bec9040b8e Reviewed-on: https://go-review.googlesource.com/c/tools/+/628376 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 52eb446 commit a54bd37

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

gopls/internal/golang/inline_all.go

+5
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,16 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
152152
continue
153153
}
154154

155+
if typeutil.StaticCallee(refpkg.TypesInfo(), call) == nil {
156+
continue // dynamic call
157+
}
158+
155159
// Sanity check.
156160
if obj := refpkg.TypesInfo().ObjectOf(name); obj == nil ||
157161
obj.Name() != origDecl.Name.Name ||
158162
obj.Pkg() == nil ||
159163
obj.Pkg().Path() != string(pkg.Metadata().PkgPath) {
164+
160165
return nil, bug.Errorf("cannot inline: corrupted reference %v", ref)
161166
}
162167

gopls/internal/test/marker/testdata/codeaction/removeparam_method.txt

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Specifically, check
44
1. basic removal of unused parameters, when the receiver is named, locally and
55
across package boundaries
66
2. handling of unnamed receivers
7+
3. no panics related to references through interface satisfaction
78

89
-- go.mod --
910
module example.com/rm
@@ -37,6 +38,16 @@ func _() {
3738

3839
func sideEffects() int
3940

41+
type Fooer interface {
42+
Foo(int)
43+
}
44+
45+
// Dynamic calls aren't rewritten.
46+
// Previously, this would cause a bug report or crash (golang/go#69896).
47+
func _(f Fooer) {
48+
f.Foo(1)
49+
}
50+
4051
-- @basic/basic.go --
4152
package rm
4253

@@ -66,6 +77,16 @@ func _() {
6677
}
6778

6879
func sideEffects() int
80+
81+
type Fooer interface {
82+
Foo(int)
83+
}
84+
85+
// Dynamic calls aren't rewritten.
86+
// Previously, this would cause a bug report or crash (golang/go#69896).
87+
func _(f Fooer) {
88+
f.Foo(1)
89+
}
6990
-- missingrecv.go --
7091
package rm
7192

0 commit comments

Comments
 (0)