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
Assignees
Milestone

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented May 19, 2016

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

@mdempsky mdempsky commented May 19, 2016

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.

Loading

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

@griesemer griesemer commented May 21, 2016

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

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented May 21, 2016

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

Loading

@gopherbot gopherbot closed this in 30fc940 May 23, 2016
@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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants