Skip to content

Commit

Permalink
gopls/internal/golang: don't try to inline dynamic calls
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr authored and gopherbot committed Nov 18, 2024
1 parent 52eb446 commit a54bd37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gopls/internal/golang/inline_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,16 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
continue
}

if typeutil.StaticCallee(refpkg.TypesInfo(), call) == nil {
continue // dynamic call
}

// Sanity check.
if obj := refpkg.TypesInfo().ObjectOf(name); obj == nil ||
obj.Name() != origDecl.Name.Name ||
obj.Pkg() == nil ||
obj.Pkg().Path() != string(pkg.Metadata().PkgPath) {

return nil, bug.Errorf("cannot inline: corrupted reference %v", ref)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Specifically, check
1. basic removal of unused parameters, when the receiver is named, locally and
across package boundaries
2. handling of unnamed receivers
3. no panics related to references through interface satisfaction

-- go.mod --
module example.com/rm
Expand Down Expand Up @@ -37,6 +38,16 @@ func _() {

func sideEffects() int

type Fooer interface {
Foo(int)
}

// Dynamic calls aren't rewritten.
// Previously, this would cause a bug report or crash (golang/go#69896).
func _(f Fooer) {
f.Foo(1)
}

-- @basic/basic.go --
package rm

Expand Down Expand Up @@ -66,6 +77,16 @@ func _() {
}

func sideEffects() int

type Fooer interface {
Foo(int)
}

// Dynamic calls aren't rewritten.
// Previously, this would cause a bug report or crash (golang/go#69896).
func _(f Fooer) {
f.Foo(1)
}
-- missingrecv.go --
package rm

Expand Down

0 comments on commit a54bd37

Please sign in to comment.