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: inconsistent results between reflection and non-reflection #35488

Closed
go101 opened this issue Nov 10, 2019 · 1 comment

Comments

@go101
Copy link

@go101 go101 commented Nov 10, 2019

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

$ go version
go version go1.13.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import "reflect"

type (
	FooStruct = struct {
		x int `some:"foo"`
	}
	FooStruct_Ptr *FooStruct

	FooStructDefined struct {
		x int `some:"foo"`
	}
	FooStructDefined_Ptr *FooStructDefined
)

type (
	BarStruct = struct {
		x int `some:"bar"`
	}
	BarStruct_Ptr *BarStruct

	BarStructDefined struct {
		x int `some:"bar"`
	}
	BarStructDefined_Ptr *BarStructDefined
)

func main() {
	var foo    *FooStruct
	var foo_D  FooStruct_Ptr
	var fooD   *FooStructDefined
	var fooD_D FooStructDefined_Ptr
	
	_ = (*FooStruct)(fooD) // ok (correct)
	_ = (*FooStructDefined)(foo) // ok (correct)
	println(reflect.TypeOf(foo).ConvertibleTo(reflect.TypeOf(fooD)))
	println(reflect.TypeOf(fooD).ConvertibleTo(reflect.TypeOf(foo)))
	
	// _ = (*FooStruct)(fooD_D) // error (correct)
	// _ = (*FooStructDefined_Ptr)(foo) // error (correct)
	println(reflect.TypeOf(foo).ConvertibleTo(reflect.TypeOf(fooD_D)))
	println(reflect.TypeOf(fooD_D).ConvertibleTo(reflect.TypeOf(foo)))
	
	// _ = (*FooStruct_Ptr)(fooD) // error (correct)
	// _ = (*FooStructDefined)(foo_D) // error (correct)
	println(reflect.TypeOf(fooD).ConvertibleTo(reflect.TypeOf(foo_D)))
	println(reflect.TypeOf(foo_D).ConvertibleTo(reflect.TypeOf(fooD)))
	
	// _ = (*FooStruct_Ptr)(fooD_D) // error (correct)
	// _ = (*FooStructDefined_Ptr)(foo_D) // error (correct)
	println(reflect.TypeOf(fooD_D).ConvertibleTo(reflect.TypeOf(foo_D)))
	println(reflect.TypeOf(foo_D).ConvertibleTo(reflect.TypeOf(fooD_D)))
	
	var bar    *BarStruct
	var bar_D  BarStruct_Ptr
	var barD   *BarStructDefined
	var barD_D BarStructDefined_Ptr
	_ = barD_D
	
	_ = (*FooStruct)(bar) // ok (correct)
	_ = (*FooStruct)(bar_D) // ok (correct)
	_ = (*FooStruct)(barD) // ok (correct, when ignoring tags, distict underlying types but identical base types, and they are both non-defined)
	//_ = ( *FooStruct)(barD_D) // error (correct)
	println(reflect.TypeOf(bar).ConvertibleTo(reflect.TypeOf(foo)))
	println(reflect.TypeOf(bar_D).ConvertibleTo(reflect.TypeOf(foo)))
	println(reflect.TypeOf(barD).ConvertibleTo(reflect.TypeOf(foo)))
	println(reflect.TypeOf(barD_D).ConvertibleTo(reflect.TypeOf(foo)))
	
	_ = FooStruct_Ptr(bar) // ok (correct)
	_ = FooStruct_Ptr(bar_D) // ok (correct)
	//_ = FooStruct_Ptr(barD) // error (correct)
	//_ = FooStruct_Ptr(barD_D) // error (correct)
	println(reflect.TypeOf(bar).ConvertibleTo(reflect.TypeOf(foo_D)))
	println(reflect.TypeOf(bar_D).ConvertibleTo(reflect.TypeOf(foo_D)))
	println(reflect.TypeOf(barD).ConvertibleTo(reflect.TypeOf(foo_D)))
	println(reflect.TypeOf(barD_D).ConvertibleTo(reflect.TypeOf(foo_D)))
	
	_ = (*FooStructDefined)(bar) // ok (correct, when ignoring tags, distict underlying types but identical base types, and they are both non-defined)
	//_ = (*FooStructDefined)(bar_D) // error (correct)
	//_ = (*FooStructDefined)(barD) // error (correct)
	//_ = (*FooStructDefined)(barD_D) // error (correct)
	println(reflect.TypeOf(bar).ConvertibleTo(reflect.TypeOf(fooD)))
	println(reflect.TypeOf(bar_D).ConvertibleTo(reflect.TypeOf(fooD)))
	println(reflect.TypeOf(barD).ConvertibleTo(reflect.TypeOf(fooD))) // true (wrong)
	println(reflect.TypeOf(barD_D).ConvertibleTo(reflect.TypeOf(fooD)))
	
	//_ = FooStructDefined_Ptr(bar) //  error (correct)
	//_ = FooStructDefined_Ptr(bar_D) // error (correct)
	//_ = FooStructDefined_Ptr(barD) // error (correct)
	//_ = FooStructDefined_Ptr(barD_D) // error (correct)
	println(reflect.TypeOf(bar).ConvertibleTo(reflect.TypeOf(fooD_D)))
	println(reflect.TypeOf(bar_D).ConvertibleTo(reflect.TypeOf(fooD_D)))
	println(reflect.TypeOf(barD).ConvertibleTo(reflect.TypeOf(fooD_D)))
	println(reflect.TypeOf(barD_D).ConvertibleTo(reflect.TypeOf(fooD_D)))
}

What did you expect to see?

The // true (wrong) line should print false.

@go101

This comment has been minimized.

Copy link
Author

@go101 go101 commented Nov 10, 2019

Looks I made a mistake here. The line _ = (*FooStructDefined)(barD) // error (correct) really compiles. So closed.

@go101 go101 closed this Nov 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.