// If the operand type is a type parameter, all types in its type set
// must have a common underlying type, which must be a signature.
- u, err := commonUnder(x.typ, func(t, u Type) *errorCause {
+ u, err := commonUnder(x.typ, func(t, u Type) *typeError {
if _, ok := u.(*Signature); u != nil && !ok {
- return newErrorCause("%s is not a function", t)
+ return typeErrorf("%s is not a function", t)
}
return nil
})
// or send to x (recv == false) operation. If the operation is not valid, chanElem
// reports an error and returns nil.
func (check *Checker) chanElem(pos poser, x *operand, recv bool) Type {
- u, err := commonUnder(x.typ, func(t, u Type) *errorCause {
+ u, err := commonUnder(x.typ, func(t, u Type) *typeError {
if u == nil {
- return newErrorCause("no specific channel type")
+ return typeErrorf("no specific channel type")
}
ch, _ := u.(*Chan)
if ch == nil {
- return newErrorCause("non-channel %s", t)
+ return typeErrorf("non-channel %s", t)
}
if recv && ch.dir == SendOnly {
- return newErrorCause("send-only channel %s", t)
+ return typeErrorf("send-only channel %s", t)
}
if !recv && ch.dir == RecvOnly {
- return newErrorCause("receive-only channel %s", t)
+ return typeErrorf("receive-only channel %s", t)
}
return nil
})
}
// If T is comparable, comparableType returns nil.
-// Otherwise it returns an error cause explaining why T is not comparable.
+// Otherwise it returns a type error explaining why T is not comparable.
// If dynamic is set, non-type parameter interfaces are always comparable.
-func comparableType(T Type, dynamic bool, seen map[Type]bool) *errorCause {
+func comparableType(T Type, dynamic bool, seen map[Type]bool) *typeError {
if seen[T] {
return nil
}
case *Basic:
// assume invalid types to be comparable to avoid follow-up errors
if t.kind == UntypedNil {
- return newErrorCause("")
+ return typeErrorf("")
}
case *Pointer, *Chan:
case *Struct:
for _, f := range t.fields {
if comparableType(f.typ, dynamic, seen) != nil {
- return newErrorCause("struct containing %s cannot be compared", f.typ)
+ return typeErrorf("struct containing %s cannot be compared", f.typ)
}
}
case *Array:
if comparableType(t.elem, dynamic, seen) != nil {
- return newErrorCause("%s cannot be compared", T)
+ return typeErrorf("%s cannot be compared", T)
}
case *Interface:
} else {
cause = "incomparable types in type set"
}
- return newErrorCause(cause)
+ return typeErrorf(cause)
default:
- return newErrorCause("")
+ return typeErrorf("")
}
return nil
return Typ[Invalid], Typ[Invalid], cause, false
}
- rtyp, err := commonUnder(orig, func(t, u Type) *errorCause {
+ rtyp, err := commonUnder(orig, func(t, u Type) *typeError {
// A channel must permit receive operations.
if ch, _ := u.(*Chan); ch != nil && ch.dir == SendOnly {
- return newErrorCause("receive from send-only channel %s", t)
+ return typeErrorf("receive from send-only channel %s", t)
}
return nil
})
yield(t, under(t))
}
-// A errorCause describes an error cause.
-type errorCause struct {
+// A typeError describes a type error.
+type typeError struct {
format_ string
args []any
}
-var emptyErrorCause errorCause
+var emptyTypeError typeError
-func newErrorCause(format string, args ...any) *errorCause {
+func typeErrorf(format string, args ...any) *typeError {
if format == "" {
- return &emptyErrorCause
+ return &emptyTypeError
}
- return &errorCause{format, args}
+ return &typeError{format, args}
}
-// format formats a cause as a string.
+// format formats a type error as a string.
// check may be nil.
-func (err *errorCause) format(check *Checker) string {
+func (err *typeError) format(check *Checker) string {
return check.sprintf(err.format_, err.args...)
}
// If t is a type parameter, cond is nil, and t's type set contains no channel types,
// commonUnder returns the common underlying type of all types in t's type set if
-// it exists, or nil and an error cause otherwise.
+// it exists, or nil and a type error otherwise.
//
// If t is a type parameter, cond is nil, and there are channel types, t's type set
// must only contain channel types, they must all have the same element types,
// channel directions must not conflict, and commonUnder returns one of the most
-// restricted channels. Otherwise, the function returns nil and an error cause.
+// restricted channels. Otherwise, the function returns nil and a type error.
//
// If cond != nil, each pair (t, u) of type and underlying type in t's type set
// must satisfy the condition expressed by cond. If the result of cond is != nil,
-// commonUnder returns nil and the error cause reported by cond.
+// commonUnder returns nil and the type error reported by cond.
// Note that cond is called before any other conditions are checked; specifically
// cond may be called with (nil, nil) if the type set contains no specific types.
//
// If t is not a type parameter, commonUnder behaves as if t was a type parameter
// with the single type t in its type set.
-func commonUnder(t Type, cond func(t, u Type) *errorCause) (Type, *errorCause) {
+func commonUnder(t Type, cond func(t, u Type) *typeError) (Type, *typeError) {
var ct, cu Type // type and respective common underlying type
- var err *errorCause
+ var err *typeError
bad := func(format string, args ...any) bool {
cu = nil
- err = newErrorCause(format, args...)
+ err = typeErrorf(format, args...)
return false
}
// If the operand type is a type parameter, all types in its type set
// must have a common underlying type, which must be a signature.
- u, err := commonUnder(x.typ, func(t, u Type) *errorCause {
+ u, err := commonUnder(x.typ, func(t, u Type) *typeError {
if _, ok := u.(*Signature); u != nil && !ok {
- return newErrorCause("%s is not a function", t)
+ return typeErrorf("%s is not a function", t)
}
return nil
})
// or send to x (recv == false) operation. If the operation is not valid, chanElem
// reports an error and returns nil.
func (check *Checker) chanElem(pos positioner, x *operand, recv bool) Type {
- u, err := commonUnder(x.typ, func(t, u Type) *errorCause {
+ u, err := commonUnder(x.typ, func(t, u Type) *typeError {
if u == nil {
- return newErrorCause("no specific channel type")
+ return typeErrorf("no specific channel type")
}
ch, _ := u.(*Chan)
if ch == nil {
- return newErrorCause("non-channel %s", t)
+ return typeErrorf("non-channel %s", t)
}
if recv && ch.dir == SendOnly {
- return newErrorCause("send-only channel %s", t)
+ return typeErrorf("send-only channel %s", t)
}
if !recv && ch.dir == RecvOnly {
- return newErrorCause("receive-only channel %s", t)
+ return typeErrorf("receive-only channel %s", t)
}
return nil
})
}
// If T is comparable, comparableType returns nil.
-// Otherwise it returns an error cause explaining why T is not comparable.
+// Otherwise it returns a type error explaining why T is not comparable.
// If dynamic is set, non-type parameter interfaces are always comparable.
-func comparableType(T Type, dynamic bool, seen map[Type]bool) *errorCause {
+func comparableType(T Type, dynamic bool, seen map[Type]bool) *typeError {
if seen[T] {
return nil
}
case *Basic:
// assume invalid types to be comparable to avoid follow-up errors
if t.kind == UntypedNil {
- return newErrorCause("")
+ return typeErrorf("")
}
case *Pointer, *Chan:
case *Struct:
for _, f := range t.fields {
if comparableType(f.typ, dynamic, seen) != nil {
- return newErrorCause("struct containing %s cannot be compared", f.typ)
+ return typeErrorf("struct containing %s cannot be compared", f.typ)
}
}
case *Array:
if comparableType(t.elem, dynamic, seen) != nil {
- return newErrorCause("%s cannot be compared", T)
+ return typeErrorf("%s cannot be compared", T)
}
case *Interface:
} else {
cause = "incomparable types in type set"
}
- return newErrorCause(cause)
+ return typeErrorf(cause)
default:
- return newErrorCause("")
+ return typeErrorf("")
}
return nil
return Typ[Invalid], Typ[Invalid], cause, false
}
- rtyp, err := commonUnder(orig, func(t, u Type) *errorCause {
+ rtyp, err := commonUnder(orig, func(t, u Type) *typeError {
// A channel must permit receive operations.
if ch, _ := u.(*Chan); ch != nil && ch.dir == SendOnly {
- return newErrorCause("receive from send-only channel %s", t)
+ return typeErrorf("receive from send-only channel %s", t)
}
return nil
})
yield(t, under(t))
}
-// A errorCause describes an error cause.
-type errorCause struct {
+// A typeError describes a type error.
+type typeError struct {
format_ string
args []any
}
-var emptyErrorCause errorCause
+var emptyTypeError typeError
-func newErrorCause(format string, args ...any) *errorCause {
+func typeErrorf(format string, args ...any) *typeError {
if format == "" {
- return &emptyErrorCause
+ return &emptyTypeError
}
- return &errorCause{format, args}
+ return &typeError{format, args}
}
-// format formats a cause as a string.
+// format formats a type error as a string.
// check may be nil.
-func (err *errorCause) format(check *Checker) string {
+func (err *typeError) format(check *Checker) string {
return check.sprintf(err.format_, err.args...)
}
// If t is a type parameter, cond is nil, and t's type set contains no channel types,
// commonUnder returns the common underlying type of all types in t's type set if
-// it exists, or nil and an error cause otherwise.
+// it exists, or nil and a type error otherwise.
//
// If t is a type parameter, cond is nil, and there are channel types, t's type set
// must only contain channel types, they must all have the same element types,
// channel directions must not conflict, and commonUnder returns one of the most
-// restricted channels. Otherwise, the function returns nil and an error cause.
+// restricted channels. Otherwise, the function returns nil and a type error.
//
// If cond != nil, each pair (t, u) of type and underlying type in t's type set
// must satisfy the condition expressed by cond. If the result of cond is != nil,
-// commonUnder returns nil and the error cause reported by cond.
+// commonUnder returns nil and the type error reported by cond.
// Note that cond is called before any other conditions are checked; specifically
// cond may be called with (nil, nil) if the type set contains no specific types.
//
// If t is not a type parameter, commonUnder behaves as if t was a type parameter
// with the single type t in its type set.
-func commonUnder(t Type, cond func(t, u Type) *errorCause) (Type, *errorCause) {
+func commonUnder(t Type, cond func(t, u Type) *typeError) (Type, *typeError) {
var ct, cu Type // type and respective common underlying type
- var err *errorCause
+ var err *typeError
bad := func(format string, args ...any) bool {
cu = nil
- err = newErrorCause(format, args...)
+ err = typeErrorf(format, args...)
return false
}