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