Skip to content

Commit

Permalink
Fix allownil propagated to byte slices as slice elements
Browse files Browse the repository at this point in the history
`[][]byte` would assume that the byte slices had allownil set. This would cause an extra `if x == nil {x = make([]byte,0)}` to be inserted on elements.

Regression from #363
  • Loading branch information
klauspost committed Oct 22, 2024
1 parent 6da1c88 commit 483195a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions _generated/omitempty.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type OmitEmpty0 struct {

APtrNamedStr *NamedString `msg:"aptrnamedstr,omitempty"`

AString string `msg:"astring,omitempty"`
ANamedString string `msg:"anamedstring,omitempty"`
AByteSlice []byte `msg:"abyteslice,omitempty"`
AString string `msg:"astring,omitempty"`
ANamedString string `msg:"anamedstring,omitempty"`
AByteSlice []byte `msg:"abyteslice,omitempty"`
ASliceByteSlice [][]byte `msg:"aslicebyteslice,omitempty"`
AMapByteSlice map[string][]byte `msg:"amapbyteslice,omitempty"`

ASliceString []string `msg:"aslicestring,omitempty"`
ASliceNamedString []NamedString `msg:"aslicenamedstring,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions gen/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ func (p *printer) closeblock() { p.print("\n}") }
// }
func (p *printer) rangeBlock(ctx *Context, idx string, iter string, t traversal, inner Elem) {
ctx.PushVar(idx)
// Tags on slices do not extend to the elements, so we always disable allownil on elements.
// If we want this to happen in the future, it should be a unique tag.
type an interface {
SetIsAllowNil(b bool)
}
if set, ok := inner.(an); ok {
set.SetIsAllowNil(false)
}
p.printf("\n for %s := range %s {", idx, iter)
next(t, inner)
p.closeblock()
Expand Down
2 changes: 1 addition & 1 deletion msgp/write_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func BenchmarkAppendFloat(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendFloat64(buf, src[i&(n-1)])
AppendFloat(buf, src[i&(n-1)])
}
}

Expand Down

0 comments on commit 483195a

Please sign in to comment.