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

cmd/gc: inconsistent result type for logical operations #3924

Closed
griesemer opened this issue Aug 7, 2012 · 3 comments
Closed

cmd/gc: inconsistent result type for logical operations #3924

griesemer opened this issue Aug 7, 2012 · 3 comments
Milestone

Comments

@griesemer
Copy link
Contributor

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)
}
)
@griesemer
Copy link
Contributor Author

Comment 1:

Labels changed: added priority-soon, removed priority-later.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 2:

Labels changed: added go1.1.

@DanielMorsing
Copy link
Contributor

Comment 3:

This issue was closed by revision 5188c0b.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
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

4 participants