mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-23 01:11:40 +00:00
fix #13076: codegen error when using anonymous struct
Signed-off-by: cai.li <cai.li@qingteng.cn>
This commit is contained in:
parent
b9f42814b5
commit
2506bf5b06
@ -247,7 +247,7 @@ func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypePara
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if st.Anonymous() {
|
if st.Anonymous() {
|
||||||
w("\t%s %s", fname, tag)
|
w("\t%s %s", qname, tag)
|
||||||
} else {
|
} else {
|
||||||
w("\t%s %s %s", fname, qname, tag)
|
w("\t%s %s %s", fname, qname, tag)
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
package codegen
|
package codegen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go/types"
|
||||||
"log"
|
"log"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@ -174,3 +176,79 @@ func TestGenericContainsPointers(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAssertStructUnchanged(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
t *types.Struct
|
||||||
|
tname string
|
||||||
|
params *types.TypeParamList
|
||||||
|
ctx string
|
||||||
|
it *ImportTracker
|
||||||
|
}
|
||||||
|
|
||||||
|
// package t1 with a struct T1 with two fields
|
||||||
|
p1 := types.NewPackage("t1", "t1")
|
||||||
|
t1 := types.NewNamed(types.NewTypeName(0, p1, "T1", nil), types.NewStruct([]*types.Var{
|
||||||
|
types.NewField(0, nil, "P1", types.Typ[types.Int], false),
|
||||||
|
types.NewField(0, nil, "P2", types.Typ[types.String], false),
|
||||||
|
}, nil), nil)
|
||||||
|
p1.Scope().Insert(t1.Obj())
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "t1-internally_defined",
|
||||||
|
args: args{
|
||||||
|
t: t1.Underlying().(*types.Struct),
|
||||||
|
tname: "prefix_",
|
||||||
|
params: nil,
|
||||||
|
ctx: "",
|
||||||
|
it: NewImportTracker(p1),
|
||||||
|
},
|
||||||
|
want: []byte("var _prefix_NeedsRegeneration = prefix_(struct {\n\tP1 int \n\tP2 string \n}{})"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "t2-with_named_field",
|
||||||
|
args: args{
|
||||||
|
t: types.NewStruct([]*types.Var{
|
||||||
|
types.NewField(0, nil, "T1", t1, false),
|
||||||
|
types.NewField(0, nil, "P1", types.Typ[types.Int], false),
|
||||||
|
types.NewField(0, nil, "P2", types.Typ[types.String], false),
|
||||||
|
}, nil),
|
||||||
|
tname: "prefix_",
|
||||||
|
params: nil,
|
||||||
|
ctx: "",
|
||||||
|
it: NewImportTracker(types.NewPackage("t2", "t2")),
|
||||||
|
},
|
||||||
|
// the struct should be regenerated with the named field
|
||||||
|
want: []byte("var _prefix_NeedsRegeneration = prefix_(struct {\n\tT1 t1.T1 \n\tP1 int \n\tP2 string \n}{})"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "t3-with_embedded_field",
|
||||||
|
args: args{
|
||||||
|
t: types.NewStruct([]*types.Var{
|
||||||
|
types.NewField(0, nil, "T1", t1, true),
|
||||||
|
types.NewField(0, nil, "P1", types.Typ[types.Int], false),
|
||||||
|
types.NewField(0, nil, "P2", types.Typ[types.String], false),
|
||||||
|
}, nil),
|
||||||
|
tname: "prefix_",
|
||||||
|
params: nil,
|
||||||
|
ctx: "",
|
||||||
|
it: NewImportTracker(types.NewPackage("t3", "t3")),
|
||||||
|
},
|
||||||
|
// the struct should be regenerated with the embedded field
|
||||||
|
want: []byte("var _prefix_NeedsRegeneration = prefix_(struct {\n\tt1.T1 \n\tP1 int \n\tP2 string \n}{})"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := AssertStructUnchanged(tt.args.t, tt.args.tname, tt.args.params, tt.args.ctx, tt.args.it); !strings.Contains(string(got), string(tt.want)) {
|
||||||
|
t.Errorf("AssertStructUnchanged() = \n%s\nwant: \n%s", string(got), string(tt.want))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user