diff --git a/gopls/doc/features/diagnostics.md b/gopls/doc/features/diagnostics.md index 8c9305c94b6..9ebcb8e6c8c 100644 --- a/gopls/doc/features/diagnostics.md +++ b/gopls/doc/features/diagnostics.md @@ -127,7 +127,7 @@ Client support: -### `stubMethods`: Declare missing methods of type +### `stubMissingInterfaceMethods`: Declare missing methods to implement an interface of type When a value of a concrete type is assigned to a variable of an interface type, but the concrete type does not possess all the @@ -162,6 +162,37 @@ func (NegativeErr) Error() string { } ``` +### `StubMissingCalledFunction`: Generate missing methods from function calls + +When you attempt to call a method on a type that does not have that method, +the compiler will report an error like “type X has no field or method Y”. +In this scenario, gopls now offers a quick fix to generate a stub declaration of +the missing method on that concrete type. The correct signature is inferred +from the method call. + +Consider the following code where `Foo` does not have a method `bar`: + +```go +type Foo struct{} + +func main() { + var s string + f := Foo{} + s = f.bar("str", 42) +} +``` + +This code will not compile and produces the error: +`f.bar undefined (type Foo has no field or method bar`. + +Gopls will offer a quick fix to declare this method: + +```go +func (f Foo) bar(s string, i int) string { + panic("unimplemented") +} +``` + Beware that the new declarations appear alongside the concrete type, which may be in a different file or even package from the cursor position. diff --git a/gopls/doc/release/v0.17.0.md b/gopls/doc/release/v0.17.0.md index 3f4f7458223..f2941d626cf 100644 --- a/gopls/doc/release/v0.17.0.md +++ b/gopls/doc/release/v0.17.0.md @@ -63,3 +63,11 @@ function's Go `func` declaration. If the function is implemented in C or assembly, the function has no body. Executing a second Definition query (while already at the Go declaration) will navigate you to the assembly implementation. + +## Generate missing method from function calls +Gopls now offers a new code action, “Declare missing methods of Type T”, +When you attempt to call a method on a type that does not have that method, +the compiler will report an error like “type X has no field or method Y”. +In this scenario, gopls now offers a quick fix to generate a stub declaration of +the missing method on that concrete type, the correct signature is inferred +from the method call.