Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions _generated/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,22 @@ type ConvertErrVal string
type ConvertErr struct {
Err ConvertErrVal
}

//msgp:shim ConvertIntVal as:int64 using:fromConvertIntVal/toConvertIntVal mode:convert
//msgp:ignore ConvertIntVal

func fromConvertIntVal(v ConvertIntVal) (int64, error) {
return int64(v), nil
}

func toConvertIntVal(i int64) (ConvertIntVal, error) {
return ConvertIntVal(i), nil
}

type ConvertIntVal int64

// ConvertInt exercises a fixed-size (int64) convert shim, whose generated
// Msgsize must not declare an unused temporary (#446).
type ConvertInt struct {
Int ConvertIntVal
Comment thread
klauspost marked this conversation as resolved.
Outdated
}
20 changes: 20 additions & 0 deletions _generated/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,23 @@ func TestConvertToMarshalError(t *testing.T) {
t.Fatalf("expected conversion error, found %v", err.Error())
}
}

func TestConvertInt(t *testing.T) {
// #446: a fixed-size convert shim must generate a Msgsize that is an
// accurate constant and compiles (no unassigned temporary).
in := ConvertInt{Int: 42}
b, err := in.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
if in.Msgsize() < len(b) {
t.Fatalf("Msgsize %d under-reports marshaled size %d", in.Msgsize(), len(b))
}
var out ConvertInt
if _, err = out.UnmarshalMsg(b); err != nil {
t.Fatal(err)
}
if out != in {
t.Fatalf("round-trip mismatch: %v != %v", out, in)
}
}
27 changes: 17 additions & 10 deletions gen/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,23 @@ func (s *sizeGen) gBase(b *BaseElem) {
return
}
if b.Convert && b.ShimMode == Convert {
s.state = add
vname := randIdent()
s.p.printf("\nvar %s %s", vname, b.BaseType())

// ensure we don't get "unused variable" warnings from outer slice iterations
s.p.printf("\n_ = %s", b.Varname())

s.p.printf("\ns += %s", basesizeExpr(b.Value, vname, b.BaseName()))
s.state = expr

if fixedSize(b.Value) {
// A fixed-size base has a constant wire size, so there is no need
// for a temporary holding the converted value. Emitting an
// (unassigned) temporary left it unused, producing a "declared and
// not used" compile error in the generated Msgsize (#446).
s.addConstant(basesizeExpr(b.Value, "", b.BaseName()))
} else {
s.state = add
vname := randIdent()
s.p.printf("\nvar %s %s", vname, b.BaseType())

// ensure we don't get "unused variable" warnings from outer slice iterations
s.p.printf("\n_ = %s", b.Varname())

s.p.printf("\ns += %s", basesizeExpr(b.Value, vname, b.BaseName()))
s.state = expr
}
Comment thread
klauspost marked this conversation as resolved.
Outdated
} else {
vname := b.Varname()
if b.Convert {
Expand Down
Loading