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

go/types: "var x, y b = i.(b)" should declare 'y' with type 'b', not 'bool' #15755

Closed
mdempsky opened this issue May 19, 2016 · 3 comments
Closed
Milestone

Comments

@mdempsky
Copy link
Member

cmd/compile and gccgo accept this Go source, but go/types rejects it with "cannot compare x == y (mismatched types b and bool)":

package p

func f() bool {
    var i interface{}
    type b bool
    var x, y b = i.(b)
    return x == y
}

My reading of the Go spec is that cmd/compile and gccgo are correct here:

If a type is present, each variable is given that type.

So x and y should have type b.

A type assertion used in an assignment or initialization of the special form [...] yields an additional untyped boolean value.

So y is initialized with an untyped boolean value, which is assignable to the named boolean type b.

Arguably the syntax var x, y b = i.(b) isn't actually one of the three "special forms" described in the Go spec, but none of cmd/compile, go/types, or gccgo complain about that. It seems reasonable to expect that var x, y b = i.(b) should behave identically to var x, y b; x, y = i.(b) (which all three tools allow).

/cc @griesemer @ianlancetaylor

@mdempsky mdempsky added this to the Go1.8 milestone May 19, 2016
@mdempsky
Copy link
Member Author

Aside: When I implemented 1a94431 to extend cmd/cgo's "special form"-like logic for returning errno as a second value to work in variable declarations, I was wondering whether "var x, y b = i.(b)" was actually one of Go's "special forms". Since the compilers and go/types both experimentally accepted it, I accepted it in cmd/cgo too.

@griesemer griesemer self-assigned this May 19, 2016
@griesemer
Copy link
Contributor

@mdempsky I believe those special forms are also valid. I filed #15782.

@gopherbot
Copy link

CL https://golang.org/cl/23313 mentions this issue.

@golang golang locked and limited conversation to collaborators May 23, 2017
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

3 participants