Source file src/go/types/assignments.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/assignments.go
     3  
     4  // Copyright 2013 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // This file implements initialization and assignment checks.
     9  
    10  package types
    11  
    12  import (
    13  	"fmt"
    14  	"go/ast"
    15  	. "internal/types/errors"
    16  	"strings"
    17  )
    18  
    19  // assignment reports whether x can be assigned to a variable of type T,
    20  // if necessary by attempting to convert untyped values to the appropriate
    21  // type. context describes the context in which the assignment takes place.
    22  // Use T == nil to indicate assignment to an untyped blank identifier.
    23  // If the assignment check fails, x.mode is set to invalid.
    24  func (check *Checker) assignment(x *operand, T Type, context string) {
    25  	check.singleValue(x)
    26  
    27  	switch x.mode {
    28  	case invalid:
    29  		return // error reported before
    30  	case nilvalue:
    31  		assert(isTypes2)
    32  		// ok
    33  	case constant_, variable, mapindex, value, commaok, commaerr:
    34  		// ok
    35  	default:
    36  		// we may get here because of other problems (go.dev/issue/39634, crash 12)
    37  		// TODO(gri) do we need a new "generic" error code here?
    38  		check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context)
    39  		x.mode = invalid
    40  		return
    41  	}
    42  
    43  	if isUntyped(x.typ) {
    44  		target := T
    45  		// spec: "If an untyped constant is assigned to a variable of interface
    46  		// type or the blank identifier, the constant is first converted to type
    47  		// bool, rune, int, float64, complex128 or string respectively, depending
    48  		// on whether the value is a boolean, rune, integer, floating-point,
    49  		// complex, or string constant."
    50  		if isTypes2 {
    51  			if x.isNil() {
    52  				if T == nil {
    53  					check.errorf(x, UntypedNilUse, "use of untyped nil in %s", context)
    54  					x.mode = invalid
    55  					return
    56  				}
    57  			} else if T == nil || isNonTypeParamInterface(T) {
    58  				target = Default(x.typ)
    59  			}
    60  		} else { // go/types
    61  			if T == nil || isNonTypeParamInterface(T) {
    62  				if T == nil && x.typ == Typ[UntypedNil] {
    63  					check.errorf(x, UntypedNilUse, "use of untyped nil in %s", context)
    64  					x.mode = invalid
    65  					return
    66  				}
    67  				target = Default(x.typ)
    68  			}
    69  		}
    70  		newType, val, code := check.implicitTypeAndValue(x, target)
    71  		if code != 0 {
    72  			msg := check.sprintf("cannot use %s as %s value in %s", x, target, context)
    73  			switch code {
    74  			case TruncatedFloat:
    75  				msg += " (truncated)"
    76  			case NumericOverflow:
    77  				msg += " (overflows)"
    78  			default:
    79  				code = IncompatibleAssign
    80  			}
    81  			check.error(x, code, msg)
    82  			x.mode = invalid
    83  			return
    84  		}
    85  		if val != nil {
    86  			x.val = val
    87  			check.updateExprVal(x.expr, val)
    88  		}
    89  		if newType != x.typ {
    90  			x.typ = newType
    91  			check.updateExprType(x.expr, newType, false)
    92  		}
    93  	}
    94  	// x.typ is typed
    95  
    96  	// A generic (non-instantiated) function value cannot be assigned to a variable.
    97  	if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
    98  		check.errorf(x, WrongTypeArgCount, "cannot use generic function %s without instantiation in %s", x, context)
    99  		x.mode = invalid
   100  		return
   101  	}
   102  
   103  	// spec: "If a left-hand side is the blank identifier, any typed or
   104  	// non-constant value except for the predeclared identifier nil may
   105  	// be assigned to it."
   106  	if T == nil {
   107  		return
   108  	}
   109  
   110  	cause := ""
   111  	if ok, code := x.assignableTo(check, T, &cause); !ok {
   112  		if cause != "" {
   113  			check.errorf(x, code, "cannot use %s as %s value in %s: %s", x, T, context, cause)
   114  		} else {
   115  			check.errorf(x, code, "cannot use %s as %s value in %s", x, T, context)
   116  		}
   117  		x.mode = invalid
   118  	}
   119  }
   120  
   121  func (check *Checker) initConst(lhs *Const, x *operand) {
   122  	if x.mode == invalid || !isValid(x.typ) || !isValid(lhs.typ) {
   123  		if lhs.typ == nil {
   124  			lhs.typ = Typ[Invalid]
   125  		}
   126  		return
   127  	}
   128  
   129  	// rhs must be a constant
   130  	if x.mode != constant_ {
   131  		check.errorf(x, InvalidConstInit, "%s is not constant", x)
   132  		if lhs.typ == nil {
   133  			lhs.typ = Typ[Invalid]
   134  		}
   135  		return
   136  	}
   137  	assert(isConstType(x.typ))
   138  
   139  	// If the lhs doesn't have a type yet, use the type of x.
   140  	if lhs.typ == nil {
   141  		lhs.typ = x.typ
   142  	}
   143  
   144  	check.assignment(x, lhs.typ, "constant declaration")
   145  	if x.mode == invalid {
   146  		return
   147  	}
   148  
   149  	lhs.val = x.val
   150  }
   151  
   152  // initVar checks the initialization lhs = x in a variable declaration.
   153  // If lhs doesn't have a type yet, it is given the type of x,
   154  // or Typ[Invalid] in case of an error.
   155  // If the initialization check fails, x.mode is set to invalid.
   156  func (check *Checker) initVar(lhs *Var, x *operand, context string) {
   157  	if x.mode == invalid || !isValid(x.typ) || !isValid(lhs.typ) {
   158  		if lhs.typ == nil {
   159  			lhs.typ = Typ[Invalid]
   160  		}
   161  		x.mode = invalid
   162  		return
   163  	}
   164  
   165  	// If lhs doesn't have a type yet, use the type of x.
   166  	if lhs.typ == nil {
   167  		typ := x.typ
   168  		if isUntyped(typ) {
   169  			// convert untyped types to default types
   170  			if typ == Typ[UntypedNil] {
   171  				check.errorf(x, UntypedNilUse, "use of untyped nil in %s", context)
   172  				lhs.typ = Typ[Invalid]
   173  				x.mode = invalid
   174  				return
   175  			}
   176  			typ = Default(typ)
   177  		}
   178  		lhs.typ = typ
   179  	}
   180  
   181  	check.assignment(x, lhs.typ, context)
   182  }
   183  
   184  // lhsVar checks a lhs variable in an assignment and returns its type.
   185  // lhsVar takes care of not counting a lhs identifier as a "use" of
   186  // that identifier. The result is nil if it is the blank identifier,
   187  // and Typ[Invalid] if it is an invalid lhs expression.
   188  func (check *Checker) lhsVar(lhs ast.Expr) Type {
   189  	// Determine if the lhs is a (possibly parenthesized) identifier.
   190  	ident, _ := ast.Unparen(lhs).(*ast.Ident)
   191  
   192  	// Don't evaluate lhs if it is the blank identifier.
   193  	if ident != nil && ident.Name == "_" {
   194  		check.recordDef(ident, nil)
   195  		return nil
   196  	}
   197  
   198  	// If the lhs is an identifier denoting a variable v, this reference
   199  	// is not a 'use' of v. Remember current value of v.used and restore
   200  	// after evaluating the lhs via check.expr.
   201  	var v *Var
   202  	var v_used bool
   203  	if ident != nil {
   204  		if obj := check.lookup(ident.Name); obj != nil {
   205  			// It's ok to mark non-local variables, but ignore variables
   206  			// from other packages to avoid potential race conditions with
   207  			// dot-imported variables.
   208  			if w, _ := obj.(*Var); w != nil && w.pkg == check.pkg {
   209  				v = w
   210  				v_used = v.used
   211  			}
   212  		}
   213  	}
   214  
   215  	var x operand
   216  	check.expr(nil, &x, lhs)
   217  
   218  	if v != nil {
   219  		v.used = v_used // restore v.used
   220  	}
   221  
   222  	if x.mode == invalid || !isValid(x.typ) {
   223  		return Typ[Invalid]
   224  	}
   225  
   226  	// spec: "Each left-hand side operand must be addressable, a map index
   227  	// expression, or the blank identifier. Operands may be parenthesized."
   228  	switch x.mode {
   229  	case invalid:
   230  		return Typ[Invalid]
   231  	case variable, mapindex:
   232  		// ok
   233  	default:
   234  		if sel, ok := x.expr.(*ast.SelectorExpr); ok {
   235  			var op operand
   236  			check.expr(nil, &op, sel.X)
   237  			if op.mode == mapindex {
   238  				check.errorf(&x, UnaddressableFieldAssign, "cannot assign to struct field %s in map", ExprString(x.expr))
   239  				return Typ[Invalid]
   240  			}
   241  		}
   242  		check.errorf(&x, UnassignableOperand, "cannot assign to %s (neither addressable nor a map index expression)", x.expr)
   243  		return Typ[Invalid]
   244  	}
   245  
   246  	return x.typ
   247  }
   248  
   249  // assignVar checks the assignment lhs = rhs (if x == nil), or lhs = x (if x != nil).
   250  // If x != nil, it must be the evaluation of rhs (and rhs will be ignored).
   251  // If the assignment check fails and x != nil, x.mode is set to invalid.
   252  func (check *Checker) assignVar(lhs, rhs ast.Expr, x *operand, context string) {
   253  	T := check.lhsVar(lhs) // nil if lhs is _
   254  	if !isValid(T) {
   255  		if x != nil {
   256  			x.mode = invalid
   257  		} else {
   258  			check.use(rhs)
   259  		}
   260  		return
   261  	}
   262  
   263  	if x == nil {
   264  		var target *target
   265  		// avoid calling ExprString if not needed
   266  		if T != nil {
   267  			if _, ok := under(T).(*Signature); ok {
   268  				target = newTarget(T, ExprString(lhs))
   269  			}
   270  		}
   271  		x = new(operand)
   272  		check.expr(target, x, rhs)
   273  	}
   274  
   275  	if T == nil && context == "assignment" {
   276  		context = "assignment to _ identifier"
   277  	}
   278  	check.assignment(x, T, context)
   279  }
   280  
   281  // operandTypes returns the list of types for the given operands.
   282  func operandTypes(list []*operand) (res []Type) {
   283  	for _, x := range list {
   284  		res = append(res, x.typ)
   285  	}
   286  	return res
   287  }
   288  
   289  // varTypes returns the list of types for the given variables.
   290  func varTypes(list []*Var) (res []Type) {
   291  	for _, x := range list {
   292  		res = append(res, x.typ)
   293  	}
   294  	return res
   295  }
   296  
   297  // typesSummary returns a string of the form "(t1, t2, ...)" where the
   298  // ti's are user-friendly string representations for the given types.
   299  // If variadic is set and the last type is a slice, its string is of
   300  // the form "...E" where E is the slice's element type.
   301  func (check *Checker) typesSummary(list []Type, variadic bool) string {
   302  	var res []string
   303  	for i, t := range list {
   304  		var s string
   305  		switch {
   306  		case t == nil:
   307  			fallthrough // should not happen but be cautious
   308  		case !isValid(t):
   309  			s = "unknown type"
   310  		case isUntyped(t):
   311  			if isNumeric(t) {
   312  				// Do not imply a specific type requirement:
   313  				// "have number, want float64" is better than
   314  				// "have untyped int, want float64" or
   315  				// "have int, want float64".
   316  				s = "number"
   317  			} else {
   318  				// If we don't have a number, omit the "untyped" qualifier
   319  				// for compactness.
   320  				s = strings.Replace(t.(*Basic).name, "untyped ", "", -1)
   321  			}
   322  		case variadic && i == len(list)-1:
   323  			s = check.sprintf("...%s", t.(*Slice).elem)
   324  		}
   325  		if s == "" {
   326  			s = check.sprintf("%s", t)
   327  		}
   328  		res = append(res, s)
   329  	}
   330  	return "(" + strings.Join(res, ", ") + ")"
   331  }
   332  
   333  func measure(x int, unit string) string {
   334  	if x != 1 {
   335  		unit += "s"
   336  	}
   337  	return fmt.Sprintf("%d %s", x, unit)
   338  }
   339  
   340  func (check *Checker) assignError(rhs []ast.Expr, l, r int) {
   341  	vars := measure(l, "variable")
   342  	vals := measure(r, "value")
   343  	rhs0 := rhs[0]
   344  
   345  	if len(rhs) == 1 {
   346  		if call, _ := ast.Unparen(rhs0).(*ast.CallExpr); call != nil {
   347  			check.errorf(rhs0, WrongAssignCount, "assignment mismatch: %s but %s returns %s", vars, call.Fun, vals)
   348  			return
   349  		}
   350  	}
   351  	check.errorf(rhs0, WrongAssignCount, "assignment mismatch: %s but %s", vars, vals)
   352  }
   353  
   354  func (check *Checker) returnError(at positioner, lhs []*Var, rhs []*operand) {
   355  	l, r := len(lhs), len(rhs)
   356  	qualifier := "not enough"
   357  	if r > l {
   358  		at = rhs[l] // report at first extra value
   359  		qualifier = "too many"
   360  	} else if r > 0 {
   361  		at = rhs[r-1] // report at last value
   362  	}
   363  	err := check.newError(WrongResultCount)
   364  	err.addf(at, "%s return values", qualifier)
   365  	err.addf(noposn, "have %s", check.typesSummary(operandTypes(rhs), false))
   366  	err.addf(noposn, "want %s", check.typesSummary(varTypes(lhs), false))
   367  	err.report()
   368  }
   369  
   370  // initVars type-checks assignments of initialization expressions orig_rhs
   371  // to variables lhs.
   372  // If returnStmt is non-nil, initVars type-checks the implicit assignment
   373  // of result expressions orig_rhs to function result parameters lhs.
   374  func (check *Checker) initVars(lhs []*Var, orig_rhs []ast.Expr, returnStmt ast.Stmt) {
   375  	context := "assignment"
   376  	if returnStmt != nil {
   377  		context = "return statement"
   378  	}
   379  
   380  	l, r := len(lhs), len(orig_rhs)
   381  
   382  	// If l == 1 and the rhs is a single call, for a better
   383  	// error message don't handle it as n:n mapping below.
   384  	isCall := false
   385  	if r == 1 {
   386  		_, isCall = ast.Unparen(orig_rhs[0]).(*ast.CallExpr)
   387  	}
   388  
   389  	// If we have a n:n mapping from lhs variable to rhs expression,
   390  	// each value can be assigned to its corresponding variable.
   391  	if l == r && !isCall {
   392  		var x operand
   393  		for i, lhs := range lhs {
   394  			desc := lhs.name
   395  			if returnStmt != nil && desc == "" {
   396  				desc = "result variable"
   397  			}
   398  			check.expr(newTarget(lhs.typ, desc), &x, orig_rhs[i])
   399  			check.initVar(lhs, &x, context)
   400  		}
   401  		return
   402  	}
   403  
   404  	// If we don't have an n:n mapping, the rhs must be a single expression
   405  	// resulting in 2 or more values; otherwise we have an assignment mismatch.
   406  	if r != 1 {
   407  		// Only report a mismatch error if there are no other errors on the rhs.
   408  		if check.use(orig_rhs...) {
   409  			if returnStmt != nil {
   410  				rhs := check.exprList(orig_rhs)
   411  				check.returnError(returnStmt, lhs, rhs)
   412  			} else {
   413  				check.assignError(orig_rhs, l, r)
   414  			}
   415  		}
   416  		// ensure that LHS variables have a type
   417  		for _, v := range lhs {
   418  			if v.typ == nil {
   419  				v.typ = Typ[Invalid]
   420  			}
   421  		}
   422  		return
   423  	}
   424  
   425  	rhs, commaOk := check.multiExpr(orig_rhs[0], l == 2 && returnStmt == nil)
   426  	r = len(rhs)
   427  	if l == r {
   428  		for i, lhs := range lhs {
   429  			check.initVar(lhs, rhs[i], context)
   430  		}
   431  		// Only record comma-ok expression if both initializations succeeded
   432  		// (go.dev/issue/59371).
   433  		if commaOk && rhs[0].mode != invalid && rhs[1].mode != invalid {
   434  			check.recordCommaOkTypes(orig_rhs[0], rhs)
   435  		}
   436  		return
   437  	}
   438  
   439  	// In all other cases we have an assignment mismatch.
   440  	// Only report a mismatch error if there are no other errors on the rhs.
   441  	if rhs[0].mode != invalid {
   442  		if returnStmt != nil {
   443  			check.returnError(returnStmt, lhs, rhs)
   444  		} else {
   445  			check.assignError(orig_rhs, l, r)
   446  		}
   447  	}
   448  	// ensure that LHS variables have a type
   449  	for _, v := range lhs {
   450  		if v.typ == nil {
   451  			v.typ = Typ[Invalid]
   452  		}
   453  	}
   454  	// orig_rhs[0] was already evaluated
   455  }
   456  
   457  // assignVars type-checks assignments of expressions orig_rhs to variables lhs.
   458  func (check *Checker) assignVars(lhs, orig_rhs []ast.Expr) {
   459  	l, r := len(lhs), len(orig_rhs)
   460  
   461  	// If l == 1 and the rhs is a single call, for a better
   462  	// error message don't handle it as n:n mapping below.
   463  	isCall := false
   464  	if r == 1 {
   465  		_, isCall = ast.Unparen(orig_rhs[0]).(*ast.CallExpr)
   466  	}
   467  
   468  	// If we have a n:n mapping from lhs variable to rhs expression,
   469  	// each value can be assigned to its corresponding variable.
   470  	if l == r && !isCall {
   471  		for i, lhs := range lhs {
   472  			check.assignVar(lhs, orig_rhs[i], nil, "assignment")
   473  		}
   474  		return
   475  	}
   476  
   477  	// If we don't have an n:n mapping, the rhs must be a single expression
   478  	// resulting in 2 or more values; otherwise we have an assignment mismatch.
   479  	if r != 1 {
   480  		// Only report a mismatch error if there are no other errors on the lhs or rhs.
   481  		okLHS := check.useLHS(lhs...)
   482  		okRHS := check.use(orig_rhs...)
   483  		if okLHS && okRHS {
   484  			check.assignError(orig_rhs, l, r)
   485  		}
   486  		return
   487  	}
   488  
   489  	rhs, commaOk := check.multiExpr(orig_rhs[0], l == 2)
   490  	r = len(rhs)
   491  	if l == r {
   492  		for i, lhs := range lhs {
   493  			check.assignVar(lhs, nil, rhs[i], "assignment")
   494  		}
   495  		// Only record comma-ok expression if both assignments succeeded
   496  		// (go.dev/issue/59371).
   497  		if commaOk && rhs[0].mode != invalid && rhs[1].mode != invalid {
   498  			check.recordCommaOkTypes(orig_rhs[0], rhs)
   499  		}
   500  		return
   501  	}
   502  
   503  	// In all other cases we have an assignment mismatch.
   504  	// Only report a mismatch error if there are no other errors on the rhs.
   505  	if rhs[0].mode != invalid {
   506  		check.assignError(orig_rhs, l, r)
   507  	}
   508  	check.useLHS(lhs...)
   509  	// orig_rhs[0] was already evaluated
   510  }
   511  
   512  func (check *Checker) shortVarDecl(pos positioner, lhs, rhs []ast.Expr) {
   513  	top := len(check.delayed)
   514  	scope := check.scope
   515  
   516  	// collect lhs variables
   517  	seen := make(map[string]bool, len(lhs))
   518  	lhsVars := make([]*Var, len(lhs))
   519  	newVars := make([]*Var, 0, len(lhs))
   520  	hasErr := false
   521  	for i, lhs := range lhs {
   522  		ident, _ := lhs.(*ast.Ident)
   523  		if ident == nil {
   524  			check.useLHS(lhs)
   525  			// TODO(gri) This is redundant with a go/parser error. Consider omitting in go/types?
   526  			check.errorf(lhs, BadDecl, "non-name %s on left side of :=", lhs)
   527  			hasErr = true
   528  			continue
   529  		}
   530  
   531  		name := ident.Name
   532  		if name != "_" {
   533  			if seen[name] {
   534  				check.errorf(lhs, RepeatedDecl, "%s repeated on left side of :=", lhs)
   535  				hasErr = true
   536  				continue
   537  			}
   538  			seen[name] = true
   539  		}
   540  
   541  		// Use the correct obj if the ident is redeclared. The
   542  		// variable's scope starts after the declaration; so we
   543  		// must use Scope.Lookup here and call Scope.Insert
   544  		// (via check.declare) later.
   545  		if alt := scope.Lookup(name); alt != nil {
   546  			check.recordUse(ident, alt)
   547  			// redeclared object must be a variable
   548  			if obj, _ := alt.(*Var); obj != nil {
   549  				lhsVars[i] = obj
   550  			} else {
   551  				check.errorf(lhs, UnassignableOperand, "cannot assign to %s", lhs)
   552  				hasErr = true
   553  			}
   554  			continue
   555  		}
   556  
   557  		// declare new variable
   558  		obj := NewVar(ident.Pos(), check.pkg, name, nil)
   559  		lhsVars[i] = obj
   560  		if name != "_" {
   561  			newVars = append(newVars, obj)
   562  		}
   563  		check.recordDef(ident, obj)
   564  	}
   565  
   566  	// create dummy variables where the lhs is invalid
   567  	for i, obj := range lhsVars {
   568  		if obj == nil {
   569  			lhsVars[i] = NewVar(lhs[i].Pos(), check.pkg, "_", nil)
   570  		}
   571  	}
   572  
   573  	check.initVars(lhsVars, rhs, nil)
   574  
   575  	// process function literals in rhs expressions before scope changes
   576  	check.processDelayed(top)
   577  
   578  	if len(newVars) == 0 && !hasErr {
   579  		check.softErrorf(pos, NoNewVar, "no new variables on left side of :=")
   580  		return
   581  	}
   582  
   583  	// declare new variables
   584  	// spec: "The scope of a constant or variable identifier declared inside
   585  	// a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl
   586  	// for short variable declarations) and ends at the end of the innermost
   587  	// containing block."
   588  	scopePos := endPos(rhs[len(rhs)-1])
   589  	for _, obj := range newVars {
   590  		check.declare(scope, nil, obj, scopePos) // id = nil: recordDef already called
   591  	}
   592  }
   593  

View as plain text