]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: don't panic when checking a ListExpr in exprInternal
authorRob Findley <rfindley@google.com>
Tue, 27 Apr 2021 02:25:56 +0000 (22:25 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 27 Apr 2021 13:46:16 +0000 (13:46 +0000)
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>
src/go/internal/typeparams/notypeparams.go
src/go/internal/typeparams/typeparams.go
src/go/types/expr.go
src/go/types/fixedbugs/issue45635.go2

index 7bd62c9efecca75685ce30df5998c45d2abbf0cf..a8c25ac2b1fb141b138f0e522700cc5a0d70dcbd 100644 (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
 }
index 0332b6b816f5fd7aae6372335a8ebadaf266dd87..66f66afb2845d9e95818027af7e2874a2d6110c9 100644 (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:
index 4023362a4ece6c783fc2ad2da1c1c00052c7a463..7d701d985be2c950e7afaa2d9c1d34b69068978d 100644 (file)
@@ -1805,7 +1805,11 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                // types, which are comparatively rare.
 
        default:
-               panic(fmt.Sprintf("%s: unknown expression type %T", check.fset.Position(e.Pos()), e))
+               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
index 1fbe0382ea5d9b8fed28c184518737a1f08ce647..ec5cf3c5ff93e2db98ff69d7d9eda2a8f1c0f78b 100644 (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" */ ]
+}