From c9f43507c6d8106646b1262052cc9a2c5dbb6e4c Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 27 Apr 2021 23:01:16 +0700 Subject: cmd/compile: fix typechecking logical operators panic with non-boolean operand In CL 255899, we added code to make clearer error when non-bool used as operand to logical operators. The code is safe, because node type is guaranteed to be non-nil. In CL 279442, we refactored typechecking arith, including moving typechecking logical operators to separate case. Now we have to explicitly check if operand type is not nil, because calling Expr can set operand type nil for non-bool operands. Fixes #45804 Change-Id: Ie2b6e18f65c0614a803b343f60e78ee1d660bbeb Reviewed-on: https://go-review.googlesource.com/c/go/+/314209 Trust: Cuong Manh Le Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/typecheck/typecheck.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/cmd/compile/internal/typecheck/typecheck.go') diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index ab493e0caa..00dd44b96b 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -596,6 +596,10 @@ func typecheck1(n ir.Node, top int) ir.Node { case ir.OANDAND, ir.OOROR: n := n.(*ir.LogicalExpr) n.X, n.Y = Expr(n.X), Expr(n.Y) + if n.X.Type() == nil || n.Y.Type() == nil { + n.SetType(nil) + return n + } // For "x == x && len(s)", it's better to report that "len(s)" (type int) // can't be used with "&&" than to report that "x == x" (type untyped bool) // can't be converted to int (see issue #41500). -- cgit v1.2.3-54-g00ecf