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
Closed

reflect: inconsistent results between reflection and non-reflection #35488

go101 opened this issue Nov 10, 2019 · 1 comment

Comments

@go101
Copy link

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

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 as completed Nov 10, 2019
@golang golang locked and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants