Skip to content

Commit

Permalink
Add Document
Browse files Browse the repository at this point in the history
  • Loading branch information
xzbdmw committed Oct 11, 2024
1 parent 557ee89 commit cac52c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion gopls/doc/features/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Client support:
<!-- Below we list any quick fixes (by their internal fix name)
that aren't analyzers. -->

### `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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions gopls/doc/release/v0.17.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit cac52c7

Please sign in to comment.