1
0
mirror of https://github.com/golang/go synced 2024-09-24 11:20:20 -06:00

encoding/gob: better error messages when types mismatch

The transmitter must encode an interface value if it is to be decoded
into an interface value, but it's a common and confusing error to
encode a concrete value and attempt to decode it into an interface,
particularly *interface{}. This CL attempts to explain things better.

Fixes #2367.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5485072
This commit is contained in:
Rob Pike 2011-12-13 20:40:55 -08:00
parent 1c50c32af0
commit ba576b2b48
2 changed files with 7 additions and 2 deletions

View File

@ -1068,7 +1068,12 @@ func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *de
engine.instr = make([]decInstr, 1) // one item
name := rt.String() // best we can do
if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
return nil, errors.New("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId))
remoteType := dec.typeString(remoteId)
// Common confusing case: local interface type, remote concrete type.
if ut.base.Kind() == reflect.Interface && remoteId != tInterface {
return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType)
}
return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType)
}
op, indir := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
ovfl := errors.New(`value for "` + name + `" out of range`)

View File

@ -309,7 +309,7 @@ var singleTests = []SingleTest{
{[7]int{4, 55, 1, 44, 22, 66, 1234}, &testArray, ""},
// Decode errors
{172, &testFloat32, "wrong type"},
{172, &testFloat32, "type"},
}
func TestSingletons(t *testing.T) {