| 1 |
This file tests the behavior of the 'fill struct' code action in a Go 1.26 module, |
| 2 |
focusing on how gopls handles direct references to embedded fields in struct literals |
| 3 |
(a feature introduced in Go 1.27, see golang/go#9859). |
| 4 |
|
| 5 |
-- flags -- |
| 6 |
-min_go_command=go1.26 |
| 7 |
-ignore_extra_diags |
| 8 |
|
| 9 |
-- go.mod -- |
| 10 |
module golang.org/lsptests/fillstruct |
| 11 |
|
| 12 |
go 1.26 |
| 13 |
|
| 14 |
-- malformed/methodasfield.go -- |
| 15 |
package malformed |
| 16 |
|
| 17 |
type foo struct { |
| 18 |
A, B, C string |
| 19 |
} |
| 20 |
|
| 21 |
func (f *foo) Bar() {} |
| 22 |
|
| 23 |
// Test that we don't crash when a method is used as a struct literal key. |
| 24 |
var _ = foo{ |
| 25 |
Bar: 0, |
| 26 |
} //@codeaction("}", "refactor.rewrite.fillStruct", edit=methodasfield) |
| 27 |
-- @methodasfield/malformed/methodasfield.go -- |
| 28 |
@@ -12 +12,3 @@ |
| 29 |
+ A: "", |
| 30 |
+ B: "", |
| 31 |
+ C: "", |
| 32 |
-- malformed/fieldconflict.go -- |
| 33 |
package malformed |
| 34 |
|
| 35 |
type a struct { |
| 36 |
b |
| 37 |
a1 string |
| 38 |
a2 string |
| 39 |
} |
| 40 |
|
| 41 |
type b struct { |
| 42 |
b1 string |
| 43 |
} |
| 44 |
|
| 45 |
// In Go 1.26, direct reference to embedded fields (like b1: "") is invalid. |
| 46 |
// gopls treats "b1" as unknown, does not detect a conflict with b, and |
| 47 |
// suggests filling direct fields a1 and a2. |
| 48 |
var _ = a{ |
| 49 |
b: b{}, |
| 50 |
b1: "", |
| 51 |
} //@codeaction("}", "refactor.rewrite.fillStruct", edit=fieldconflict) |
| 52 |
-- @fieldconflict/malformed/fieldconflict.go -- |
| 53 |
@@ -19 +19,2 @@ |
| 54 |
+ a1: "", |
| 55 |
+ a2: "", |
| 56 |
-- malformed/embeddedfields.go -- |
| 57 |
package malformed |
| 58 |
|
| 59 |
type E struct { |
| 60 |
E1, E2 string |
| 61 |
} |
| 62 |
|
| 63 |
type T struct { |
| 64 |
E |
| 65 |
T1, T2 string |
| 66 |
} |
| 67 |
|
| 68 |
// In Go 1.26, direct reference to embedded fields (like E1: "") is invalid. |
| 69 |
// gopls treat "E1" as unknown, does not flatten E, and suggests filling direct |
| 70 |
// fields E, T1, and T2. |
| 71 |
var _ = T{ |
| 72 |
E1: "", |
| 73 |
} //@codeaction("}", "refactor.rewrite.fillStruct", edit=embeddedfieldsgo) |
| 74 |
-- @embeddedfieldsgo/malformed/embeddedfields.go -- |
| 75 |
@@ -17 +17,3 @@ |
| 76 |
+ E: E{}, |
| 77 |
+ T1: "", |
| 78 |
+ T2: "", |