1
0
mirror of https://github.com/golang/go synced 2024-11-22 02:34:40 -07:00

gob: report an error when encoding a non-empty struct with

no public fields.
Fix a couple of tests caught out by this change.

R=rsc
CC=golang-dev
https://golang.org/cl/4044043
This commit is contained in:
Rob Pike 2011-01-22 00:10:11 -08:00
parent f0b8f84d37
commit a0a4e85ad6
3 changed files with 12 additions and 13 deletions

View File

@ -1088,11 +1088,11 @@ func (v Vector) Square() int {
}
type Point struct {
a, b int
X, Y int
}
func (p Point) Square() int {
return p.a*p.a + p.b*p.b
return p.X*p.X + p.Y*p.Y
}
// A struct with interfaces in it.

View File

@ -264,9 +264,6 @@ func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
}
}
func encNoOp(i *encInstr, state *encoderState, p unsafe.Pointer) {
}
// Byte arrays are encoded as an unsigned count followed by the raw bytes.
func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
b := *(*[]byte)(p)
@ -516,16 +513,18 @@ func (enc *Encoder) compileEnc(rt reflect.Type) *encEngine {
srt, isStruct := rt.(*reflect.StructType)
engine := new(encEngine)
if isStruct {
engine.instr = make([]encInstr, srt.NumField()+1) // +1 for terminator
for fieldnum := 0; fieldnum < srt.NumField(); fieldnum++ {
f := srt.Field(fieldnum)
op, indir := enc.encOpFor(f.Type)
for fieldNum := 0; fieldNum < srt.NumField(); fieldNum++ {
f := srt.Field(fieldNum)
if !isExported(f.Name) {
op = encNoOp
continue
}
engine.instr[fieldnum] = encInstr{op, fieldnum, indir, uintptr(f.Offset)}
op, indir := enc.encOpFor(f.Type)
engine.instr = append(engine.instr, encInstr{op, fieldNum, indir, uintptr(f.Offset)})
}
engine.instr[srt.NumField()] = encInstr{encStructTerminator, 0, 0, 0}
if srt.NumField() > 0 && len(engine.instr) == 0 {
errorf("type %s has no exported fields", rt)
}
engine.instr = append(engine.instr, encInstr{encStructTerminator, 0, 0, 0})
} else {
engine.instr = make([]encInstr, 1)
op, indir := enc.encOpFor(rt)

View File

@ -220,7 +220,7 @@ func TestSlice(t *testing.T) {
func TestValueError(t *testing.T) {
// Encode a *T, decode a T
type Type4 struct {
a int
A int
}
t4p := &Type4{3}
var t4 Type4 // note: not a pointer.