From 8e3dfe783ab5ed110b6b338662ee1dee6a7dc51b Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 16 May 2023 17:24:16 -0700 Subject: [PATCH] go/types, types2: assert that builtin reports valid operand mode upon success Fix a case where x.mode == invalid was returned despite builtin returning true. Change-Id: Iae9c18aac16bcbadc3530d341b380e05c8743fcc Reviewed-on: https://go-review.googlesource.com/c/go/+/495299 Auto-Submit: Robert Griesemer Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot --- src/cmd/compile/internal/types2/builtins.go | 14 +++++++++----- src/go/types/builtins.go | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go index 63b62a66d2..de74da0bac 100644 --- a/src/cmd/compile/internal/types2/builtins.go +++ b/src/cmd/compile/internal/types2/builtins.go @@ -200,12 +200,15 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) ( } } - if mode == invalid && under(x.typ) != Typ[Invalid] { - code := InvalidCap - if id == _Len { - code = InvalidLen + if mode == invalid { + // avoid error if underlying type is invalid + if under(x.typ) != Typ[Invalid] { + code := InvalidCap + if id == _Len { + code = InvalidLen + } + check.errorf(x, code, invalidArg+"%s for %s", x, bin.name) } - check.errorf(x, code, invalidArg+"%s for %s", x, bin.name) return } @@ -850,6 +853,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) ( unreachable() } + assert(x.mode != invalid) return true } diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go index 63a59262df..64d34e51cb 100644 --- a/src/go/types/builtins.go +++ b/src/go/types/builtins.go @@ -201,12 +201,15 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b } } - if mode == invalid && under(x.typ) != Typ[Invalid] { - code := InvalidCap - if id == _Len { - code = InvalidLen + if mode == invalid { + // avoid error if underlying type is invalid + if under(x.typ) != Typ[Invalid] { + code := InvalidCap + if id == _Len { + code = InvalidLen + } + check.errorf(x, code, invalidArg+"%s for %s", x, bin.name) } - check.errorf(x, code, invalidArg+"%s for %s", x, bin.name) return } @@ -851,6 +854,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b unreachable() } + assert(x.mode != invalid) return true } -- 2.50.0