diff --git a/encode_test.go b/encode_test.go index 3fd297e..90cb67b 100644 --- a/encode_test.go +++ b/encode_test.go @@ -2148,3 +2148,23 @@ func TestEmptyStructInterface(t *testing.T) { t.Fatalf("failed to encode empty struct interface. expected:[%q] but got:[%q]", expected, got) } } + +func TestIssue290(t *testing.T) { + type Issue290 interface { + A() + } + var a struct { + A Issue290 + } + expected, err := stdjson.Marshal(a) + if err != nil { + t.Fatal(err) + } + got, err := json.Marshal(a) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(expected, got) { + t.Fatalf("failed to encode non empty interface. expected = %q but got %q", expected, got) + } +} \ No newline at end of file diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index 5857249..eccb961 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr - typ = iface.itab.typ + if iface.itab != nil { + typ = iface.itab.typ + } } else { iface := (*emptyInterface)(up) ifacePtr = iface.ptr diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 5857249..6853dc4 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr - typ = iface.itab.typ + if iface.itab != nil { + typ = iface.itab.typ + } } else { iface := (*emptyInterface)(up) ifacePtr = iface.ptr diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index 73af884..256781d 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr - typ = iface.itab.typ + if iface.itab != nil { + typ = iface.itab.typ + } } else { iface := (*emptyInterface)(up) ifacePtr = iface.ptr diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index 7b7844e..c9011c8 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr - typ = iface.itab.typ + if iface.itab != nil { + typ = iface.itab.typ + } } else { iface := (*emptyInterface)(up) ifacePtr = iface.ptr diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index 6e05155..2502216 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr - typ = iface.itab.typ + if iface.itab != nil { + typ = iface.itab.typ + } } else { iface := (*emptyInterface)(up) ifacePtr = iface.ptr