From fdf152f8b3e4a777f88e6bea51da8ff353b742e1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 15 Jul 2018 16:08:02 +1000 Subject: [PATCH] Reject "x == 0" for pointer-typed x --- lang/check/type.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lang/check/type.go b/lang/check/type.go index f54f9ff3..da5622db 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -736,17 +736,6 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { rTyp := rhs.MType() switch op { - default: - if !lTyp.IsNumTypeOrIdeal() { - return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", - op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) - } - if !rTyp.IsNumTypeOrIdeal() { - return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", - op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) - } - case t.IDXBinaryNotEq, t.IDXBinaryEqEq: - // No-op. case t.IDXBinaryAnd, t.IDXBinaryOr: if !lTyp.IsBool() { return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", @@ -756,6 +745,20 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) } + case t.IDXBinaryNotEq, t.IDXBinaryEqEq: + if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) { + break + } + fallthrough + default: + if !lTyp.IsNumTypeOrIdeal() { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", + op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) + } + if !rTyp.IsNumTypeOrIdeal() { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", + op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) + } } switch op {