Skip to content

Commit 581c0b3

Browse files
tttoadgopherbot
authored andcommitted
gopls/internal/lsp/source: add receiver name to stubbed methods
Fixes golang/go#64078 Change-Id: Ib551119b04a36d0be0929a3f949b052b598f57ad Reviewed-on: https://go-review.googlesource.com/c/tools/+/544916 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent c95fa0f commit 581c0b3

File tree

8 files changed

+91
-26
lines changed

8 files changed

+91
-26
lines changed

gopls/internal/cmd/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ var _ io.Reader = C{}
978978
type C struct{}
979979
980980
// Read implements io.Reader.
981-
func (C) Read(p []byte) (n int, err error) {
981+
func (c C) Read(p []byte) (n int, err error) {
982982
panic("unimplemented")
983983
}
984984
`[1:]

gopls/internal/lsp/source/stub.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,46 @@ func stubMethodsFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.
190190
star = "*"
191191
}
192192

193+
// If there are any that have named receiver, choose the first one.
194+
// Otherwise, use lowercase for the first letter of the object.
195+
rn := strings.ToLower(si.Concrete.Obj().Name()[0:1])
196+
for i := 0; i < si.Concrete.NumMethods(); i++ {
197+
if recv, ok := si.Concrete.Method(i).Type().(*types.Signature); ok && recv.Recv().Name() != "" {
198+
rn = recv.Recv().Name()
199+
break
200+
}
201+
}
202+
203+
// Check for receiver name conflicts
204+
checkRecvName := func(tuple *types.Tuple) bool {
205+
for i := 0; i < tuple.Len(); i++ {
206+
if rn == tuple.At(i).Name() {
207+
return true
208+
}
209+
}
210+
return false
211+
}
212+
193213
// Format the new methods.
194214
var newMethods bytes.Buffer
215+
195216
for index := range missing {
217+
mrn := rn + " "
218+
if sig, ok := missing[index].fn.Type().(*types.Signature); ok {
219+
if checkRecvName(sig.Params()) || checkRecvName(sig.Results()) {
220+
mrn = ""
221+
}
222+
}
223+
196224
fmt.Fprintf(&newMethods, `// %s implements %s.
197-
%sfunc (%s%s%s) %s%s {
225+
%sfunc (%s%s%s%s) %s%s {
198226
panic("unimplemented")
199227
}
200228
`,
201229
missing[index].fn.Name(),
202230
iface,
203231
missing[index].needSubtle,
232+
mrn,
204233
star,
205234
si.Concrete.Obj().Name(),
206235
FormatTypeParams(si.Concrete.TypeParams()),

gopls/internal/test/marker/testdata/stubmethods/basic.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var _ error = C(0) //@suggestedfix(re"C.0.", re"missing method Error", stub)
1313
-- @stub/a/a.go --
1414
@@ -5 +5,5 @@
1515
+// Error implements error.
16-
+func (C) Error() string {
16+
+func (c C) Error() string {
1717
+ panic("unimplemented")
1818
+}
1919
+

gopls/internal/test/marker/testdata/stubmethods/issue61693.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func _() {
2020
-- @stub/main.go --
2121
@@ -5 +5,5 @@
2222
+// Error implements error.
23-
+func (C) Error() string {
23+
+func (c C) Error() string {
2424
+ panic("unimplemented")
2525
+}
2626
+

gopls/internal/test/marker/testdata/stubmethods/issue61830.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _ I = &A{} //@suggestedfix(re"&A..", re"missing method M", stub)
1818
-- @stub/p.go --
1919
@@ -13 +13,5 @@
2020
+// M implements I.
21-
+func (*A) M(io.Reader, B) {
21+
+func (a *A) M(io.Reader, B) {
2222
+ panic("unimplemented")
2323
+}
2424
+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This test verifies that the named receiver is generated.
2+
3+
-- p.go --
4+
package p
5+
6+
type A struct{}
7+
8+
func (aa *A) M1() {
9+
panic("unimplemented")
10+
}
11+
12+
type I interface {
13+
M1()
14+
M2(aa string)
15+
M3(bb string)
16+
M4() (aa string)
17+
}
18+
19+
var _ I = &A{} //@suggestedfix(re"&A..", re"missing method M", stub)
20+
-- @stub/p.go --
21+
@@ -5 +5,15 @@
22+
+// M2 implements I.
23+
+func (*A) M2(aa string) {
24+
+ panic("unimplemented")
25+
+}
26+
+
27+
+// M3 implements I.
28+
+func (aa *A) M3(bb string) {
29+
+ panic("unimplemented")
30+
+}
31+
+
32+
+// M4 implements I.
33+
+func (*A) M4() (aa string) {
34+
+ panic("unimplemented")
35+
+}
36+
+

gopls/internal/test/marker/testdata/stubmethods/issue64114.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ type WriteStruct struct {
2727
+
2828
+// RRRR implements WriteTest.
2929
+// Subtle: this method shadows the method (WriterTwoStruct).RRRR of WriteStruct.WriterTwoStruct.
30-
+func (*WriteStruct) RRRR() {
30+
+func (w *WriteStruct) RRRR() {
3131
+ panic("unimplemented")
3232
+}
3333
+
3434
+// WWWW implements WriteTest.
35-
+func (*WriteStruct) WWWW() {
35+
+func (w *WriteStruct) WWWW() {
3636
+ panic("unimplemented")
3737
+}

gopls/internal/test/marker/testdata/suggestedfix/stub.txt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type byteWriter struct{}
5353
@@ -12 +12,5 @@
5454
+
5555
+// WriteByte implements io.ByteWriter.
56-
+func (*byteWriter) WriteByte(c byte) error {
56+
+func (b *byteWriter) WriteByte(c byte) error {
5757
+ panic("unimplemented")
5858
+}
5959
-- assign_multivars.go --
@@ -73,7 +73,7 @@ type multiByteWriter struct{}
7373
@@ -13 +13,5 @@
7474
+
7575
+// WriteByte implements io.ByteWriter.
76-
+func (*multiByteWriter) WriteByte(c byte) error {
76+
+func (m *multiByteWriter) WriteByte(c byte) error {
7777
+ panic("unimplemented")
7878
+}
7979
-- call_expr.go --
@@ -94,7 +94,7 @@ type callExpr struct{}
9494
@@ -14 +14,5 @@
9595
+
9696
+// Error implements error.
97-
+func (*callExpr) Error() string {
97+
+func (c *callExpr) Error() string {
9898
+ panic("unimplemented")
9999
+}
100100
-- embedded.go --
@@ -116,22 +116,22 @@ type embeddedInterface interface {
116116
-- @embedded/embedded.go --
117117
@@ -12 +12,20 @@
118118
+// Len implements embeddedInterface.
119-
+func (*embeddedConcrete) Len() int {
119+
+func (e *embeddedConcrete) Len() int {
120120
+ panic("unimplemented")
121121
+}
122122
+
123123
+// Less implements embeddedInterface.
124-
+func (*embeddedConcrete) Less(i int, j int) bool {
124+
+func (e *embeddedConcrete) Less(i int, j int) bool {
125125
+ panic("unimplemented")
126126
+}
127127
+
128128
+// Read implements embeddedInterface.
129-
+func (*embeddedConcrete) Read(p []byte) (n int, err error) {
129+
+func (e *embeddedConcrete) Read(p []byte) (n int, err error) {
130130
+ panic("unimplemented")
131131
+}
132132
+
133133
+// Swap implements embeddedInterface.
134-
+func (*embeddedConcrete) Swap(i int, j int) {
134+
+func (e *embeddedConcrete) Swap(i int, j int) {
135135
+ panic("unimplemented")
136136
+}
137137
+
@@ -148,7 +148,7 @@ type customErr struct{}
148148
@@ -9 +9,5 @@
149149
+
150150
+// Error implements error.
151-
+func (*customErr) Error() string {
151+
+func (c *customErr) Error() string {
152152
+ panic("unimplemented")
153153
+}
154154
-- function_return.go --
@@ -167,7 +167,7 @@ type closer struct{}
167167
@@ -12 +12,5 @@
168168
+
169169
+// Close implements io.Closer.
170-
+func (closer) Close() error {
170+
+func (c closer) Close() error {
171171
+ panic("unimplemented")
172172
+}
173173
-- generic_receiver.go --
@@ -187,7 +187,7 @@ type genReader[T, Y any] struct {
187187
@@ -13 +13,5 @@
188188
+
189189
+// ReadFrom implements io.ReaderFrom.
190-
+func (*genReader[T, Y]) ReadFrom(r io.Reader) (n int64, err error) {
190+
+func (g *genReader[T, Y]) ReadFrom(r io.Reader) (n int64, err error) {
191191
+ panic("unimplemented")
192192
+}
193193
-- ignored_imports.go --
@@ -213,7 +213,7 @@ type ignoredResetter struct{}
213213
@@ -19 +19,5 @@
214214
+
215215
+// Reset implements zlib.Resetter.
216-
+func (*ignoredResetter) Reset(r Reader, dict []byte) error {
216+
+func (i *ignoredResetter) Reset(r Reader, dict []byte) error {
217217
+ panic("unimplemented")
218218
+}
219219
-- issue2606.go --
@@ -227,7 +227,7 @@ var _ I = C(0) //@suggestedfix("C", re"does not implement", issue2606)
227227
-- @issue2606/issue2606.go --
228228
@@ -7 +7,5 @@
229229
+// Error implements I.
230-
+func (C) Error() string {
230+
+func (c C) Error() string {
231231
+ panic("unimplemented")
232232
+}
233233
+
@@ -247,7 +247,7 @@ type multiVar struct{}
247247
@@ -12 +12,5 @@
248248
+
249249
+// Read implements io.Reader.
250-
+func (*multiVar) Read(p []byte) (n int, err error) {
250+
+func (m *multiVar) Read(p []byte) (n int, err error) {
251251
+ panic("unimplemented")
252252
+}
253253
-- pointer.go --
@@ -264,7 +264,7 @@ type pointerImpl struct{}
264264
@@ -10 +10,5 @@
265265
+
266266
+// ReadFrom implements io.ReaderFrom.
267-
+func (*pointerImpl) ReadFrom(r io.Reader) (n int64, err error) {
267+
+func (p *pointerImpl) ReadFrom(r io.Reader) (n int64, err error) {
268268
+ panic("unimplemented")
269269
+}
270270
-- renamed_import.go --
@@ -283,7 +283,7 @@ type myIO struct{}
283283
@@ -12 +12,5 @@
284284
+
285285
+// Reset implements zlib.Resetter.
286-
+func (*myIO) Reset(r myio.Reader, dict []byte) error {
286+
+func (m *myIO) Reset(r myio.Reader, dict []byte) error {
287287
+ panic("unimplemented")
288288
+}
289289
-- renamed_import_iface.go --
@@ -307,7 +307,7 @@ type otherInterfaceImpl struct{}
307307
@@ -14 +16,5 @@
308308
+
309309
+// Get implements other.Interface.
310-
+func (*otherInterfaceImpl) Get(context.Context) *bytes.Buffer {
310+
+func (o *otherInterfaceImpl) Get(context.Context) *bytes.Buffer {
311311
+ panic("unimplemented")
312312
+}
313313
-- stdlib.go --
@@ -324,7 +324,7 @@ type writer struct{}
324324
@@ -10 +10,5 @@
325325
+
326326
+// Write implements io.Writer.
327-
+func (writer) Write(p []byte) (n int, err error) {
327+
+func (w writer) Write(p []byte) (n int, err error) {
328328
+ panic("unimplemented")
329329
+}
330330
-- typedecl_group.go --
@@ -354,12 +354,12 @@ func _() {
354354
-- @typedecl_group/typedecl_group.go --
355355
@@ -18 +18,10 @@
356356
+// Close implements io.ReadCloser.
357-
+func (rdcloser) Close() error {
357+
+func (r rdcloser) Close() error {
358358
+ panic("unimplemented")
359359
+}
360360
+
361361
+// Read implements io.ReadCloser.
362-
+func (rdcloser) Read(p []byte) (n int, err error) {
362+
+func (r rdcloser) Read(p []byte) (n int, err error) {
363363
+ panic("unimplemented")
364364
+}
365365
+

0 commit comments

Comments
 (0)