Skip to content

Commit 286e3e6

Browse files
committed
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 01e522a commit 286e3e6

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

src/cmd/compile/internal/types2/call.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
525525
}
526526

527527
check.exprOrType(x, e.X, false)
528-
if x.mode == invalid {
528+
switch x.mode {
529+
case builtin:
530+
check.errorf(e.Pos(), "cannot select on %s", x)
531+
goto Error
532+
case invalid:
529533
goto Error
530534
}
531535

src/cmd/compile/internal/types2/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
182182
}
183183

184184
if !valid {
185-
check.errorf(x, invalidOp+"cannot index %s", x)
185+
check.errorf(e.Pos(), invalidOp+"cannot index %s", x)
186186
x.mode = invalid
187187
return false
188188
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
func _() {
8+
len. /* ERROR cannot select on len */ Println
9+
len. /* ERROR cannot select on len */ Println()
10+
_ = len. /* ERROR cannot select on len */ Println
11+
_ = len[ /* ERROR cannot index len */ 0]
12+
_ = *len /* ERROR cannot indirect len */
13+
}

src/go/types/call.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,12 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
527527
}
528528

529529
check.exprOrType(x, e.X, false)
530-
if x.mode == invalid {
530+
switch x.mode {
531+
case builtin:
532+
// types2 uses the position of '.' for the error
533+
check.errorf(e.Sel, _UncalledBuiltin, "cannot select on %s", x)
534+
goto Error
535+
case invalid:
531536
goto Error
532537
}
533538

src/go/types/index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
183183
}
184184

185185
if !valid {
186+
// types2 uses the position of '[' for the error
186187
check.invalidOp(x, _NonIndexableOperand, "cannot index %s", x)
187188
x.mode = invalid
188189
return false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
func _() {
8+
len.Println /* ERROR cannot select on len */
9+
len.Println /* ERROR cannot select on len */ ()
10+
_ = len.Println /* ERROR cannot select on len */
11+
_ = len /* ERROR cannot index len */ [0]
12+
_ = *len /* ERROR cannot indirect len */
13+
}

0 commit comments

Comments
 (0)