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: type identity failure in ChanOf corner case #39897

Open
TheCount opened this issue Jun 28, 2020 · 2 comments · May be fixed by #39898
Open

reflect: type identity failure in ChanOf corner case #39897

TheCount opened this issue Jun 28, 2020 · 2 comments · May be fixed by #39898
Labels
Milestone

Comments

@TheCount
Copy link

@TheCount TheCount commented Jun 28, 2020

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

$ go version
go version devel +c875503cf7 Sun Jun 28 03:14:10 2020 +0000 linux/amd64

Does this issue reproduce with the latest release?

It does on go1.14, haven't checked the most recent minor release.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
(redacted some stuff)

What did you do?

https://play.golang.org/p/YZG9p_qVnyC

package main

import(
        "reflect"
)

type StandsOut int // so we can grep for it

func main() {
        var a chan (<-chan StandsOut)
        var b chan<- (chan StandsOut)
        var c <-chan (<-chan StandsOut)
        ta := reflect.ChanOf(
                reflect.BothDir, reflect.ChanOf(
                        reflect.RecvDir, reflect.TypeOf(StandsOut(0)),
                ),
        )
        tb := reflect.ChanOf(
                reflect.SendDir, reflect.ChanOf(
                        reflect.BothDir, reflect.TypeOf(StandsOut(0)),
                ),
        )
        tc := reflect.ChanOf(
                reflect.RecvDir, reflect.ChanOf(
                        reflect.RecvDir, reflect.TypeOf(StandsOut(0)),
                ),
        )
        println("ta == reflect.TypeOf(a)", ta == reflect.TypeOf(a))
        println("tb == reflect.TypeOf(b)", tb == reflect.TypeOf(b))
        println("tc == reflect.TypeOf(c)", tc == reflect.TypeOf(c))
}

What did you expect to see?

ta == reflect.TypeOf(a) true
tb == reflect.TypeOf(b) true
tc == reflect.TypeOf(c) true

What did you see instead?

ta == reflect.TypeOf(a) false
tb == reflect.TypeOf(b) true
tc == reflect.TypeOf(c) true

This bug appears to be known but I couldn't find a corresponding github issue.

When grepping the resulting binary, the type of a in the above program appears to be recorded as chan (<-chan main.StandsOut), so the solution might be as simple as building additional parentheses around typ.string() in the implementation of reflect.ChanOf whenever the outer chan is BothDir and the inner chan is RecvDir.

I'll try to fix it.

@TheCount
Copy link
Author

@TheCount TheCount commented Jun 28, 2020

so the solution might be as simple as building additional parentheses around typ.string() in the implementation of reflect.ChanOf whenever the outer chan is BothDir and the inner chan is RecvDir.

… and inner chan type is not a defined type.

TheCount pushed a commit to TheCount/go that referenced this issue Jun 28, 2020
The existing implementation of ChanOf omits the parentheses when
building the type string for channel types of the form
chan (<-chan T), so add them.

Fixes golang#39897
@gopherbot
Copy link

@gopherbot gopherbot commented Jun 28, 2020

Change https://golang.org/cl/240280 mentions this issue: reflect: fix direction association in ChanOf

@ianlancetaylor ianlancetaylor added this to the Go1.16 milestone Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.