Source file src/go/types/signature.go

     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 types
     6  
     7  import (
     8  	"fmt"
     9  	"go/ast"
    10  	"go/token"
    11  	. "internal/types/errors"
    12  	"path/filepath"
    13  	"strings"
    14  )
    15  
    16  // ----------------------------------------------------------------------------
    17  // API
    18  
    19  // A Signature represents a (non-builtin) function or method type.
    20  // The receiver is ignored when comparing signatures for identity.
    21  type Signature struct {
    22  	// We need to keep the scope in Signature (rather than passing it around
    23  	// and store it in the Func Object) because when type-checking a function
    24  	// literal we call the general type checker which returns a general Type.
    25  	// We then unpack the *Signature and use the scope for the literal body.
    26  	rparams  *TypeParamList // receiver type parameters from left to right, or nil
    27  	tparams  *TypeParamList // type parameters from left to right, or nil
    28  	scope    *Scope         // function scope for package-local and non-instantiated signatures; nil otherwise
    29  	recv     *Var           // nil if not a method
    30  	recvold  *Var           // receiver dropped via method selection; or nil
    31  	params   *Tuple         // (incoming) parameters from left to right; or nil
    32  	results  *Tuple         // (outgoing) results from left to right; or nil
    33  	variadic bool           // true if the last parameter's type is of the form ...T
    34  
    35  	// If recvold is the sentinel value [methExpr], then recvold should
    36  	// instead be sourced from params[0]. Otherwise, recvold points to
    37  	// the receiver of the original method signature from which this
    38  	// function signature was cloned via a selector expression.
    39  
    40  	// If variadic, the last element of params ordinarily has an
    41  	// unnamed Slice type. As a special case, in a call to append,
    42  	// it may be string, or a TypeParam T whose typeset ⊇ {string, []byte}.
    43  	// It may even be a named []byte type if a client instantiates
    44  	// T at such a type.
    45  }
    46  
    47  // sentinel value for detecting method expressions
    48  var methodExprSentinel = &Var{}
    49  
    50  // NewSignature returns a new function type for the given receiver, parameters,
    51  // and results, either of which may be nil. If variadic is set, the function
    52  // is variadic, it must have at least one parameter, and the last parameter
    53  // must be of unnamed slice type.
    54  //
    55  // Deprecated: Use [NewSignatureType] instead which allows for type parameters.
    56  //
    57  //go:fix inline
    58  func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
    59  	return NewSignatureType(recv, nil, nil, params, results, variadic)
    60  }
    61  
    62  // NewSignatureType creates a new function type for the given receiver,
    63  // receiver type parameters, type parameters, parameters, and results.
    64  //
    65  // If variadic is set, params must hold at least one parameter and the
    66  // last parameter must be an unnamed slice or a type parameter whose
    67  // type set has an unnamed slice as common underlying type.
    68  //
    69  // As a special case, to support append([]byte, str...), for variadic
    70  // signatures the last parameter may also be a string type, or a type
    71  // parameter containing a mix of byte slices and string types in its
    72  // type set. It may even be a named []byte slice type resulting from
    73  // instantiation of such a type parameter.
    74  //
    75  // If recvTypeParams is non-empty, recv must be non-nil.
    76  func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params, results *Tuple, variadic bool) *Signature {
    77  	if variadic {
    78  		n := params.Len()
    79  		if n == 0 {
    80  			panic("variadic function must have at least one parameter")
    81  		}
    82  		last := params.At(n - 1).typ
    83  		var S *Slice
    84  		for t := range typeset(last) {
    85  			if t == nil {
    86  				break
    87  			}
    88  			var s *Slice
    89  			if isString(t) {
    90  				s = NewSlice(universeByte)
    91  			} else {
    92  				// Variadic Go functions have a last parameter of type []T,
    93  				// suggesting we should reject a named slice type B here.
    94  				//
    95  				// However, a call to built-in append(slice, x...)
    96  				// where x has a TypeParam type [T ~string | ~[]byte],
    97  				// has the type func([]byte, T). Since a client may
    98  				// instantiate this type at T=B, we must permit
    99  				// named slice types, even when this results in a
   100  				// signature func([]byte, B) where type B []byte.
   101  				//
   102  				// (The caller of NewSignatureType may have no way to
   103  				// know that it is dealing with the append special case.)
   104  				s, _ = t.Underlying().(*Slice)
   105  			}
   106  			if S == nil {
   107  				S = s
   108  			} else if s == nil || !Identical(S, s) {
   109  				S = nil
   110  				break
   111  			}
   112  		}
   113  		if S == nil {
   114  			panic(fmt.Sprintf("got %s, want variadic parameter of slice or string type", last))
   115  		}
   116  	}
   117  	sig := &Signature{recv: recv, params: params, results: results, variadic: variadic}
   118  	if len(recvTypeParams) != 0 {
   119  		if recv == nil {
   120  			panic("function with receiver type parameters must have a receiver")
   121  		}
   122  		sig.rparams = bindTParams(recvTypeParams)
   123  	}
   124  	if len(typeParams) != 0 {
   125  		sig.tparams = bindTParams(typeParams)
   126  	}
   127  	return sig
   128  }
   129  
   130  // Recv returns the receiver of signature s (if a method), or nil if a
   131  // function. It is ignored when comparing signatures for identity.
   132  //
   133  // For an abstract method, Recv returns the enclosing interface either
   134  // as a *[Named] or an *[Interface]. Due to embedding, an interface may
   135  // contain methods whose receiver type is a different interface.
   136  func (s *Signature) Recv() *Var { return s.recv }
   137  
   138  // TypeParams returns the type parameters of signature s, or nil.
   139  func (s *Signature) TypeParams() *TypeParamList { return s.tparams }
   140  
   141  // RecvTypeParams returns the receiver type parameters of signature s, or nil.
   142  func (s *Signature) RecvTypeParams() *TypeParamList { return s.rparams }
   143  
   144  // Params returns the parameters of signature s, or nil.
   145  // See [NewSignatureType] for details of variadic functions.
   146  func (s *Signature) Params() *Tuple { return s.params }
   147  
   148  // Results returns the results of signature s, or nil.
   149  func (s *Signature) Results() *Tuple { return s.results }
   150  
   151  // Variadic reports whether the signature s is variadic.
   152  func (s *Signature) Variadic() bool { return s.variadic }
   153  
   154  func (s *Signature) Underlying() Type { return s }
   155  func (s *Signature) String() string   { return TypeString(s, nil) }
   156  
   157  // argType returns the expected type of the i'th argument in a call to s, or nil.
   158  func (s *Signature) argType(i int) Type {
   159  	assert(i >= 0)
   160  	if s.params == nil {
   161  		return nil
   162  	}
   163  	vars := s.params.vars
   164  	n := len(vars)
   165  	if i < n-1 || !s.variadic && i == n-1 {
   166  		return vars[i].typ
   167  	}
   168  	if s.variadic {
   169  		return vars[n-1].typ.(*Slice).elem
   170  	}
   171  	return nil
   172  }
   173  
   174  // ----------------------------------------------------------------------------
   175  // Implementation
   176  
   177  // funcType type-checks a function or method type.
   178  func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast.FuncType) {
   179  	check.openScope(ftyp, "function")
   180  	check.scope.isFunc = true
   181  	check.recordScope(ftyp, check.scope)
   182  	sig.scope = check.scope
   183  	defer check.closeScope()
   184  
   185  	// collect method receiver, if any
   186  	var recv *Var
   187  	var rparams *TypeParamList
   188  	if recvPar != nil && recvPar.NumFields() > 0 {
   189  		// We have at least one receiver; make sure we don't have more than one.
   190  		if n := len(recvPar.List); n > 1 {
   191  			check.error(recvPar.List[n-1], InvalidRecv, "method has multiple receivers")
   192  			// continue with first one
   193  		}
   194  		// all type parameters' scopes start after the method name
   195  		scopePos := ftyp.Pos()
   196  		recv, rparams = check.collectRecv(recvPar.List[0], scopePos)
   197  	}
   198  
   199  	// collect and declare function type parameters
   200  	if ftyp.TypeParams != nil {
   201  		check.collectTypeParams(&sig.tparams, ftyp.TypeParams)
   202  	}
   203  
   204  	// collect ordinary and result parameters
   205  	pnames, params, variadic := check.collectParams(ParamVar, ftyp.Params)
   206  	rnames, results, _ := check.collectParams(ResultVar, ftyp.Results)
   207  
   208  	// declare named receiver, ordinary, and result parameters
   209  	scopePos := ftyp.End() // all parameter's scopes start after the signature
   210  	if recv != nil && recv.name != "" {
   211  		check.declare(check.scope, recvPar.List[0].Names[0], recv, scopePos)
   212  	}
   213  	check.declareParams(pnames, params, scopePos)
   214  	check.declareParams(rnames, results, scopePos)
   215  
   216  	sig.recv = recv
   217  	sig.rparams = rparams
   218  	sig.params = NewTuple(params...)
   219  	sig.results = NewTuple(results...)
   220  	sig.variadic = variadic
   221  }
   222  
   223  // collectRecv extracts the method receiver and its type parameters (if any) from rparam.
   224  // It declares the type parameters (but not the receiver) in the current scope, and
   225  // returns the receiver variable and its type parameter list (if any).
   226  func (check *Checker) collectRecv(rparam *ast.Field, scopePos token.Pos) (*Var, *TypeParamList) {
   227  	// Unpack the receiver parameter which is of the form
   228  	//
   229  	//	"(" [rfield] ["*"] rbase ["[" rtparams "]"] ")"
   230  	//
   231  	// The receiver name rname, the pointer indirection, and the
   232  	// receiver type parameters rtparams may not be present.
   233  	rptr, rbase, rtparams := check.unpackRecv(rparam.Type, true)
   234  
   235  	// Determine the receiver base type.
   236  	var recvType Type = Typ[Invalid]
   237  	var recvTParamsList *TypeParamList
   238  	if rtparams == nil {
   239  		// If there are no type parameters, we can simply typecheck rparam.Type.
   240  		// If that is a generic type, varType will complain.
   241  		// Further receiver constraints will be checked later, with validRecv.
   242  		// We use rparam.Type (rather than base) to correctly record pointer
   243  		// and parentheses in types.Info (was bug, see go.dev/issue/68639).
   244  		recvType = check.varType(rparam.Type)
   245  		// Defining new methods on instantiated (alias or defined) types is not permitted.
   246  		// Follow literal pointer/alias type chain and check.
   247  		// (Correct code permits at most one pointer indirection, but for this check it
   248  		// doesn't matter if we have multiple pointers.)
   249  		a, _ := unpointer(recvType).(*Alias) // recvType is not generic per above
   250  		for a != nil {
   251  			baseType := unpointer(a.fromRHS)
   252  			if g, _ := baseType.(genericType); g != nil && g.TypeParams() != nil {
   253  				check.errorf(rbase, InvalidRecv, "cannot define new methods on instantiated type %s", g)
   254  				recvType = Typ[Invalid] // avoid follow-on errors by Checker.validRecv
   255  				break
   256  			}
   257  			a, _ = baseType.(*Alias)
   258  		}
   259  	} else {
   260  		// If there are type parameters, rbase must denote a generic base type.
   261  		// Important: rbase must be resolved before declaring any receiver type
   262  		// parameters (which may have the same name, see below).
   263  		var baseType *Named // nil if not valid
   264  		var cause string
   265  		if t := check.genericType(rbase, &cause); isValid(t) {
   266  			switch t := t.(type) {
   267  			case *Named:
   268  				baseType = t
   269  			case *Alias:
   270  				// Methods on generic aliases are not permitted.
   271  				// Only report an error if the alias type is valid.
   272  				if isValid(t) {
   273  					check.errorf(rbase, InvalidRecv, "cannot define new methods on generic alias type %s", t)
   274  				}
   275  				// Ok to continue but do not set basetype in this case so that
   276  				// recvType remains invalid (was bug, see go.dev/issue/70417).
   277  			default:
   278  				panic("unreachable")
   279  			}
   280  		} else {
   281  			if cause != "" {
   282  				check.errorf(rbase, InvalidRecv, "%s", cause)
   283  			}
   284  			// Ok to continue but do not set baseType (see comment above).
   285  		}
   286  
   287  		// Collect the type parameters declared by the receiver (see also
   288  		// Checker.collectTypeParams). The scope of the type parameter T in
   289  		// "func (r T[T]) f() {}" starts after f, not at r, so we declare it
   290  		// after typechecking rbase (see go.dev/issue/52038).
   291  		recvTParams := make([]*TypeParam, len(rtparams))
   292  		for i, rparam := range rtparams {
   293  			tpar := check.declareTypeParam(rparam, scopePos)
   294  			recvTParams[i] = tpar
   295  			// For historic reasons, type parameters in receiver type expressions
   296  			// are considered both definitions and uses and thus must be recorded
   297  			// in the Info.Uses and Info.Types maps (see go.dev/issue/68670).
   298  			check.recordUse(rparam, tpar.obj)
   299  			check.recordTypeAndValue(rparam, typexpr, tpar, nil)
   300  		}
   301  		recvTParamsList = bindTParams(recvTParams)
   302  
   303  		// Get the type parameter bounds from the receiver base type
   304  		// and set them for the respective (local) receiver type parameters.
   305  		if baseType != nil {
   306  			baseTParams := baseType.TypeParams().list()
   307  			if len(recvTParams) == len(baseTParams) {
   308  				smap := makeRenameMap(baseTParams, recvTParams)
   309  				for i, recvTPar := range recvTParams {
   310  					baseTPar := baseTParams[i]
   311  					check.mono.recordCanon(recvTPar, baseTPar)
   312  					// baseTPar.bound is possibly parameterized by other type parameters
   313  					// defined by the generic base type. Substitute those parameters with
   314  					// the receiver type parameters declared by the current method.
   315  					recvTPar.bound = check.subst(recvTPar.obj.pos, baseTPar.bound, smap, nil, check.context())
   316  				}
   317  			} else {
   318  				got := measure(len(recvTParams), "type parameter")
   319  				check.errorf(rbase, BadRecv, "receiver declares %s, but receiver base type declares %d", got, len(baseTParams))
   320  			}
   321  
   322  			// The type parameters declared by the receiver also serve as
   323  			// type arguments for the receiver type. Instantiate the receiver.
   324  			check.verifyVersionf(rbase, go1_18, "type instantiation")
   325  			targs := make([]Type, len(recvTParams))
   326  			for i, targ := range recvTParams {
   327  				targs[i] = targ
   328  			}
   329  			recvType = check.instance(rparam.Type.Pos(), baseType, targs, nil, check.context())
   330  			check.recordInstance(rbase, targs, recvType)
   331  
   332  			// Reestablish pointerness if needed (but avoid a pointer to an invalid type).
   333  			if rptr && isValid(recvType) {
   334  				recvType = NewPointer(recvType)
   335  			}
   336  
   337  			check.recordParenthesizedRecvTypes(rparam.Type, recvType)
   338  		}
   339  	}
   340  
   341  	// Make sure we have no more than one receiver name.
   342  	var rname *ast.Ident
   343  	if n := len(rparam.Names); n >= 1 {
   344  		if n > 1 {
   345  			check.error(rparam.Names[n-1], InvalidRecv, "method has multiple receivers")
   346  		}
   347  		rname = rparam.Names[0]
   348  	}
   349  
   350  	// Create the receiver parameter.
   351  	// recvType is invalid if baseType was never set.
   352  	var recv *Var
   353  	if rname != nil && rname.Name != "" {
   354  		// named receiver
   355  		recv = newVar(RecvVar, rname.Pos(), check.pkg, rname.Name, recvType)
   356  		// In this case, the receiver is declared by the caller
   357  		// because it must be declared after any type parameters
   358  		// (otherwise it might shadow one of them).
   359  	} else {
   360  		// anonymous receiver
   361  		recv = newVar(RecvVar, rparam.Pos(), check.pkg, "", recvType)
   362  		check.recordImplicit(rparam, recv)
   363  	}
   364  
   365  	// Delay validation of receiver type as it may cause premature expansion of types
   366  	// the receiver type is dependent on (see go.dev/issue/51232, go.dev/issue/51233).
   367  	check.later(func() {
   368  		check.validRecv(rbase, recv)
   369  	}).describef(recv, "validRecv(%s)", recv)
   370  
   371  	return recv, recvTParamsList
   372  }
   373  
   374  func unpointer(t Type) Type {
   375  	for {
   376  		p, _ := t.(*Pointer)
   377  		if p == nil {
   378  			return t
   379  		}
   380  		t = p.base
   381  	}
   382  }
   383  
   384  // recordParenthesizedRecvTypes records parenthesized intermediate receiver type
   385  // expressions that all map to the same type, by recursively unpacking expr and
   386  // recording the corresponding type for it. Example:
   387  //
   388  //	expression  -->  type
   389  //	----------------------
   390  //	(*(T[P]))        *T[P]
   391  //	 *(T[P])         *T[P]
   392  //	  (T[P])          T[P]
   393  //	   T[P]           T[P]
   394  func (check *Checker) recordParenthesizedRecvTypes(expr ast.Expr, typ Type) {
   395  	for {
   396  		check.recordTypeAndValue(expr, typexpr, typ, nil)
   397  		switch e := expr.(type) {
   398  		case *ast.ParenExpr:
   399  			expr = e.X
   400  		case *ast.StarExpr:
   401  			expr = e.X
   402  			// In a correct program, typ must be an unnamed
   403  			// pointer type. But be careful and don't panic.
   404  			ptr, _ := typ.(*Pointer)
   405  			if ptr == nil {
   406  				return // something is wrong
   407  			}
   408  			typ = ptr.base
   409  		default:
   410  			return // cannot unpack any further
   411  		}
   412  	}
   413  }
   414  
   415  // collectParams collects (but does not declare) all parameter/result
   416  // variables of list and returns the list of names and corresponding
   417  // variables, and whether the (parameter) list is variadic.
   418  // Anonymous parameters are recorded with nil names.
   419  func (check *Checker) collectParams(kind VarKind, list *ast.FieldList) (names []*ast.Ident, params []*Var, variadic bool) {
   420  	if list == nil {
   421  		return
   422  	}
   423  
   424  	var named, anonymous bool
   425  	for i, field := range list.List {
   426  		ftype := field.Type
   427  		if t, _ := ftype.(*ast.Ellipsis); t != nil {
   428  			ftype = t.Elt
   429  			if kind == ParamVar && i == len(list.List)-1 && len(field.Names) <= 1 {
   430  				variadic = true
   431  			} else {
   432  				check.softErrorf(t, InvalidSyntaxTree, "invalid use of ...")
   433  				// ignore ... and continue
   434  			}
   435  		}
   436  		typ := check.varType(ftype)
   437  		// The parser ensures that f.Tag is nil and we don't
   438  		// care if a constructed AST contains a non-nil tag.
   439  		if len(field.Names) > 0 {
   440  			// named parameter
   441  			for _, name := range field.Names {
   442  				if name.Name == "" {
   443  					check.error(name, InvalidSyntaxTree, "anonymous parameter")
   444  					// ok to continue
   445  				}
   446  				par := newVar(kind, name.Pos(), check.pkg, name.Name, typ)
   447  				// named parameter is declared by caller
   448  				names = append(names, name)
   449  				params = append(params, par)
   450  			}
   451  			named = true
   452  		} else {
   453  			// anonymous parameter
   454  			par := newVar(kind, ftype.Pos(), check.pkg, "", typ)
   455  			check.recordImplicit(field, par)
   456  			names = append(names, nil)
   457  			params = append(params, par)
   458  			anonymous = true
   459  		}
   460  	}
   461  
   462  	if named && anonymous {
   463  		check.error(list, InvalidSyntaxTree, "list contains both named and anonymous parameters")
   464  		// ok to continue
   465  	}
   466  
   467  	// For a variadic function, change the last parameter's type from T to []T.
   468  	// Since we type-checked T rather than ...T, we also need to retro-actively
   469  	// record the type for ...T.
   470  	if variadic {
   471  		last := params[len(params)-1]
   472  		last.typ = &Slice{elem: last.typ}
   473  		check.recordTypeAndValue(list.List[len(list.List)-1].Type, typexpr, last.typ, nil)
   474  	}
   475  
   476  	return
   477  }
   478  
   479  // declareParams declares each named parameter in the current scope.
   480  func (check *Checker) declareParams(names []*ast.Ident, params []*Var, scopePos token.Pos) {
   481  	for i, name := range names {
   482  		if name != nil && name.Name != "" {
   483  			check.declare(check.scope, name, params[i], scopePos)
   484  		}
   485  	}
   486  }
   487  
   488  // validRecv verifies that the receiver satisfies its respective spec requirements
   489  // and reports an error otherwise.
   490  func (check *Checker) validRecv(pos positioner, recv *Var) {
   491  	// spec: "The receiver type must be of the form T or *T where T is a type name."
   492  	rtyp, _ := deref(recv.typ)
   493  	atyp := Unalias(rtyp)
   494  	if !isValid(atyp) {
   495  		return // error was reported before
   496  	}
   497  	// spec: "The type denoted by T is called the receiver base type; it must not
   498  	// be a pointer or interface type and it must be declared in the same package
   499  	// as the method."
   500  	switch T := atyp.(type) {
   501  	case *Named:
   502  		if T.obj.pkg != check.pkg || isCGoTypeObj(check.fset, T.obj) {
   503  			check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
   504  			break
   505  		}
   506  		var cause string
   507  		switch u := T.Underlying().(type) {
   508  		case *Basic:
   509  			// unsafe.Pointer is treated like a regular pointer
   510  			if u.kind == UnsafePointer {
   511  				cause = "unsafe.Pointer"
   512  			}
   513  		case *Pointer, *Interface:
   514  			cause = "pointer or interface type"
   515  		case *TypeParam:
   516  			// The underlying type of a receiver base type cannot be a
   517  			// type parameter: "type T[P any] P" is not a valid declaration.
   518  			panic("unreachable")
   519  		}
   520  		if cause != "" {
   521  			check.errorf(pos, InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
   522  		}
   523  	case *Basic:
   524  		check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
   525  	default:
   526  		check.errorf(pos, InvalidRecv, "invalid receiver type %s", recv.typ)
   527  	}
   528  }
   529  
   530  // isCGoTypeObj reports whether the given type name was created by cgo.
   531  func isCGoTypeObj(fset *token.FileSet, obj *TypeName) bool {
   532  	return strings.HasPrefix(obj.name, "_Ctype_") ||
   533  		strings.HasPrefix(filepath.Base(fset.File(obj.pos).Name()), "_cgo_")
   534  }
   535  

View as plain text