1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:50:06 -07:00

go/types, types2: report an error for x.sel where x is a built-in

In case of a selector expression x.sel where x is a built-in
we didn't report an error because the type of built-ins is
invalid and we surpress errors on operands of invalid types,
assuming that an error has been reported before.

Add a corresponding check for this case.

Review all places where we call Checker.exprOrType to ensure
(invalid) built-ins are reported.

Adjusted position for index error in types2.

Fixes #51360.

Change-Id: I24693819c729994ab79d31de8fa7bd370b3e8469
Reviewed-on: https://go-review.googlesource.com/c/go/+/388054
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2022-02-25 11:10:44 -08:00
parent 01e522a973
commit 286e3e61aa
6 changed files with 39 additions and 3 deletions

View File

@ -525,7 +525,11 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
}
check.exprOrType(x, e.X, false)
if x.mode == invalid {
switch x.mode {
case builtin:
check.errorf(e.Pos(), "cannot select on %s", x)
goto Error
case invalid:
goto Error
}

View File

@ -182,7 +182,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
}
if !valid {
check.errorf(x, invalidOp+"cannot index %s", x)
check.errorf(e.Pos(), invalidOp+"cannot index %s", x)
x.mode = invalid
return false
}

View File

@ -0,0 +1,13 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
func _() {
len. /* ERROR cannot select on len */ Println
len. /* ERROR cannot select on len */ Println()
_ = len. /* ERROR cannot select on len */ Println
_ = len[ /* ERROR cannot index len */ 0]
_ = *len /* ERROR cannot indirect len */
}

View File

@ -527,7 +527,12 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
}
check.exprOrType(x, e.X, false)
if x.mode == invalid {
switch x.mode {
case builtin:
// types2 uses the position of '.' for the error
check.errorf(e.Sel, _UncalledBuiltin, "cannot select on %s", x)
goto Error
case invalid:
goto Error
}

View File

@ -183,6 +183,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
}
if !valid {
// types2 uses the position of '[' for the error
check.invalidOp(x, _NonIndexableOperand, "cannot index %s", x)
x.mode = invalid
return false

View File

@ -0,0 +1,13 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
func _() {
len.Println /* ERROR cannot select on len */
len.Println /* ERROR cannot select on len */ ()
_ = len.Println /* ERROR cannot select on len */
_ = len /* ERROR cannot index len */ [0]
_ = *len /* ERROR cannot indirect len */
}