]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: better error messages when types mismatch
authorRob Pike <r@golang.org>
Wed, 14 Dec 2011 04:40:55 +0000 (20:40 -0800)
committerRob Pike <r@golang.org>
Wed, 14 Dec 2011 04:40:55 +0000 (20:40 -0800)
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

src/pkg/encoding/gob/decode.go
src/pkg/encoding/gob/encoder_test.go

index 1515d1286d093d6a87990441195cc428ea645e75..ba1f2eb813078b823926d82a17d01e37dda492e9 100644 (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`)
index 5bc957bb37011eb144eaf6248da2e776f1d84f8c..cd1500d0772546afdad8427c3bb9b3cf5807a0b3 100644 (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) {