Skip to content

Commit 63daa77

Browse files
committed
go/types: guard against checking instantiation when generics is disabled
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 <[email protected]> Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 197a5ee commit 63daa77

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/go/types/check_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,15 @@ func checkFiles(t *testing.T, sizes Sizes, goVersion string, filenames []string,
207207
t.Fatal("no source files")
208208
}
209209

210+
if strings.HasSuffix(filenames[0], ".go2") && !typeparams.Enabled {
211+
t.Skip("type params are not enabled")
212+
}
213+
if strings.HasSuffix(filenames[0], ".go1") && typeparams.Enabled {
214+
t.Skip("type params are enabled")
215+
}
216+
210217
mode := parser.AllErrors
211-
if strings.HasSuffix(filenames[0], ".go2") {
212-
if !typeparams.Enabled {
213-
t.Skip("type params are not enabled")
214-
}
215-
} else {
218+
if !strings.HasSuffix(filenames[0], ".go2") {
216219
mode |= typeparams.DisallowParsing
217220
}
218221

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2021 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 issue46404
6+
7+
// Check that we don't type check t[_] as an instantiation.
8+
type t [t /* ERROR not a type */ [_]]_ // ERROR cannot use

src/go/types/typexpr.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,12 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
463463
}
464464

465465
case *ast.IndexExpr:
466-
exprs := typeparams.UnpackExpr(e.Index)
467-
return check.instantiatedType(e.X, exprs, def)
466+
if typeparams.Enabled {
467+
exprs := typeparams.UnpackExpr(e.Index)
468+
return check.instantiatedType(e.X, exprs, def)
469+
}
470+
check.errorf(e0, _NotAType, "%s is not a type", e0)
471+
check.use(e.X)
468472

469473
case *ast.ParenExpr:
470474
// Generic types must be instantiated before they can be used in any form.

0 commit comments

Comments
 (0)