package types2
-import "cmd/compile/internal/syntax"
+import (
+ "cmd/compile/internal/syntax"
+ "fmt"
+ . "internal/types/errors"
+)
// ----------------------------------------------------------------------------
// API
// NewSignatureType creates a new function type for the given receiver,
// receiver type parameters, type parameters, parameters, and results. If
// variadic is set, params must hold at least one parameter and the last
-// parameter must be of unnamed slice type. If recv is non-nil, typeParams must
-// be empty. If recvTypeParams is non-empty, recv must be non-nil.
+// parameter's core type must be of unnamed slice or bytestring type.
+// If recv is non-nil, typeParams must be empty. If recvTypeParams is
+// non-empty, recv must be non-nil.
func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params, results *Tuple, variadic bool) *Signature {
if variadic {
n := params.Len()
if n == 0 {
panic("variadic function must have at least one parameter")
}
- if _, ok := params.At(n - 1).typ.(*Slice); !ok {
- panic("variadic parameter must be of unnamed slice type")
+ core := coreString(params.At(n - 1).typ)
+ if _, ok := core.(*Slice); !ok && !isString(core) {
+ panic(fmt.Sprintf("got %s, want variadic parameter with unnamed slice type or string as core type", core.String()))
}
}
sig := &Signature{recv: recv, params: params, results: results, variadic: variadic}
// 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, nil).(*Named); recv != nil {
+ if recv := asNamed(check.genericType(rname, nil)); recv != nil {
recvTParams = recv.TypeParams().list()
}
}
} else if len(tparams) < len(recvTParams) {
// Reporting an error here is a stop-gap measure to avoid crashes in the
// compiler when a type parameter/argument cannot be inferred later. It
- // may lead to follow-on errors (see issues #51339, #51343).
+ // may lead to follow-on errors (see issues go.dev/issue/51339, go.dev/issue/51343).
// TODO(gri) find a better solution
got := measure(len(tparams), "type parameter")
- check.errorf(recvPar, _BadRecv, "got %s, but receiver base type declares %d", got, len(recvTParams))
+ check.errorf(recvPar, BadRecv, "got %s, but receiver base type declares %d", got, len(recvTParams))
}
}
}
results, _ := check.collectParams(scope, ftyp.ResultList, false)
scope.Squash(func(obj, alt Object) {
var err error_
+ err.code = DuplicateDecl
err.errorf(obj, "%s redeclared in this block", obj.Name())
err.recordAltDecl(alt)
check.report(&err)
recv = NewParam(nopos, nil, "", Typ[Invalid]) // ignore recv below
default:
// more than one receiver
- check.error(recvList[len(recvList)-1].Pos(), _InvalidRecv, "method must have exactly one receiver")
+ check.error(recvList[len(recvList)-1].Pos(), InvalidRecv, "method must have exactly one receiver")
fallthrough // continue with first receiver
case 1:
recv = recvList[0]
sig.recv = recv
// Delay validation of receiver type as it may cause premature expansion
- // of types the receiver type is dependent on (see issues #51232, #51233).
+ // of types the receiver type is dependent on (see issues go.dev/issue/51232, go.dev/issue/51233).
check.later(func() {
// spec: "The receiver type must be of the form T or *T where T is a type name."
rtyp, _ := deref(recv.typ)
- if rtyp == Typ[Invalid] {
+ atyp := _Unalias(rtyp)
+ if !isValid(atyp) {
return // error was reported before
}
// spec: "The type denoted by T is called the receiver base type; it must not
// be a pointer or interface type and it must be declared in the same package
// as the method."
- switch T := rtyp.(type) {
+ switch T := atyp.(type) {
case *Named:
// The receiver type may be an instantiated type referred to
// by an alias (which cannot have receiver parameters for now).
if T.TypeArgs() != nil && sig.RecvTypeParams() == nil {
- check.errorf(recv, _InvalidRecv, "cannot define new methods on instantiated type %s", rtyp)
+ check.errorf(recv, InvalidRecv, "cannot define new methods on instantiated type %s", rtyp)
break
}
if T.obj.pkg != check.pkg {
- check.errorf(recv, _InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
+ check.errorf(recv, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
break
}
var cause string
unreachable()
}
if cause != "" {
- check.errorf(recv, _InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
+ check.errorf(recv, InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
}
case *Basic:
- check.errorf(recv, _InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
+ check.errorf(recv, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
default:
- check.errorf(recv, _InvalidRecv, "invalid receiver type %s", recv.typ)
+ check.errorf(recv, InvalidRecv, "invalid receiver type %s", recv.typ)
}
}).describef(recv, "validate receiver %s", recv)
}
if variadicOk && i == len(list)-1 {
variadic = true
} else {
- check.softErrorf(t, _MisplacedDotDotDot, "can only use ... with final parameter in list")
+ check.softErrorf(t, MisplacedDotDotDot, "can only use ... with final parameter in list")
// ignore ... and continue
}
}
// named parameter
name := field.Name.Value
if name == "" {
- check.error(field.Name, 0, invalidAST+"anonymous parameter")
+ check.error(field.Name, InvalidSyntaxTree, "anonymous parameter")
// ok to continue
}
par := NewParam(field.Name.Pos(), check.pkg, name, typ)
}
if named && anonymous {
- check.error(list[0], 0, invalidAST+"list contains both named and anonymous parameters")
+ check.error(list[0], InvalidSyntaxTree, "list contains both named and anonymous parameters")
// ok to continue
}