Skip to content

Commit 65798b6

Browse files
authored
Fix allownil propagated to byte slices as slice elements (#374)
`[][]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
1 parent 6da1c88 commit 65798b6

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

_generated/omitempty.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ type OmitEmpty0 struct {
2929

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

32-
AString string `msg:"astring,omitempty"`
33-
ANamedString string `msg:"anamedstring,omitempty"`
34-
AByteSlice []byte `msg:"abyteslice,omitempty"`
32+
AString string `msg:"astring,omitempty"`
33+
ANamedString string `msg:"anamedstring,omitempty"`
34+
AByteSlice []byte `msg:"abyteslice,omitempty"`
35+
ASliceByteSlice [][]byte `msg:"aslicebyteslice,omitempty"`
36+
AMapByteSlice map[string][]byte `msg:"amapbyteslice,omitempty"`
3537

3638
ASliceString []string `msg:"aslicestring,omitempty"`
3739
ASliceNamedString []NamedString `msg:"aslicenamedstring,omitempty"`

gen/spec.go

+8
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ func (p *printer) closeblock() { p.print("\n}") }
385385
// }
386386
func (p *printer) rangeBlock(ctx *Context, idx string, iter string, t traversal, inner Elem) {
387387
ctx.PushVar(idx)
388+
// Tags on slices do not extend to the elements, so we always disable allownil on elements.
389+
// If we want this to happen in the future, it should be a unique tag.
390+
type an interface {
391+
SetIsAllowNil(b bool)
392+
}
393+
if set, ok := inner.(an); ok {
394+
set.SetIsAllowNil(false)
395+
}
388396
p.printf("\n for %s := range %s {", idx, iter)
389397
next(t, inner)
390398
p.closeblock()

msgp/write_bytes_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func BenchmarkAppendFloat(b *testing.B) {
199199
b.ReportAllocs()
200200
b.ResetTimer()
201201
for i := 0; i < b.N; i++ {
202-
AppendFloat64(buf, src[i&(n-1)])
202+
AppendFloat(buf, src[i&(n-1)])
203203
}
204204
}
205205

0 commit comments

Comments
 (0)