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

mdempsky opened this issue May 19, 2016 · 3 comments


Copy link

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

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

Copy link

CL 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.
None yet

No branches or pull requests

3 participants