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

reflect: the string representation of the type with anonymous fields and created by StructOf looks the fields are not anonymous #25403

Open
dotaheor opened this Issue May 15, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@dotaheor

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"

type MyInt int

func main() {
	type T = struct {
		MyInt
	}
	fmt.Println(reflect.TypeOf(T{})) // struct { main.MyInt }
	
	var n MyInt
	tn := reflect.TypeOf(n)
	// panic: reflect.StructOf: too many methods
	tt := reflect.StructOf([]reflect.StructField{
		{Name: "MyInt", Type: tn, Anonymous: true},
	})
	fmt.Println(tt) // struct { MyInt main.MyInt }
}

What did you expect to see?

struct { main.MyInt }
struct { main.MyInt }

What did you see instead?

struct { main.MyInt }
struct { MyInt main.MyInt }
@dotaheor

This comment has been minimized.

dotaheor commented May 15, 2018

btw, gccgo print:

struct { main.MyInt }
struct { ? main.MyInt }
@odeke-em

This comment has been minimized.

Member

odeke-em commented Jun 1, 2018

@dotaheor

This comment has been minimized.

dotaheor commented Jun 24, 2018

Maybe this problem is caused by the two facts combined:

  1. we can't declare types through reflection now.
  2. Programs compiled with gc don't trace names of alias types at run time.
package main

import "fmt"
import "reflect"

type MyInt = int

func main() {
	type T = struct {
		MyInt
	}
	t := reflect.TypeOf(T{})
	fmt.Println(t) // struct { int }
	                    // The above line should print struct { main.MyInt }.
	                    // gccgo does it right.	
	var n int
	tn := reflect.TypeOf(n)
	// panic: reflect.StructOf: too many methods
	tt := reflect.StructOf([]reflect.StructField{
		{Name: "MyInt", Type: tn, Anonymous: true},
	})
	fmt.Println(tt) // struct { MyInt int }
	                     // The above line should print struct { main.MyInt }.
	fmt.Println(t == tt) // false (should be true)
}
@dotaheor

This comment has been minimized.

dotaheor commented Jun 24, 2018

Possibly related to #24781 and #24721

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment