// Also: Don't report an error via genericType since it will be reported
// again when we type-check the signature.
// TODO(gri) maybe the receiver should be marked as invalid instead?
- if recv, _ := check.genericType(rname, false).(*Named); recv != nil {
+ if recv, _ := check.genericType(rname, nil).(*Named); recv != nil {
recvTParams = recv.TypeParams().list()
}
}
--- /dev/null
+// 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 p
+
+type G[P any] int
+
+type (
+ _ G[int]
+ _ G[G /* ERROR "cannot use.*without instantiation" */]
+ _ bool /* ERROR "invalid operation: bool\[int\] \(bool is not a generic type\)" */ [int]
+ _ bool /* ERROR "invalid operation: bool\[G\] \(bool is not a generic type\)" */[G]
+)
+
+// The example from the issue.
+func _() {
+ _ = &([10]bool /* ERROR "invalid operation.*bool is not a generic type" */ [1]{})
+}
return typ
}
-// genericType is like typ but the type must be an (uninstantiated) generic type.
-func (check *Checker) genericType(e syntax.Expr, reportErr bool) Type {
+// genericType is like typ but the type must be an (uninstantiated) generic
+// type. If reason is non-nil and the type expression was a valid type but not
+// generic, reason will be populated with a message describing the error.
+func (check *Checker) genericType(e syntax.Expr, reason *string) Type {
typ := check.typInternal(e, nil)
assert(isTyped(typ))
if typ != Typ[Invalid] && !isGeneric(typ) {
- if reportErr {
- check.errorf(e, "%s is not a generic type", typ)
+ if reason != nil {
+ *reason = check.sprintf("%s is not a generic type", typ)
}
typ = Typ[Invalid]
}
}()
}
- gtyp := check.genericType(x, true)
+ var reason string
+ gtyp := check.genericType(x, &reason)
+ if reason != "" {
+ check.errorf(x, invalidOp+"%s%s (%s)", x, xlist, reason)
+ }
if gtyp == Typ[Invalid] {
return gtyp // error already reported
}