Skip to content

OptimizeReflectImplements(?) is causing behaviour differences #3580

Open
@dgryski

Description

@dgryski
~/go/src/github.com/dgryski/bug/anynil $ tinygo run main.go
false
~/go/src/github.com/dgryski/bug/anynil $ go run main.go
true
~/go/src/github.com/dgryski/bug/anynil $ cat main.go
package main

import (
	"encoding/json"
	"reflect"
)

type nilJSONMarshaler string

func (nm *nilJSONMarshaler) MarshalJSON() ([]byte, error) {
	// behavior differs from upstream Go
	return json.Marshal("0zenil0:" + string(*nm))

	// behaviour matches upstream Go
	// return nil, nil
}

var _ = json.Marshaler(nil)

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

var marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()

func main() {
	v := struct{ M Marshaler }{(*nilJSONMarshaler)(nil)}
	f0 := reflect.TypeOf(v).Field(0).Type
	println(f0.Implements(marshalerType))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions