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
The following program should not compile with gc:
package main
type mybool bool
func main() {
var x, y int = 1, 2
var _ bool = x < y && x < y
var _ mybool = x < y && x < y // should be illegal because x < y is of type bool
}
The spec says:
1) "The result of a comparison can be assigned to any boolean type. If the context
does not demand a specific boolean type, the result has type bool." (
http://golang.org/ref/spec#Comparison_operators )
2) "For other (non-comparison) binary operators, the operand types must be
identical unless the operation involves shifts or untyped constants." (
http://golang.org/ref/spec#Operators )
3) "Logical operators apply to boolean values and yield a result of the same type
as the operands." ( http://golang.org/ref/spec#Logical_operators )
4) "In assignments, each value must be assignable to the type of the operand to
which it is assigned." ( http://golang.org/ref/spec#Assignments ).
5) Definition of assignability ( http://golang.org/ref/spec#Assignability ).
Looking at the last declaration in the program:
Per 1), the result of the comparisons x < y is bool (note that x, y are not
constant). Per 2), the && operation is legal because both operands (x < y)
are of the same type bool. Per 3), the result type of the && operation is of the
same type as the operands, which is bool. Per 4) and 5), a bool value is not assignable
to a mybool.
gc compiles and runs the above program w/o complaint. gccgo correctly complains with:
x.go:8:6: error: incompatible type in initialization (cannot use type bool as type
mybool).
(The following program emphasizes that elsewhere in an && operation, an operand
that is a comparison indeed assumes the type bool:
package main
type mybool bool
func main() {
var x, y int = 1, 2
var _ = bool(true) && x < y // this is legal as x < y produces a bool
var _ = mybool(true) && x < y // invalid operation: mybool(true) && x < y (mismatched types mybool and bool)
}
)
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: