1
0
mirror of https://github.com/golang/go synced 2024-11-26 06:27:58 -07:00

go/types: don't panic when checking a ListExpr in exprInternal

As an alternative to CL 312149, add a catch-all error message in
exprInternal when encountering a ListExpr, rather than panicking.

We still might want something like CL 312149 to improve the error
message or recovery from bad indexing.

Change-Id: I865f7cc4eefa4a3b7bd8f3100df96d0144e1712f
Reviewed-on: https://go-review.googlesource.com/c/go/+/313909
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-04-26 22:25:56 -04:00 committed by Robert Findley
parent 6edd573218
commit 39844971fb
4 changed files with 37 additions and 1 deletions

View File

@ -30,6 +30,10 @@ func UnpackExpr(expr ast.Expr) []ast.Expr {
return []ast.Expr{expr}
}
func IsListExpr(n ast.Node) bool {
return false
}
func Get(ast.Node) *ast.FieldList {
return nil
}

View File

@ -38,6 +38,11 @@ func UnpackExpr(x ast.Expr) []ast.Expr {
return nil
}
func IsListExpr(n ast.Node) bool {
_, ok := n.(*ast.ListExpr)
return ok
}
func Get(n ast.Node) *ast.FieldList {
switch n := n.(type) {
case *ast.TypeSpec:

View File

@ -1805,8 +1805,12 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
// types, which are comparatively rare.
default:
if typeparams.IsListExpr(e) {
check.errorf(e, _Todo, "invalid multi-index expression")
} else {
panic(fmt.Sprintf("%s: unknown expression type %T", check.fset.Position(e.Pos()), e))
}
}
// everything went well
x.expr = e

View File

@ -7,3 +7,26 @@ package main
func main() {
some /* ERROR "undeclared name" */ [int, int]()
}
type N[T any] struct{}
var _ N /* ERROR "0 arguments but 1 type parameters" */ []
type I interface {
type map[int]int, []int
}
func _[T I]() {
var m map[int]int
_ = m[1 /* ERROR "multi-index expression" */, 2 /* ERROR "expected type" */ ]
var a [3]int
_ = a[1 /* ERROR "multi-index expression" */, 2 /* ERROR "expected type" */ ]
var s []int
_ = s[1 /* ERROR "multi-index expression" */, 2 /* ERROR "expected type" */ ]
var t T
// TODO(rFindley) Fix the duplicate error below.
_ = t[1 /* ERROR "multi-index expression" */ /* ERROR "multi-index expression" */, 2 /* ERROR "expected type" */ ]
}