]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: guard against checking instantiation when generics is disabled
authorRob Findley <rfindley@google.com>
Tue, 22 Jun 2021 00:16:37 +0000 (20:16 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 22 Jun 2021 11:31:57 +0000 (11:31 +0000)
When type checking t[_], where t is a type name, it was possible to leak
an error message related to generics. Fix this by guarding on
typeparams.Enabled.

In order to test this fix, we need to be able to run the new go/types
test only if type parameters are disabled. Introduce the .go1 test data
suffix (similar to .go2) to control this behavior.

Originally found via fuzzing, though the test case was manually
simplified.

Updates #46404

Change-Id: Ib1e2c27cf974c2a5ca5b9d6d01b84a30ba4d583b
Reviewed-on: https://go-review.googlesource.com/c/go/+/329793
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/types/check_test.go
src/go/types/testdata/fixedbugs/issue46404.go1 [new file with mode: 0644]
src/go/types/typexpr.go

index 6c3b630a1b6ef54b075caec94d1595185534ec1d..5a3e57ba44d39cd5d4a7a3298381a5a3c7cf9b92 100644 (file)
@@ -207,12 +207,15 @@ func checkFiles(t *testing.T, sizes Sizes, goVersion string, filenames []string,
                t.Fatal("no source files")
        }
 
+       if strings.HasSuffix(filenames[0], ".go2") && !typeparams.Enabled {
+               t.Skip("type params are not enabled")
+       }
+       if strings.HasSuffix(filenames[0], ".go1") && typeparams.Enabled {
+               t.Skip("type params are enabled")
+       }
+
        mode := parser.AllErrors
-       if strings.HasSuffix(filenames[0], ".go2") {
-               if !typeparams.Enabled {
-                       t.Skip("type params are not enabled")
-               }
-       } else {
+       if !strings.HasSuffix(filenames[0], ".go2") {
                mode |= typeparams.DisallowParsing
        }
 
diff --git a/src/go/types/testdata/fixedbugs/issue46404.go1 b/src/go/types/testdata/fixedbugs/issue46404.go1
new file mode 100644 (file)
index 0000000..db604bc
--- /dev/null
@@ -0,0 +1,8 @@
+// Copyright 2021 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 issue46404
+
+// Check that we don't type check t[_] as an instantiation.
+type t [t /* ERROR not a type */ [_]]_ // ERROR cannot use
index 5185c33fcbf012cd0a3d95ad317cc25fbee4f0f2..1738d864a6573965f9bf5cf626b9b9a48fdb60ec 100644 (file)
@@ -463,8 +463,12 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
                }
 
        case *ast.IndexExpr:
-               exprs := typeparams.UnpackExpr(e.Index)
-               return check.instantiatedType(e.X, exprs, def)
+               if typeparams.Enabled {
+                       exprs := typeparams.UnpackExpr(e.Index)
+                       return check.instantiatedType(e.X, exprs, def)
+               }
+               check.errorf(e0, _NotAType, "%s is not a type", e0)
+               check.use(e.X)
 
        case *ast.ParenExpr:
                // Generic types must be instantiated before they can be used in any form.