Skip to content

reflect: StructOf with interface fields panics at run time #25400

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

Closed
dotaheor opened this issue May 15, 2018 · 2 comments
Closed

reflect: StructOf with interface fields panics at run time #25400

dotaheor opened this issue May 15, 2018 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@dotaheor
Copy link

dotaheor commented May 15, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.2 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

package main

import "fmt"
import "reflect"

func main() {
	type T = struct {
		error
	}
	fmt.Println(reflect.TypeOf(T{})) // struct { error }
	
	var err error
	te := reflect.TypeOf(err)
	// panic: reflect.StructOf: field 0 has no type
	tt := reflect.StructOf([]reflect.StructField{
		{Name: "E", Type: te, Anonymous: true},
	})
	fmt.Println(tt)
}

What did you expect to see?

not panic

What did you see instead?

panic

@dotaheor
Copy link
Author

Looks, it also panics for non-anonymous interface fields.

@dotaheor dotaheor changed the title reflect: StructOf with anonymous interface fields panics at run time reflect: StructOf with interface fields panics at run time May 15, 2018
@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels May 15, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone May 15, 2018
@dotaheor
Copy link
Author

dotaheor commented May 15, 2018

sorry, It looks I made a mistake for this example.
This is not a bug.

package main

import "fmt"
import "reflect"

func main() {
	type T = struct {
		error
	}
	fmt.Println(reflect.TypeOf(T{})) // struct { error }
	
	var err *error
	te := reflect.TypeOf(err).Elem()
	// panic: reflect.StructOf: field 0 has no type
	tt := reflect.StructOf([]reflect.StructField{
		{Name: "E", Type: te, Anonymous: true},
	})
	fmt.Println(tt) // struct { E error }
}

The only problem of it is the string presentation of tt,
which is a duplication of #25403

@golang golang locked and limited conversation to collaborators May 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants