From: Robert Griesemer Date: Wed, 17 May 2023 00:24:16 +0000 (-0700) Subject: go/types, types2: assert that builtin reports valid operand mode upon success X-Git-Tag: go1.21rc1~458 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8e3dfe783ab5ed110b6b338662ee1dee6a7dc51b;p=gostls13.git 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 --- 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 }