diff options
author | Robert Griesemer <gri@golang.org> | 2022-06-07 13:39:56 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2022-06-07 21:37:21 +0000 |
commit | 3507805bcdcd6674c842e25fdb5f07f5ce47ba87 (patch) | |
tree | 97954aa488fa885a7ba1c5529bd80c5b3c500566 | |
parent | 269bf7e855da04e664fe8d7ffb654c4d0b1439f5 (diff) | |
download | go-3507805bcdcd6674c842e25fdb5f07f5ce47ba87.tar.gz go-3507805bcdcd6674c842e25fdb5f07f5ce47ba87.zip |
go/types, types2: better error message for invalid use of constraint type
Fixes #42881.
Change-Id: If800c5f90c0034d192bf8b6649e5cfda96df48cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/410954
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
-rw-r--r-- | src/cmd/compile/internal/types2/testdata/fixedbugs/issue42881.go | 16 | ||||
-rw-r--r-- | src/cmd/compile/internal/types2/typexpr.go | 4 | ||||
-rw-r--r-- | src/go/types/testdata/fixedbugs/issue42881.go | 16 | ||||
-rw-r--r-- | src/go/types/typexpr.go | 4 |
4 files changed, 36 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42881.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42881.go new file mode 100644 index 0000000000..7122d1c787 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42881.go @@ -0,0 +1,16 @@ +// Copyright 2022 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 p + +type ( + T1 interface{ comparable } + T2 interface{ int } +) + +var ( + _ comparable // ERROR cannot use type comparable outside a type constraint: interface is \(or embeds\) comparable + _ T1 // ERROR cannot use type T1 outside a type constraint: interface is \(or embeds\) comparable + _ T2 // ERROR cannot use type T2 outside a type constraint: interface contains type constraints +) diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index f0cd236050..692feb9751 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -167,9 +167,9 @@ func (check *Checker) validVarType(e syntax.Expr, typ Type) { tset := computeInterfaceTypeSet(check, pos, t) // TODO(gri) is this the correct position? if !tset.IsMethodSet() { if tset.comparable { - check.softErrorf(pos, "interface is (or embeds) comparable") + check.softErrorf(pos, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", typ) } else { - check.softErrorf(pos, "interface contains type constraints") + check.softErrorf(pos, "cannot use type %s outside a type constraint: interface contains type constraints", typ) } } } diff --git a/src/go/types/testdata/fixedbugs/issue42881.go b/src/go/types/testdata/fixedbugs/issue42881.go new file mode 100644 index 0000000000..7122d1c787 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue42881.go @@ -0,0 +1,16 @@ +// Copyright 2022 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 p + +type ( + T1 interface{ comparable } + T2 interface{ int } +) + +var ( + _ comparable // ERROR cannot use type comparable outside a type constraint: interface is \(or embeds\) comparable + _ T1 // ERROR cannot use type T1 outside a type constraint: interface is \(or embeds\) comparable + _ T2 // ERROR cannot use type T2 outside a type constraint: interface contains type constraints +) diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index a881d33654..b02929df22 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -163,9 +163,9 @@ func (check *Checker) validVarType(e ast.Expr, typ Type) { tset := computeInterfaceTypeSet(check, e.Pos(), t) // TODO(gri) is this the correct position? if !tset.IsMethodSet() { if tset.comparable { - check.softErrorf(e, _MisplacedConstraintIface, "interface is (or embeds) comparable") + check.softErrorf(e, _MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", typ) } else { - check.softErrorf(e, _MisplacedConstraintIface, "interface contains type constraints") + check.softErrorf(e, _MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface contains type constraints", typ) } } } |