]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: report an error for x.sel where x is a built-in
authorRobert Griesemer <gri@golang.org>
Fri, 25 Feb 2022 19:10:44 +0000 (11:10 -0800)
committerRobert Griesemer <gri@golang.org>
Sat, 26 Feb 2022 00:21:50 +0000 (00:21 +0000)
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>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/index.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51360.go [new file with mode: 0644]
src/go/types/call.go
src/go/types/index.go
src/go/types/testdata/fixedbugs/issue51360.go [new file with mode: 0644]

index 22f65ed626692689761628677c8bd1d1ffe61e39..d12ee49adbc64904d9b7894c2861d349968d2ddc 100644 (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
        }
 
index 1eaddded9a6cc92da51452805f351f0fd9af527c..61009c121eca37c2675960853b032c60cc453d72 100644 (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
        }
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51360.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51360.go
new file mode 100644 (file)
index 0000000..447ce03
--- /dev/null
@@ -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 */
+}
index 3dab28445965facc07b5deb95fce1bb860279b36..854528ddfa76b1478d5277fa8531840a668c5fef 100644 (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
        }
 
index eac6017ba2154eeadf203839943493df0529d944..33075edaf14c97d3a10cc0ec811496259eacef44 100644 (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
diff --git a/src/go/types/testdata/fixedbugs/issue51360.go b/src/go/types/testdata/fixedbugs/issue51360.go
new file mode 100644 (file)
index 0000000..fe3de04
--- /dev/null
@@ -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 */
+}