Skip to content

x/tools/gopls: code action declare missing method does not infer return type from append or map key #75614

@paskozdilar

Description

@paskozdilar

Go version

go version go1.25.1 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='x86_64-linux-gnu-gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='x86_64-linux-gnu-g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/pasko/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/pasko/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2269890805=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/pasko/stuff/go/test/go.mod'
GOMODCACHE='/home/pasko/.local/share/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/pasko/.local/share/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.25'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/pasko/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.25/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

If we have the following code:

package main

type Foo struct{}

func main() {
	var i int
	var f Foo
	var d []int
	var m map[int]int

	i = f.GetInt()
	d = append(d, f.GetInt())
	m[f.GetInt()] = i
}

We get code actions for:

  • i = f.GetInt()
  • d = append(d, f.GetInt())
  • m[f.GetInt()] = i

In all three cases, it can be inferred from context that f.GetInt() should return an int.

When we run code action "Declare missing method Foo.GetInt()" for i = f.GetInt(), we get the following function, correctly returning int:

func (f Foo) GetInt() int {
	panic("unimplemented")
}

What did you see happen?

When we run code action "Declare missing method Foo.GetInt()" for d = append(d, f.GetInt()) or m[f.GetInt()] = i, we get the following function, with no return parameters, which is incompatible with the context where it was used:

func (f Foo) GetInt() {
	panic("unimplemented")
}

What did you expect to see?

A function declaration correctly returning an int:

func (f Foo) GetInt() int {
	panic("unimplemented")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions