Skip to content

Commit 23573d0

Browse files
committed
cmd/compile: clearer error when non-bool used as "||" and "&&" operand
Fixes #41500 Change-Id: I658d8921b7769b6e4288ca781cbdca5ff14a84ee Reviewed-on: https://go-review.googlesource.com/c/go/+/255899 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 8860251 commit 23573d0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/cmd/compile/internal/gc/typecheck.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ func typecheck1(n *Node, top int) (res *Node) {
630630
break
631631
}
632632

633+
// For "x == x && len(s)", it's better to report that "len(s)" (type int)
634+
// can't be used with "&&" than to report that "x == x" (type untyped bool)
635+
// can't be converted to int (see issue #41500).
636+
if n.Op == OANDAND || n.Op == OOROR {
637+
if !n.Left.Type.IsBoolean() {
638+
yyerror("invalid operation: %v (operator %v not defined on %s)", n, n.Op, typekind(n.Left.Type))
639+
n.Type = nil
640+
return n
641+
}
642+
if !n.Right.Type.IsBoolean() {
643+
yyerror("invalid operation: %v (operator %v not defined on %s)", n, n.Op, typekind(n.Right.Type))
644+
n.Type = nil
645+
return n
646+
}
647+
}
648+
633649
// ideal mixed with non-ideal
634650
l, r = defaultlit2(l, r, false)
635651

test/fixedbugs/issue41500.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// errorcheck
2+
3+
// Copyright 2020 The Go Authors. All rights reserved. Use of this
4+
// source code is governed by a BSD-style license that can be found in
5+
// the LICENSE file.
6+
7+
package p
8+
9+
type s struct {
10+
slice []int
11+
}
12+
13+
func f() {
14+
var x *s
15+
16+
_ = x == nil || len(x.slice) // ERROR "invalid operation: .+ \(operator \|\| not defined on int\)"
17+
_ = len(x.slice) || x == nil // ERROR "invalid operation: .+ \(operator \|\| not defined on int\)"
18+
_ = x == nil && len(x.slice) // ERROR "invalid operation: .+ \(operator && not defined on int\)"
19+
_ = len(x.slice) && x == nil // ERROR "invalid operation: .+ \(operator && not defined on int\)"
20+
}

0 commit comments

Comments
 (0)