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
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dotaheor
Copy link

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

btw, gccgo print:

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

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 15, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone May 15, 2018
@odeke-em
Copy link
Member

odeke-em commented Jun 1, 2018

/cc @crawshaw @josharian

@dotaheor
Copy link
Author

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

Possibly related to #24781 and #24721

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 28, 2018
@andybons andybons modified the milestones: Go1.12, Go1.13 Feb 12, 2019
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: Triage Backlog
Development

No branches or pull requests

6 participants