You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
cmd/compile and gccgo accept this Go source, but go/types rejects it with "cannot compare x == y (mismatched types b and bool)":
My reading of the Go spec is that cmd/compile and gccgo are correct here:
So
x
andy
should have typeb
.So
y
is initialized with an untyped boolean value, which is assignable to the named boolean typeb
.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 thatvar x, y b = i.(b)
should behave identically tovar x, y b; x, y = i.(b)
(which all three tools allow)./cc @griesemer @ianlancetaylor
The text was updated successfully, but these errors were encountered: