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/compile: confusing error message for non-bool comparison #41500

Closed
smasher164 opened this issue Sep 19, 2020 · 3 comments
Closed

cmd/compile: confusing error message for non-bool comparison #41500

smasher164 opened this issue Sep 19, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@smasher164
Copy link
Member

What version of Go are you using (go version)?

go version go1.15.2 darwin/amd64

What did you do?

I tried to compile the following program: https://play.golang.org/p/k1xIda2_lTk

package main

type Struct struct {
	Slice []int
}

func main() {
	var x *Struct
	if x == nil || len(x.Slice) {
	}
}

I forgot to add len(x.Slice) == 0.

What did you expect to see?

non-bool len(x.Slice) (type int) used as if condition

What did you see instead?

cannot use x == nil (type untyped bool) as type int
@smasher164 smasher164 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 19, 2020
@cuonglm
Copy link
Member

cuonglm commented Sep 20, 2020

This is at least, working as intended per Go spec. x == nil yields an untyped bool, len(x.Slice) yields an int, so x == nil is first converted to int, which produces that error.

I'm not sure is there anything we should fix here cc @mdempsky

@mdempsky
Copy link
Member

The issue here is when type-checking binary operations like X op Y, the Go compiler:

  1. Checks that X and Y have the same type.
  2. Checks that op is a valid operation on X and Y's type.

As @cuonglm points out, x == nil has type "untyped bool", so we try to convert it to "int". But that's not allowed, hence the error.

In this case though, that leads to a slightly suboptimal error message. Because || and && are only valid on boolean types anyway, so it's perhaps a bit misleading to users to focus on converting "untyped bool" to int, rather than not being able to use int as a bool.

I suspect a check like:

if n.Op == OANDAND || n.Op == OOROR {
    if !n.Left.Type.IsBoolean() {
        yyerror("non-bool %L used as %v operand", n.Left, n.Op)
        n.Type = nil
        break
    }
    if !n.Right.Type.IsBoolean() { ... }
}

before the defaultlit2 call might give an error message similar to the suggested one.

@gopherbot
Copy link

Change https://golang.org/cl/255899 mentions this issue: cmd/compile: clearer error when non-bool used as "||" and "&&" operand

@golang golang locked and limited conversation to collaborators Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants