Source file src/go/types/stmt.go

     1  // Copyright 2012 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 typechecking of statements.
     6  
     7  package types
     8  
     9  import (
    10  	"go/ast"
    11  	"go/constant"
    12  	"go/token"
    13  	. "internal/types/errors"
    14  	"slices"
    15  )
    16  
    17  // decl may be nil
    18  func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body *ast.BlockStmt, iota constant.Value) {
    19  	if check.conf.IgnoreFuncBodies {
    20  		panic("function body not ignored")
    21  	}
    22  
    23  	if check.conf._Trace {
    24  		check.trace(body.Pos(), "-- %s: %s", name, sig)
    25  	}
    26  
    27  	// save/restore current environment and set up function environment
    28  	// (and use 0 indentation at function start)
    29  	defer func(env environment, indent int) {
    30  		check.environment = env
    31  		check.indent = indent
    32  	}(check.environment, check.indent)
    33  	check.environment = environment{
    34  		decl:    decl,
    35  		scope:   sig.scope,
    36  		version: check.version, // TODO(adonovan): would decl.version (if decl != nil) be better?
    37  		iota:    iota,
    38  		sig:     sig,
    39  	}
    40  	check.indent = 0
    41  
    42  	check.stmtList(0, body.List)
    43  
    44  	if check.hasLabel {
    45  		check.labels(body)
    46  	}
    47  
    48  	if sig.results.Len() > 0 && !check.isTerminating(body, "") {
    49  		check.error(atPos(body.Rbrace), MissingReturn, "missing return")
    50  	}
    51  
    52  	// spec: "Implementation restriction: A compiler may make it illegal to
    53  	// declare a variable inside a function body if the variable is never used."
    54  	check.usage(sig.scope)
    55  }
    56  
    57  func (check *Checker) usage(scope *Scope) {
    58  	needUse := func(kind VarKind) bool {
    59  		return !(kind == RecvVar || kind == ParamVar || kind == ResultVar)
    60  	}
    61  	var unused []*Var
    62  	for name, elem := range scope.elems {
    63  		elem = resolve(name, elem)
    64  		if v, _ := elem.(*Var); v != nil && needUse(v.kind) && !check.usedVars[v] {
    65  			unused = append(unused, v)
    66  		}
    67  	}
    68  	slices.SortFunc(unused, func(a, b *Var) int {
    69  		return cmpPos(a.pos, b.pos)
    70  	})
    71  	for _, v := range unused {
    72  		check.softErrorf(v, UnusedVar, "declared and not used: %s", v.name)
    73  	}
    74  
    75  	for _, scope := range scope.children {
    76  		// Don't go inside function literal scopes a second time;
    77  		// they are handled explicitly by funcBody.
    78  		if !scope.isFunc {
    79  			check.usage(scope)
    80  		}
    81  	}
    82  }
    83  
    84  // stmtContext is a bitset describing which
    85  // control-flow statements are permissible,
    86  // and provides additional context information
    87  // for better error messages.
    88  type stmtContext uint
    89  
    90  const (
    91  	// permissible control-flow statements
    92  	breakOk stmtContext = 1 << iota
    93  	continueOk
    94  	fallthroughOk
    95  
    96  	// additional context information
    97  	finalSwitchCase
    98  	inTypeSwitch
    99  )
   100  
   101  func (check *Checker) simpleStmt(s ast.Stmt) {
   102  	if s != nil {
   103  		check.stmt(0, s)
   104  	}
   105  }
   106  
   107  func trimTrailingEmptyStmts(list []ast.Stmt) []ast.Stmt {
   108  	for i := len(list); i > 0; i-- {
   109  		if _, ok := list[i-1].(*ast.EmptyStmt); !ok {
   110  			return list[:i]
   111  		}
   112  	}
   113  	return nil
   114  }
   115  
   116  func (check *Checker) stmtList(ctxt stmtContext, list []ast.Stmt) {
   117  	ok := ctxt&fallthroughOk != 0
   118  	inner := ctxt &^ fallthroughOk
   119  	list = trimTrailingEmptyStmts(list) // trailing empty statements are "invisible" to fallthrough analysis
   120  	for i, s := range list {
   121  		inner := inner
   122  		if ok && i+1 == len(list) {
   123  			inner |= fallthroughOk
   124  		}
   125  		check.stmt(inner, s)
   126  	}
   127  }
   128  
   129  func (check *Checker) multipleDefaults(list []ast.Stmt) {
   130  	var first ast.Stmt
   131  	for _, s := range list {
   132  		var d ast.Stmt
   133  		switch c := s.(type) {
   134  		case *ast.CaseClause:
   135  			if len(c.List) == 0 {
   136  				d = s
   137  			}
   138  		case *ast.CommClause:
   139  			if c.Comm == nil {
   140  				d = s
   141  			}
   142  		default:
   143  			check.error(s, InvalidSyntaxTree, "case/communication clause expected")
   144  		}
   145  		if d != nil {
   146  			if first != nil {
   147  				check.errorf(d, DuplicateDefault, "multiple defaults (first at %s)", check.fset.Position(first.Pos()))
   148  			} else {
   149  				first = d
   150  			}
   151  		}
   152  	}
   153  }
   154  
   155  func (check *Checker) openScope(node ast.Node, comment string) {
   156  	scope := NewScope(check.scope, node.Pos(), node.End(), comment)
   157  	check.recordScope(node, scope)
   158  	check.scope = scope
   159  }
   160  
   161  func (check *Checker) closeScope() {
   162  	check.scope = check.scope.Parent()
   163  }
   164  
   165  func assignOp(op token.Token) token.Token {
   166  	// token_test.go verifies the token ordering this function relies on
   167  	if token.ADD_ASSIGN <= op && op <= token.AND_NOT_ASSIGN {
   168  		return op + (token.ADD - token.ADD_ASSIGN)
   169  	}
   170  	return token.ILLEGAL
   171  }
   172  
   173  func (check *Checker) suspendedCall(keyword string, call *ast.CallExpr) {
   174  	var x operand
   175  	var msg string
   176  	var code Code
   177  	switch check.rawExpr(nil, &x, call, nil, false) {
   178  	case conversion:
   179  		msg = "requires function call, not conversion"
   180  		code = InvalidDefer
   181  		if keyword == "go" {
   182  			code = InvalidGo
   183  		}
   184  	case expression:
   185  		msg = "discards result of"
   186  		code = UnusedResults
   187  	case statement:
   188  		return
   189  	default:
   190  		panic("unreachable")
   191  	}
   192  	check.errorf(&x, code, "%s %s %s", keyword, msg, &x)
   193  }
   194  
   195  // goVal returns the Go value for val, or nil.
   196  func goVal(val constant.Value) any {
   197  	// val should exist, but be conservative and check
   198  	if val == nil {
   199  		return nil
   200  	}
   201  	// Match implementation restriction of other compilers.
   202  	// gc only checks duplicates for integer, floating-point
   203  	// and string values, so only create Go values for these
   204  	// types.
   205  	switch val.Kind() {
   206  	case constant.Int:
   207  		if x, ok := constant.Int64Val(val); ok {
   208  			return x
   209  		}
   210  		if x, ok := constant.Uint64Val(val); ok {
   211  			return x
   212  		}
   213  	case constant.Float:
   214  		if x, ok := constant.Float64Val(val); ok {
   215  			return x
   216  		}
   217  	case constant.String:
   218  		return constant.StringVal(val)
   219  	}
   220  	return nil
   221  }
   222  
   223  // A valueMap maps a case value (of a basic Go type) to a list of positions
   224  // where the same case value appeared, together with the corresponding case
   225  // types.
   226  // Since two case values may have the same "underlying" value but different
   227  // types we need to also check the value's types (e.g., byte(1) vs myByte(1))
   228  // when the switch expression is of interface type.
   229  type (
   230  	valueMap  map[any][]valueType // underlying Go value -> valueType
   231  	valueType struct {
   232  		pos token.Pos
   233  		typ Type
   234  	}
   235  )
   236  
   237  func (check *Checker) caseValues(x *operand, values []ast.Expr, seen valueMap) {
   238  L:
   239  	for _, e := range values {
   240  		var v operand
   241  		check.expr(nil, &v, e)
   242  		if x.mode == invalid || v.mode == invalid {
   243  			continue L
   244  		}
   245  		check.convertUntyped(&v, x.typ)
   246  		if v.mode == invalid {
   247  			continue L
   248  		}
   249  		// Order matters: By comparing v against x, error positions are at the case values.
   250  		res := v // keep original v unchanged
   251  		check.comparison(&res, x, token.EQL, true)
   252  		if res.mode == invalid {
   253  			continue L
   254  		}
   255  		if v.mode != constant_ {
   256  			continue L // we're done
   257  		}
   258  		// look for duplicate values
   259  		if val := goVal(v.val); val != nil {
   260  			// look for duplicate types for a given value
   261  			// (quadratic algorithm, but these lists tend to be very short)
   262  			for _, vt := range seen[val] {
   263  				if Identical(v.typ, vt.typ) {
   264  					err := check.newError(DuplicateCase)
   265  					err.addf(&v, "duplicate case %s in expression switch", &v)
   266  					err.addf(atPos(vt.pos), "previous case")
   267  					err.report()
   268  					continue L
   269  				}
   270  			}
   271  			seen[val] = append(seen[val], valueType{v.Pos(), v.typ})
   272  		}
   273  	}
   274  }
   275  
   276  // isNil reports whether the expression e denotes the predeclared value nil.
   277  func (check *Checker) isNil(e ast.Expr) bool {
   278  	// The only way to express the nil value is by literally writing nil (possibly in parentheses).
   279  	if name, _ := ast.Unparen(e).(*ast.Ident); name != nil {
   280  		_, ok := check.lookup(name.Name).(*Nil)
   281  		return ok
   282  	}
   283  	return false
   284  }
   285  
   286  // caseTypes typechecks the type expressions of a type case, checks for duplicate types
   287  // using the seen map, and verifies that each type is valid with respect to the type of
   288  // the operand x corresponding to the type switch expression. If that expression is not
   289  // valid, x must be nil.
   290  //
   291  //	switch <x>.(type) {
   292  //	case <types>: ...
   293  //	...
   294  //	}
   295  //
   296  // caseTypes returns the case-specific type for a variable v introduced through a short
   297  // variable declaration by the type switch:
   298  //
   299  //	switch v := <x>.(type) {
   300  //	case <types>: // T is the type of <v> in this case
   301  //	...
   302  //	}
   303  //
   304  // If there is exactly one type expression, T is the type of that expression. If there
   305  // are multiple type expressions, or if predeclared nil is among the types, the result
   306  // is the type of x. If x is invalid (nil), the result is the invalid type.
   307  func (check *Checker) caseTypes(x *operand, types []ast.Expr, seen map[Type]ast.Expr) Type {
   308  	var T Type
   309  	var dummy operand
   310  L:
   311  	for _, e := range types {
   312  		// The spec allows the value nil instead of a type.
   313  		if check.isNil(e) {
   314  			T = nil
   315  			check.expr(nil, &dummy, e) // run e through expr so we get the usual Info recordings
   316  		} else {
   317  			T = check.varType(e)
   318  			if !isValid(T) {
   319  				continue L
   320  			}
   321  		}
   322  		// look for duplicate types
   323  		// (quadratic algorithm, but type switches tend to be reasonably small)
   324  		for t, other := range seen {
   325  			if T == nil && t == nil || T != nil && t != nil && Identical(T, t) {
   326  				// talk about "case" rather than "type" because of nil case
   327  				Ts := "nil"
   328  				if T != nil {
   329  					Ts = TypeString(T, check.qualifier)
   330  				}
   331  				err := check.newError(DuplicateCase)
   332  				err.addf(e, "duplicate case %s in type switch", Ts)
   333  				err.addf(other, "previous case")
   334  				err.report()
   335  				continue L
   336  			}
   337  		}
   338  		seen[T] = e
   339  		if x != nil && T != nil {
   340  			check.typeAssertion(e, x, T, true)
   341  		}
   342  	}
   343  
   344  	// spec: "In clauses with a case listing exactly one type, the variable has that type;
   345  	// otherwise, the variable has the type of the expression in the TypeSwitchGuard.
   346  	if len(types) != 1 || T == nil {
   347  		T = Typ[Invalid]
   348  		if x != nil {
   349  			T = x.typ
   350  		}
   351  	}
   352  
   353  	assert(T != nil)
   354  	return T
   355  }
   356  
   357  // TODO(gri) Once we are certain that typeHash is correct in all situations, use this version of caseTypes instead.
   358  // (Currently it may be possible that different types have identical names and import paths due to ImporterFrom.)
   359  func (check *Checker) caseTypes_currently_unused(x *operand, xtyp *Interface, types []ast.Expr, seen map[string]ast.Expr) Type {
   360  	var T Type
   361  	var dummy operand
   362  L:
   363  	for _, e := range types {
   364  		// The spec allows the value nil instead of a type.
   365  		var hash string
   366  		if check.isNil(e) {
   367  			check.expr(nil, &dummy, e) // run e through expr so we get the usual Info recordings
   368  			T = nil
   369  			hash = "<nil>" // avoid collision with a type named nil
   370  		} else {
   371  			T = check.varType(e)
   372  			if !isValid(T) {
   373  				continue L
   374  			}
   375  			panic("enable typeHash(T, nil)")
   376  			// hash = typeHash(T, nil)
   377  		}
   378  		// look for duplicate types
   379  		if other := seen[hash]; other != nil {
   380  			// talk about "case" rather than "type" because of nil case
   381  			Ts := "nil"
   382  			if T != nil {
   383  				Ts = TypeString(T, check.qualifier)
   384  			}
   385  			err := check.newError(DuplicateCase)
   386  			err.addf(e, "duplicate case %s in type switch", Ts)
   387  			err.addf(other, "previous case")
   388  			err.report()
   389  			continue L
   390  		}
   391  		seen[hash] = e
   392  		if T != nil {
   393  			check.typeAssertion(e, x, T, true)
   394  		}
   395  	}
   396  
   397  	// spec: "In clauses with a case listing exactly one type, the variable has that type;
   398  	// otherwise, the variable has the type of the expression in the TypeSwitchGuard.
   399  	if len(types) != 1 || T == nil {
   400  		T = Typ[Invalid]
   401  		if x != nil {
   402  			T = x.typ
   403  		}
   404  	}
   405  
   406  	assert(T != nil)
   407  	return T
   408  }
   409  
   410  // stmt typechecks statement s.
   411  func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
   412  	// statements must end with the same top scope as they started with
   413  	if debug {
   414  		defer func(scope *Scope) {
   415  			// don't check if code is panicking
   416  			if p := recover(); p != nil {
   417  				panic(p)
   418  			}
   419  			assert(scope == check.scope)
   420  		}(check.scope)
   421  	}
   422  
   423  	// process collected function literals before scope changes
   424  	defer check.processDelayed(len(check.delayed))
   425  
   426  	// reset context for statements of inner blocks
   427  	inner := ctxt &^ (fallthroughOk | finalSwitchCase | inTypeSwitch)
   428  
   429  	switch s := s.(type) {
   430  	case *ast.BadStmt, *ast.EmptyStmt:
   431  		// ignore
   432  
   433  	case *ast.DeclStmt:
   434  		check.declStmt(s.Decl)
   435  
   436  	case *ast.LabeledStmt:
   437  		check.hasLabel = true
   438  		check.stmt(ctxt, s.Stmt)
   439  
   440  	case *ast.ExprStmt:
   441  		// spec: "With the exception of specific built-in functions,
   442  		// function and method calls and receive operations can appear
   443  		// in statement context. Such statements may be parenthesized."
   444  		var x operand
   445  		kind := check.rawExpr(nil, &x, s.X, nil, false)
   446  		var msg string
   447  		var code Code
   448  		switch x.mode {
   449  		default:
   450  			if kind == statement {
   451  				return
   452  			}
   453  			msg = "is not used"
   454  			code = UnusedExpr
   455  		case builtin:
   456  			msg = "must be called"
   457  			code = UncalledBuiltin
   458  		case typexpr:
   459  			msg = "is not an expression"
   460  			code = NotAnExpr
   461  		}
   462  		check.errorf(&x, code, "%s %s", &x, msg)
   463  
   464  	case *ast.SendStmt:
   465  		var ch, val operand
   466  		check.expr(nil, &ch, s.Chan)
   467  		check.expr(nil, &val, s.Value)
   468  		if ch.mode == invalid || val.mode == invalid {
   469  			return
   470  		}
   471  		if elem := check.chanElem(inNode(s, s.Arrow), &ch, false); elem != nil {
   472  			check.assignment(&val, elem, "send")
   473  		}
   474  
   475  	case *ast.IncDecStmt:
   476  		var op token.Token
   477  		switch s.Tok {
   478  		case token.INC:
   479  			op = token.ADD
   480  		case token.DEC:
   481  			op = token.SUB
   482  		default:
   483  			check.errorf(inNode(s, s.TokPos), InvalidSyntaxTree, "unknown inc/dec operation %s", s.Tok)
   484  			return
   485  		}
   486  
   487  		var x operand
   488  		check.expr(nil, &x, s.X)
   489  		if x.mode == invalid {
   490  			return
   491  		}
   492  		if !allNumeric(x.typ) {
   493  			check.errorf(s.X, NonNumericIncDec, invalidOp+"%s%s (non-numeric type %s)", s.X, s.Tok, x.typ)
   494  			return
   495  		}
   496  
   497  		Y := &ast.BasicLit{ValuePos: s.X.Pos(), Kind: token.INT, Value: "1"} // use x's position
   498  		check.binary(&x, nil, s.X, Y, op, s.TokPos)
   499  		if x.mode == invalid {
   500  			return
   501  		}
   502  		check.assignVar(s.X, nil, &x, "assignment")
   503  
   504  	case *ast.AssignStmt:
   505  		switch s.Tok {
   506  		case token.ASSIGN, token.DEFINE:
   507  			if len(s.Lhs) == 0 {
   508  				check.error(s, InvalidSyntaxTree, "missing lhs in assignment")
   509  				return
   510  			}
   511  			if s.Tok == token.DEFINE {
   512  				check.shortVarDecl(inNode(s, s.TokPos), s.Lhs, s.Rhs)
   513  			} else {
   514  				// regular assignment
   515  				check.assignVars(s.Lhs, s.Rhs)
   516  			}
   517  
   518  		default:
   519  			// assignment operations
   520  			if len(s.Lhs) != 1 || len(s.Rhs) != 1 {
   521  				check.errorf(inNode(s, s.TokPos), MultiValAssignOp, "assignment operation %s requires single-valued expressions", s.Tok)
   522  				return
   523  			}
   524  			op := assignOp(s.Tok)
   525  			if op == token.ILLEGAL {
   526  				check.errorf(atPos(s.TokPos), InvalidSyntaxTree, "unknown assignment operation %s", s.Tok)
   527  				return
   528  			}
   529  			var x operand
   530  			check.binary(&x, nil, s.Lhs[0], s.Rhs[0], op, s.TokPos)
   531  			if x.mode == invalid {
   532  				return
   533  			}
   534  			check.assignVar(s.Lhs[0], nil, &x, "assignment")
   535  		}
   536  
   537  	case *ast.GoStmt:
   538  		check.suspendedCall("go", s.Call)
   539  
   540  	case *ast.DeferStmt:
   541  		check.suspendedCall("defer", s.Call)
   542  
   543  	case *ast.ReturnStmt:
   544  		res := check.sig.results
   545  		// Return with implicit results allowed for function with named results.
   546  		// (If one is named, all are named.)
   547  		if len(s.Results) == 0 && res.Len() > 0 && res.vars[0].name != "" {
   548  			// spec: "Implementation restriction: A compiler may disallow an empty expression
   549  			// list in a "return" statement if a different entity (constant, type, or variable)
   550  			// with the same name as a result parameter is in scope at the place of the return."
   551  			for _, obj := range res.vars {
   552  				if alt := check.lookup(obj.name); alt != nil && alt != obj {
   553  					err := check.newError(OutOfScopeResult)
   554  					err.addf(s, "result parameter %s not in scope at return", obj.name)
   555  					err.addf(alt, "inner declaration of %s", obj)
   556  					err.report()
   557  					// ok to continue
   558  				}
   559  			}
   560  		} else {
   561  			var lhs []*Var
   562  			if res.Len() > 0 {
   563  				lhs = res.vars
   564  			}
   565  			check.initVars(lhs, s.Results, s)
   566  		}
   567  
   568  	case *ast.BranchStmt:
   569  		if s.Label != nil {
   570  			check.hasLabel = true
   571  			return // checked in 2nd pass (check.labels)
   572  		}
   573  		switch s.Tok {
   574  		case token.BREAK:
   575  			if ctxt&breakOk == 0 {
   576  				check.error(s, MisplacedBreak, "break not in for, switch, or select statement")
   577  			}
   578  		case token.CONTINUE:
   579  			if ctxt&continueOk == 0 {
   580  				check.error(s, MisplacedContinue, "continue not in for statement")
   581  			}
   582  		case token.FALLTHROUGH:
   583  			if ctxt&fallthroughOk == 0 {
   584  				var msg string
   585  				switch {
   586  				case ctxt&finalSwitchCase != 0:
   587  					msg = "cannot fallthrough final case in switch"
   588  				case ctxt&inTypeSwitch != 0:
   589  					msg = "cannot fallthrough in type switch"
   590  				default:
   591  					msg = "fallthrough statement out of place"
   592  				}
   593  				check.error(s, MisplacedFallthrough, msg)
   594  			}
   595  		default:
   596  			check.errorf(s, InvalidSyntaxTree, "branch statement: %s", s.Tok)
   597  		}
   598  
   599  	case *ast.BlockStmt:
   600  		check.openScope(s, "block")
   601  		defer check.closeScope()
   602  
   603  		check.stmtList(inner, s.List)
   604  
   605  	case *ast.IfStmt:
   606  		check.openScope(s, "if")
   607  		defer check.closeScope()
   608  
   609  		check.simpleStmt(s.Init)
   610  		var x operand
   611  		check.expr(nil, &x, s.Cond)
   612  		if x.mode != invalid && !allBoolean(x.typ) {
   613  			check.error(s.Cond, InvalidCond, "non-boolean condition in if statement")
   614  		}
   615  		check.stmt(inner, s.Body)
   616  		// The parser produces a correct AST but if it was modified
   617  		// elsewhere the else branch may be invalid. Check again.
   618  		switch s.Else.(type) {
   619  		case nil, *ast.BadStmt:
   620  			// valid or error already reported
   621  		case *ast.IfStmt, *ast.BlockStmt:
   622  			check.stmt(inner, s.Else)
   623  		default:
   624  			check.error(s.Else, InvalidSyntaxTree, "invalid else branch in if statement")
   625  		}
   626  
   627  	case *ast.SwitchStmt:
   628  		inner |= breakOk
   629  		check.openScope(s, "switch")
   630  		defer check.closeScope()
   631  
   632  		check.simpleStmt(s.Init)
   633  		var x operand
   634  		if s.Tag != nil {
   635  			check.expr(nil, &x, s.Tag)
   636  			// By checking assignment of x to an invisible temporary
   637  			// (as a compiler would), we get all the relevant checks.
   638  			check.assignment(&x, nil, "switch expression")
   639  			if x.mode != invalid && !Comparable(x.typ) && !hasNil(x.typ) {
   640  				check.errorf(&x, InvalidExprSwitch, "cannot switch on %s (%s is not comparable)", &x, x.typ)
   641  				x.mode = invalid
   642  			}
   643  		} else {
   644  			// spec: "A missing switch expression is
   645  			// equivalent to the boolean value true."
   646  			x.mode = constant_
   647  			x.typ = Typ[Bool]
   648  			x.val = constant.MakeBool(true)
   649  			x.expr = &ast.Ident{NamePos: s.Body.Lbrace, Name: "true"}
   650  		}
   651  
   652  		check.multipleDefaults(s.Body.List)
   653  
   654  		seen := make(valueMap) // map of seen case values to positions and types
   655  		for i, c := range s.Body.List {
   656  			clause, _ := c.(*ast.CaseClause)
   657  			if clause == nil {
   658  				check.error(c, InvalidSyntaxTree, "incorrect expression switch case")
   659  				continue
   660  			}
   661  			check.caseValues(&x, clause.List, seen)
   662  			check.openScope(clause, "case")
   663  			inner := inner
   664  			if i+1 < len(s.Body.List) {
   665  				inner |= fallthroughOk
   666  			} else {
   667  				inner |= finalSwitchCase
   668  			}
   669  			check.stmtList(inner, clause.Body)
   670  			check.closeScope()
   671  		}
   672  
   673  	case *ast.TypeSwitchStmt:
   674  		inner |= breakOk | inTypeSwitch
   675  		check.openScope(s, "type switch")
   676  		defer check.closeScope()
   677  
   678  		check.simpleStmt(s.Init)
   679  
   680  		// A type switch guard must be of the form:
   681  		//
   682  		//     TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
   683  		//
   684  		// The parser is checking syntactic correctness;
   685  		// remaining syntactic errors are considered AST errors here.
   686  		// TODO(gri) better factoring of error handling (invalid ASTs)
   687  		//
   688  		var lhs *ast.Ident // lhs identifier or nil
   689  		var rhs ast.Expr
   690  		switch guard := s.Assign.(type) {
   691  		case *ast.ExprStmt:
   692  			rhs = guard.X
   693  		case *ast.AssignStmt:
   694  			if len(guard.Lhs) != 1 || guard.Tok != token.DEFINE || len(guard.Rhs) != 1 {
   695  				check.error(s, InvalidSyntaxTree, "incorrect form of type switch guard")
   696  				return
   697  			}
   698  
   699  			lhs, _ = guard.Lhs[0].(*ast.Ident)
   700  			if lhs == nil {
   701  				check.error(s, InvalidSyntaxTree, "incorrect form of type switch guard")
   702  				return
   703  			}
   704  
   705  			if lhs.Name == "_" {
   706  				// _ := x.(type) is an invalid short variable declaration
   707  				check.softErrorf(lhs, NoNewVar, "no new variable on left side of :=")
   708  				lhs = nil // avoid declared and not used error below
   709  			} else {
   710  				check.recordDef(lhs, nil) // lhs variable is implicitly declared in each cause clause
   711  			}
   712  
   713  			rhs = guard.Rhs[0]
   714  
   715  		default:
   716  			check.error(s, InvalidSyntaxTree, "incorrect form of type switch guard")
   717  			return
   718  		}
   719  
   720  		// rhs must be of the form: expr.(type) and expr must be an ordinary interface
   721  		expr, _ := rhs.(*ast.TypeAssertExpr)
   722  		if expr == nil || expr.Type != nil {
   723  			check.error(s, InvalidSyntaxTree, "incorrect form of type switch guard")
   724  			return
   725  		}
   726  
   727  		var sx *operand // switch expression against which cases are compared against; nil if invalid
   728  		{
   729  			var x operand
   730  			check.expr(nil, &x, expr.X)
   731  			if x.mode != invalid {
   732  				if isTypeParam(x.typ) {
   733  					check.errorf(&x, InvalidTypeSwitch, "cannot use type switch on type parameter value %s", &x)
   734  				} else if IsInterface(x.typ) {
   735  					sx = &x
   736  				} else {
   737  					check.errorf(&x, InvalidTypeSwitch, "%s is not an interface", &x)
   738  				}
   739  			}
   740  		}
   741  
   742  		check.multipleDefaults(s.Body.List)
   743  
   744  		var lhsVars []*Var              // list of implicitly declared lhs variables
   745  		seen := make(map[Type]ast.Expr) // map of seen types to positions
   746  		for _, s := range s.Body.List {
   747  			clause, _ := s.(*ast.CaseClause)
   748  			if clause == nil {
   749  				check.error(s, InvalidSyntaxTree, "incorrect type switch case")
   750  				continue
   751  			}
   752  			// Check each type in this type switch case.
   753  			T := check.caseTypes(sx, clause.List, seen)
   754  			check.openScope(clause, "case")
   755  			// If lhs exists, declare a corresponding variable in the case-local scope.
   756  			if lhs != nil {
   757  				obj := newVar(LocalVar, lhs.Pos(), check.pkg, lhs.Name, T)
   758  				check.declare(check.scope, nil, obj, clause.Colon)
   759  				check.recordImplicit(clause, obj)
   760  				// For the "declared and not used" error, all lhs variables act as
   761  				// one; i.e., if any one of them is 'used', all of them are 'used'.
   762  				// Collect them for later analysis.
   763  				lhsVars = append(lhsVars, obj)
   764  			}
   765  			check.stmtList(inner, clause.Body)
   766  			check.closeScope()
   767  		}
   768  
   769  		// If lhs exists, we must have at least one lhs variable that was used.
   770  		// (We can't use check.usage because that only looks at one scope; and
   771  		// we don't want to use the same variable for all scopes and change the
   772  		// variable type underfoot.)
   773  		if lhs != nil {
   774  			var used bool
   775  			for _, v := range lhsVars {
   776  				if check.usedVars[v] {
   777  					used = true
   778  				}
   779  				check.usedVars[v] = true // avoid usage error when checking entire function
   780  			}
   781  			if !used {
   782  				check.softErrorf(lhs, UnusedVar, "%s declared and not used", lhs.Name)
   783  			}
   784  		}
   785  
   786  	case *ast.SelectStmt:
   787  		inner |= breakOk
   788  
   789  		check.multipleDefaults(s.Body.List)
   790  
   791  		for _, s := range s.Body.List {
   792  			clause, _ := s.(*ast.CommClause)
   793  			if clause == nil {
   794  				continue // error reported before
   795  			}
   796  
   797  			// clause.Comm must be a SendStmt, RecvStmt, or default case
   798  			valid := false
   799  			var rhs ast.Expr // rhs of RecvStmt, or nil
   800  			switch s := clause.Comm.(type) {
   801  			case nil, *ast.SendStmt:
   802  				valid = true
   803  			case *ast.AssignStmt:
   804  				if len(s.Rhs) == 1 {
   805  					rhs = s.Rhs[0]
   806  				}
   807  			case *ast.ExprStmt:
   808  				rhs = s.X
   809  			}
   810  
   811  			// if present, rhs must be a receive operation
   812  			if rhs != nil {
   813  				if x, _ := ast.Unparen(rhs).(*ast.UnaryExpr); x != nil && x.Op == token.ARROW {
   814  					valid = true
   815  				}
   816  			}
   817  
   818  			if !valid {
   819  				check.error(clause.Comm, InvalidSelectCase, "select case must be send or receive (possibly with assignment)")
   820  				continue
   821  			}
   822  
   823  			check.openScope(s, "case")
   824  			if clause.Comm != nil {
   825  				check.stmt(inner, clause.Comm)
   826  			}
   827  			check.stmtList(inner, clause.Body)
   828  			check.closeScope()
   829  		}
   830  
   831  	case *ast.ForStmt:
   832  		inner |= breakOk | continueOk
   833  		check.openScope(s, "for")
   834  		defer check.closeScope()
   835  
   836  		check.simpleStmt(s.Init)
   837  		if s.Cond != nil {
   838  			var x operand
   839  			check.expr(nil, &x, s.Cond)
   840  			if x.mode != invalid && !allBoolean(x.typ) {
   841  				check.error(s.Cond, InvalidCond, "non-boolean condition in for statement")
   842  			}
   843  		}
   844  		check.simpleStmt(s.Post)
   845  		// spec: "The init statement may be a short variable
   846  		// declaration, but the post statement must not."
   847  		if s, _ := s.Post.(*ast.AssignStmt); s != nil && s.Tok == token.DEFINE {
   848  			check.softErrorf(s, InvalidPostDecl, "cannot declare in post statement")
   849  			// Don't call useLHS here because we want to use the lhs in
   850  			// this erroneous statement so that we don't get errors about
   851  			// these lhs variables being declared and not used.
   852  			check.use(s.Lhs...) // avoid follow-up errors
   853  		}
   854  		check.stmt(inner, s.Body)
   855  
   856  	case *ast.RangeStmt:
   857  		inner |= breakOk | continueOk
   858  		check.rangeStmt(inner, s, inNode(s, s.TokPos), s.Key, s.Value, nil, s.X, s.Tok == token.DEFINE)
   859  
   860  	default:
   861  		check.error(s, InvalidSyntaxTree, "invalid statement")
   862  	}
   863  }
   864  

View as plain text