Source file src/go/types/typexpr.go

     1  // Copyright 2013 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  // This file implements type-checking of identifiers and type expressions.
     6  
     7  package types
     8  
     9  import (
    10  	"fmt"
    11  	"go/ast"
    12  	"go/constant"
    13  	. "internal/types/errors"
    14  	"strings"
    15  )
    16  
    17  // ident type-checks identifier e and initializes x with the value or type of e.
    18  // If an error occurred, x.mode is set to invalid.
    19  // If wantType is set, the identifier e is expected to denote a type.
    20  func (check *Checker) ident(x *operand, e *ast.Ident, wantType bool) {
    21  	x.mode = invalid
    22  	x.expr = e
    23  
    24  	scope, obj := check.lookupScope(e.Name)
    25  	switch obj {
    26  	case nil:
    27  		if e.Name == "_" {
    28  			check.error(e, InvalidBlank, "cannot use _ as value or type")
    29  		} else if isValidName(e.Name) {
    30  			check.errorf(e, UndeclaredName, "undefined: %s", e.Name)
    31  		}
    32  		return
    33  	case universeComparable:
    34  		if !check.verifyVersionf(e, go1_18, "predeclared %s", e.Name) {
    35  			return // avoid follow-on errors
    36  		}
    37  	}
    38  	// Because the representation of any depends on gotypesalias, we don't check
    39  	// pointer identity here.
    40  	if obj.Name() == "any" && obj.Parent() == Universe {
    41  		if !check.verifyVersionf(e, go1_18, "predeclared %s", e.Name) {
    42  			return // avoid follow-on errors
    43  		}
    44  	}
    45  	check.recordUse(e, obj)
    46  
    47  	// If we want a type but don't have one, stop right here and avoid potential problems
    48  	// with missing underlying types. This also gives better error messages in some cases
    49  	// (see go.dev/issue/65344).
    50  	_, gotType := obj.(*TypeName)
    51  	if !gotType && wantType {
    52  		check.errorf(e, NotAType, "%s is not a type", obj.Name())
    53  		// avoid "declared but not used" errors
    54  		// (don't use Checker.use - we don't want to evaluate too much)
    55  		if v, _ := obj.(*Var); v != nil && v.pkg == check.pkg /* see Checker.use1 */ {
    56  			check.usedVars[v] = true
    57  		}
    58  		return
    59  	}
    60  
    61  	// Type-check the object.
    62  	// Only call Checker.objDecl if the object doesn't have a type yet
    63  	// (in which case we must actually determine it) or the object is a
    64  	// TypeName from the current package and we also want a type (in which case
    65  	// we might detect a cycle which needs to be reported). Otherwise we can skip
    66  	// the call and avoid a possible cycle error in favor of the more informative
    67  	// "not a type/value" error that this function's caller will issue (see
    68  	// go.dev/issue/25790).
    69  	//
    70  	// Note that it is important to avoid calling objDecl on objects from other
    71  	// packages, to avoid races: see issue #69912.
    72  	typ := obj.Type()
    73  	if typ == nil || (gotType && wantType && obj.Pkg() == check.pkg) {
    74  		check.objDecl(obj)
    75  		typ = obj.Type() // type must have been assigned by Checker.objDecl
    76  	}
    77  	assert(typ != nil)
    78  
    79  	// The object may have been dot-imported.
    80  	// If so, mark the respective package as used.
    81  	// (This code is only needed for dot-imports. Without them,
    82  	// we only have to mark variables, see *Var case below).
    83  	if pkgName := check.dotImportMap[dotImportKey{scope, obj.Name()}]; pkgName != nil {
    84  		check.usedPkgNames[pkgName] = true
    85  	}
    86  
    87  	switch obj := obj.(type) {
    88  	case *PkgName:
    89  		check.errorf(e, InvalidPkgUse, "use of package %s not in selector", obj.name)
    90  		return
    91  
    92  	case *Const:
    93  		check.addDeclDep(obj)
    94  		if !isValid(typ) {
    95  			return
    96  		}
    97  		if obj == universeIota {
    98  			if check.iota == nil {
    99  				check.error(e, InvalidIota, "cannot use iota outside constant declaration")
   100  				return
   101  			}
   102  			x.val = check.iota
   103  		} else {
   104  			x.val = obj.val
   105  		}
   106  		assert(x.val != nil)
   107  		x.mode = constant_
   108  
   109  	case *TypeName:
   110  		if !check.conf._EnableAlias && check.isBrokenAlias(obj) {
   111  			check.errorf(e, InvalidDeclCycle, "invalid use of type alias %s in recursive type (see go.dev/issue/50729)", obj.name)
   112  			return
   113  		}
   114  		x.mode = typexpr
   115  
   116  	case *Var:
   117  		// It's ok to mark non-local variables, but ignore variables
   118  		// from other packages to avoid potential race conditions with
   119  		// dot-imported variables.
   120  		if obj.pkg == check.pkg {
   121  			check.usedVars[obj] = true
   122  		}
   123  		check.addDeclDep(obj)
   124  		if !isValid(typ) {
   125  			return
   126  		}
   127  		x.mode = variable
   128  
   129  	case *Func:
   130  		check.addDeclDep(obj)
   131  		x.mode = value
   132  
   133  	case *Builtin:
   134  		x.id = obj.id
   135  		x.mode = builtin
   136  
   137  	case *Nil:
   138  		x.mode = value
   139  
   140  	default:
   141  		panic("unreachable")
   142  	}
   143  
   144  	x.typ = typ
   145  }
   146  
   147  // typ type-checks the type expression e and returns its type, or Typ[Invalid].
   148  // The type must not be an (uninstantiated) generic type.
   149  func (check *Checker) typ(e ast.Expr) Type {
   150  	return check.declaredType(e, nil)
   151  }
   152  
   153  // varType type-checks the type expression e and returns its type, or Typ[Invalid].
   154  // The type must not be an (uninstantiated) generic type and it must not be a
   155  // constraint interface.
   156  func (check *Checker) varType(e ast.Expr) Type {
   157  	typ := check.declaredType(e, nil)
   158  	check.validVarType(e, typ)
   159  	return typ
   160  }
   161  
   162  // validVarType reports an error if typ is a constraint interface.
   163  // The expression e is used for error reporting, if any.
   164  func (check *Checker) validVarType(e ast.Expr, typ Type) {
   165  	// If we have a type parameter there's nothing to do.
   166  	if isTypeParam(typ) {
   167  		return
   168  	}
   169  
   170  	// We don't want to call typ.Underlying() or complete interfaces while we are in
   171  	// the middle of type-checking parameter declarations that might belong
   172  	// to interface methods. Delay this check to the end of type-checking.
   173  	check.later(func() {
   174  		if t, _ := typ.Underlying().(*Interface); t != nil {
   175  			tset := computeInterfaceTypeSet(check, e.Pos(), t) // TODO(gri) is this the correct position?
   176  			if !tset.IsMethodSet() {
   177  				if tset.comparable {
   178  					check.softErrorf(e, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", typ)
   179  				} else {
   180  					check.softErrorf(e, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface contains type constraints", typ)
   181  				}
   182  			}
   183  		}
   184  	}).describef(e, "check var type %s", typ)
   185  }
   186  
   187  // declaredType is like typ but also accepts a type name def.
   188  // If def != nil, e is the type specification for the [Alias] or [Named] type
   189  // named def, and def.typ.fromRHS will be set to the [Type] of e immediately
   190  // after its creation.
   191  func (check *Checker) declaredType(e ast.Expr, def *TypeName) Type {
   192  	typ := check.typInternal(e, def)
   193  	assert(isTyped(typ))
   194  	if isGeneric(typ) {
   195  		check.errorf(e, WrongTypeArgCount, "cannot use generic type %s without instantiation", typ)
   196  		typ = Typ[Invalid]
   197  	}
   198  	check.recordTypeAndValue(e, typexpr, typ, nil)
   199  	return typ
   200  }
   201  
   202  // genericType is like typ but the type must be an (uninstantiated) generic
   203  // type. If cause is non-nil and the type expression was a valid type but not
   204  // generic, cause will be populated with a message describing the error.
   205  //
   206  // Note: If the type expression was invalid and an error was reported before,
   207  // cause will not be populated; thus cause alone cannot be used to determine
   208  // if an error occurred.
   209  func (check *Checker) genericType(e ast.Expr, cause *string) Type {
   210  	typ := check.typInternal(e, nil)
   211  	assert(isTyped(typ))
   212  	if isValid(typ) && !isGeneric(typ) {
   213  		if cause != nil {
   214  			*cause = check.sprintf("%s is not a generic type", typ)
   215  		}
   216  		typ = Typ[Invalid]
   217  	}
   218  	// TODO(gri) what is the correct call below?
   219  	check.recordTypeAndValue(e, typexpr, typ, nil)
   220  	return typ
   221  }
   222  
   223  // goTypeName returns the Go type name for typ and
   224  // removes any occurrences of "types." from that name.
   225  func goTypeName(typ Type) string {
   226  	return strings.ReplaceAll(fmt.Sprintf("%T", typ), "types.", "")
   227  }
   228  
   229  // typInternal drives type checking of types.
   230  // Must only be called by declaredType or genericType.
   231  func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
   232  	if check.conf._Trace {
   233  		check.trace(e0.Pos(), "-- type %s", e0)
   234  		check.indent++
   235  		defer func() {
   236  			check.indent--
   237  			var under Type
   238  			if T != nil {
   239  				// Calling T.Underlying() here may lead to endless instantiations.
   240  				// Test case: type T[P any] *T[P]
   241  				under = safeUnderlying(T)
   242  			}
   243  			if T == under {
   244  				check.trace(e0.Pos(), "=> %s // %s", T, goTypeName(T))
   245  			} else {
   246  				check.trace(e0.Pos(), "=> %s (under = %s) // %s", T, under, goTypeName(T))
   247  			}
   248  		}()
   249  	}
   250  
   251  	switch e := e0.(type) {
   252  	case *ast.BadExpr:
   253  		// ignore - error reported before
   254  
   255  	case *ast.Ident:
   256  		var x operand
   257  		check.ident(&x, e, true)
   258  
   259  		switch x.mode {
   260  		case typexpr:
   261  			return x.typ
   262  		case invalid:
   263  			// ignore - error reported before
   264  		case novalue:
   265  			check.errorf(&x, NotAType, "%s used as type", &x)
   266  		default:
   267  			check.errorf(&x, NotAType, "%s is not a type", &x)
   268  		}
   269  
   270  	case *ast.SelectorExpr:
   271  		var x operand
   272  		check.selector(&x, e, true)
   273  
   274  		switch x.mode {
   275  		case typexpr:
   276  			return x.typ
   277  		case invalid:
   278  			// ignore - error reported before
   279  		case novalue:
   280  			check.errorf(&x, NotAType, "%s used as type", &x)
   281  		default:
   282  			check.errorf(&x, NotAType, "%s is not a type", &x)
   283  		}
   284  
   285  	case *ast.IndexExpr, *ast.IndexListExpr:
   286  		ix := unpackIndexedExpr(e)
   287  		check.verifyVersionf(inNode(e, ix.lbrack), go1_18, "type instantiation")
   288  		return check.instantiatedType(ix)
   289  
   290  	case *ast.ParenExpr:
   291  		// Generic types must be instantiated before they can be used in any form.
   292  		// Consequently, generic types cannot be parenthesized.
   293  		return check.declaredType(e.X, def)
   294  
   295  	case *ast.ArrayType:
   296  		if e.Len == nil {
   297  			typ := new(Slice)
   298  			typ.elem = check.varType(e.Elt)
   299  			return typ
   300  		}
   301  
   302  		typ := new(Array)
   303  		// Provide a more specific error when encountering a [...] array
   304  		// rather than leaving it to the handling of the ... expression.
   305  		if _, ok := e.Len.(*ast.Ellipsis); ok {
   306  			check.error(e.Len, BadDotDotDotSyntax, "invalid use of [...] array (outside a composite literal)")
   307  			typ.len = -1
   308  		} else {
   309  			typ.len = check.arrayLength(e.Len)
   310  		}
   311  		typ.elem = check.varType(e.Elt)
   312  		if typ.len >= 0 {
   313  			return typ
   314  		}
   315  		// report error if we encountered [...]
   316  
   317  	case *ast.Ellipsis:
   318  		// dots are handled explicitly where they are valid
   319  		check.error(e, InvalidSyntaxTree, "invalid use of ...")
   320  
   321  	case *ast.StructType:
   322  		typ := new(Struct)
   323  		check.structType(typ, e)
   324  		return typ
   325  
   326  	case *ast.StarExpr:
   327  		typ := new(Pointer)
   328  		typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
   329  		typ.base = check.varType(e.X)
   330  		// If typ.base is invalid, it's unlikely that *base is particularly
   331  		// useful - even a valid dereferenciation will lead to an invalid
   332  		// type again, and in some cases we get unexpected follow-on errors
   333  		// (e.g., go.dev/issue/49005). Return an invalid type instead.
   334  		if !isValid(typ.base) {
   335  			return Typ[Invalid]
   336  		}
   337  		return typ
   338  
   339  	case *ast.FuncType:
   340  		typ := new(Signature)
   341  		check.funcType(typ, nil, e)
   342  		return typ
   343  
   344  	case *ast.InterfaceType:
   345  		typ := check.newInterface()
   346  		check.interfaceType(typ, e, def)
   347  		return typ
   348  
   349  	case *ast.MapType:
   350  		typ := new(Map)
   351  		typ.key = check.varType(e.Key)
   352  		typ.elem = check.varType(e.Value)
   353  
   354  		// spec: "The comparison operators == and != must be fully defined
   355  		// for operands of the key type; thus the key type must not be a
   356  		// function, map, or slice."
   357  		//
   358  		// Delay this check because it requires fully setup types;
   359  		// it is safe to continue in any case (was go.dev/issue/6667).
   360  		check.later(func() {
   361  			if !Comparable(typ.key) {
   362  				var why string
   363  				if isTypeParam(typ.key) {
   364  					why = " (missing comparable constraint)"
   365  				}
   366  				check.errorf(e.Key, IncomparableMapKey, "invalid map key type %s%s", typ.key, why)
   367  			}
   368  		}).describef(e.Key, "check map key %s", typ.key)
   369  
   370  		return typ
   371  
   372  	case *ast.ChanType:
   373  		typ := new(Chan)
   374  
   375  		dir := SendRecv
   376  		switch e.Dir {
   377  		case ast.SEND | ast.RECV:
   378  			// nothing to do
   379  		case ast.SEND:
   380  			dir = SendOnly
   381  		case ast.RECV:
   382  			dir = RecvOnly
   383  		default:
   384  			check.errorf(e, InvalidSyntaxTree, "unknown channel direction %d", e.Dir)
   385  			// ok to continue
   386  		}
   387  
   388  		typ.dir = dir
   389  		typ.elem = check.varType(e.Value)
   390  		return typ
   391  
   392  	default:
   393  		check.errorf(e0, NotAType, "%s is not a type", e0)
   394  		check.use(e0)
   395  	}
   396  
   397  	typ := Typ[Invalid]
   398  	return typ
   399  }
   400  
   401  func (check *Checker) instantiatedType(ix *indexedExpr) (res Type) {
   402  	if check.conf._Trace {
   403  		check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.x, ix.indices)
   404  		check.indent++
   405  		defer func() {
   406  			check.indent--
   407  			// Don't format the underlying here. It will always be nil.
   408  			check.trace(ix.Pos(), "=> %s", res)
   409  		}()
   410  	}
   411  
   412  	var cause string
   413  	typ := check.genericType(ix.x, &cause)
   414  	if cause != "" {
   415  		check.errorf(ix.orig, NotAGenericType, invalidOp+"%s (%s)", ix.orig, cause)
   416  	}
   417  	if !isValid(typ) {
   418  		return typ // error already reported
   419  	}
   420  	// typ must be a generic Alias or Named type (but not a *Signature)
   421  	if _, ok := typ.(*Signature); ok {
   422  		panic("unexpected generic signature")
   423  	}
   424  	gtyp := typ.(genericType)
   425  
   426  	// evaluate arguments
   427  	targs := check.typeList(ix.indices)
   428  	if targs == nil {
   429  		return Typ[Invalid]
   430  	}
   431  
   432  	// create instance
   433  	// The instance is not generic anymore as it has type arguments, but unless
   434  	// instantiation failed, it still satisfies the genericType interface because
   435  	// it has type parameters, too.
   436  	ityp := check.instance(ix.Pos(), gtyp, targs, nil, check.context())
   437  	inst, _ := ityp.(genericType)
   438  	if inst == nil {
   439  		return Typ[Invalid]
   440  	}
   441  
   442  	// For Named types, orig.tparams may not be set up, so we need to do expansion later.
   443  	check.later(func() {
   444  		// This is an instance from the source, not from recursive substitution,
   445  		// and so it must be resolved during type-checking so that we can report
   446  		// errors.
   447  		check.recordInstance(ix.orig, targs, inst)
   448  
   449  		name := inst.(interface{ Obj() *TypeName }).Obj().name
   450  		tparams := inst.TypeParams().list()
   451  		if check.validateTArgLen(ix.Pos(), name, len(tparams), len(targs)) {
   452  			// check type constraints
   453  			if i, err := check.verify(ix.Pos(), inst.TypeParams().list(), targs, check.context()); err != nil {
   454  				// best position for error reporting
   455  				pos := ix.Pos()
   456  				if i < len(ix.indices) {
   457  					pos = ix.indices[i].Pos()
   458  				}
   459  				check.softErrorf(atPos(pos), InvalidTypeArg, "%v", err)
   460  			} else {
   461  				check.mono.recordInstance(check.pkg, ix.Pos(), tparams, targs, ix.indices)
   462  			}
   463  		}
   464  	}).describef(ix, "verify instantiation %s", inst)
   465  
   466  	return inst
   467  }
   468  
   469  // arrayLength type-checks the array length expression e
   470  // and returns the constant length >= 0, or a value < 0
   471  // to indicate an error (and thus an unknown length).
   472  func (check *Checker) arrayLength(e ast.Expr) int64 {
   473  	// If e is an identifier, the array declaration might be an
   474  	// attempt at a parameterized type declaration with missing
   475  	// constraint. Provide an error message that mentions array
   476  	// length.
   477  	if name, _ := e.(*ast.Ident); name != nil {
   478  		obj := check.lookup(name.Name)
   479  		if obj == nil {
   480  			check.errorf(name, InvalidArrayLen, "undefined array length %s or missing type constraint", name.Name)
   481  			return -1
   482  		}
   483  		if _, ok := obj.(*Const); !ok {
   484  			check.errorf(name, InvalidArrayLen, "invalid array length %s", name.Name)
   485  			return -1
   486  		}
   487  	}
   488  
   489  	var x operand
   490  	check.expr(nil, &x, e)
   491  	if x.mode != constant_ {
   492  		if x.mode != invalid {
   493  			check.errorf(&x, InvalidArrayLen, "array length %s must be constant", &x)
   494  		}
   495  		return -1
   496  	}
   497  
   498  	if isUntyped(x.typ) || isInteger(x.typ) {
   499  		if val := constant.ToInt(x.val); val.Kind() == constant.Int {
   500  			if representableConst(val, check, Typ[Int], nil) {
   501  				if n, ok := constant.Int64Val(val); ok && n >= 0 {
   502  					return n
   503  				}
   504  			}
   505  		}
   506  	}
   507  
   508  	var msg string
   509  	if isInteger(x.typ) {
   510  		msg = "invalid array length %s"
   511  	} else {
   512  		msg = "array length %s must be integer"
   513  	}
   514  	check.errorf(&x, InvalidArrayLen, msg, &x)
   515  	return -1
   516  }
   517  
   518  // typeList provides the list of types corresponding to the incoming expression list.
   519  // If an error occurred, the result is nil, but all list elements were type-checked.
   520  func (check *Checker) typeList(list []ast.Expr) []Type {
   521  	res := make([]Type, len(list)) // res != nil even if len(list) == 0
   522  	for i, x := range list {
   523  		t := check.varType(x)
   524  		if !isValid(t) {
   525  			res = nil
   526  		}
   527  		if res != nil {
   528  			res[i] = t
   529  		}
   530  	}
   531  	return res
   532  }
   533  

View as plain text