Skip to content
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
Labels
NeedsInvestigation
Milestone

Comments

@dotaheor
Copy link

@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
Copy link
Author

@dotaheor dotaheor commented May 15, 2018

btw, gccgo print:

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

@ianlancetaylor ianlancetaylor added the NeedsInvestigation label May 15, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone May 15, 2018
@odeke-em
Copy link
Member

@odeke-em odeke-em commented Jun 1, 2018

/cc @crawshaw @josharian

@dotaheor
Copy link
Author

@dotaheor 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
Copy link
Author

@dotaheor dotaheor commented Jun 24, 2018

Possibly related to #24781 and #24721

@ianlancetaylor ianlancetaylor removed this from the Go1.11 milestone Jun 28, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 28, 2018
@andybons andybons removed this from the Go1.12 milestone Feb 12, 2019
@andybons andybons added this to the Go1.13 milestone Feb 12, 2019
@andybons andybons removed this from the Go1.13 milestone Jul 8, 2019
@andybons andybons added this to the Go1.14 milestone Jul 8, 2019
@rsc rsc removed this from the Go1.14 milestone Oct 9, 2019
@rsc rsc added this to the Backlog milestone Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

5 participants