Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Shadowing promoted methods with a struct field is not working #1228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flimzy opened this issue Aug 28, 2023 · 1 comment
Open

Shadowing promoted methods with a struct field is not working #1228

flimzy opened this issue Aug 28, 2023 · 1 comment

Comments

@flimzy
Copy link
Member

flimzy commented Aug 28, 2023

It's unclear yet whether this is a bug in the GopherJS compiler, or some customizations we've made either to reflect or encoding/json. I'll try to investigate when I have time. But consider the following code:

package main

import (
	"encoding/json"
	"fmt"
)

type Foo struct {
	Foo string `json:"foo"`
}

func (f *Foo) UnmarshalJSON(p []byte) error {
	t := struct {
		Foo
		UnmarshalJSON struct{}
	}{}
	return json.Unmarshal(p, &t)
}

func main() {
	input := []byte(`{"foo":"a string"}`)
	var f Foo
	_ = json.Unmarshal(input, &f)
	fmt.Println(f)
}

In the Go playground, it works as expected, with the UnmarshalJSON struct field shadowing the promoted UnmarshalJSON method on the embedded Foo type, and thus producing the output {}.

However, in GopherJS, it appears we attempt to execute the non-function, and produce a runtime panic: h.UnmarshalJSON is not a function.

@nevkontakte
Copy link
Member

That does sound like a bug. I think there are a fair few issues around edge cases of struct embedding handling and this looks like one of them.

@nevkontakte nevkontakte added this to the Go spec compliance milestone Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants