Source file
src/go/types/stmt.go
1
2
3
4
5
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
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
28
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,
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
53
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
77
78 if !scope.isFunc {
79 check.usage(scope)
80 }
81 }
82 }
83
84
85
86
87
88 type stmtContext uint
89
90 const (
91
92 breakOk stmtContext = 1 << iota
93 continueOk
94 fallthroughOk
95
96
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)
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
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
196 func goVal(val constant.Value) any {
197
198 if val == nil {
199 return nil
200 }
201
202
203
204
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
224
225
226
227
228
229 type (
230 valueMap map[any][]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
250 res := v
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
257 }
258
259 if val := goVal(v.val); val != nil {
260
261
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
277 func (check *Checker) isNil(e ast.Expr) bool {
278
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
313 if check.isNil(e) {
314 T = nil
315 check.expr(nil, &dummy, e)
316 } else {
317 T = check.varType(e)
318 if !isValid(T) {
319 continue L
320 }
321 }
322
323
324 for t, other := range seen {
325 if T == nil && t == nil || T != nil && t != nil && Identical(T, t) {
326
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
345
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
358
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
365 var hash string
366 if check.isNil(e) {
367 check.expr(nil, &dummy, e)
368 T = nil
369 hash = "<nil>"
370 } else {
371 T = check.varType(e)
372 if !isValid(T) {
373 continue L
374 }
375 panic("enable typeHash(T, nil)")
376
377 }
378
379 if other := seen[hash]; other != nil {
380
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
398
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
411 func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
412
413 if debug {
414 defer func(scope *Scope) {
415
416 if p := recover(); p != nil {
417 panic(p)
418 }
419 assert(scope == check.scope)
420 }(check.scope)
421 }
422
423
424 defer check.processDelayed(len(check.delayed))
425
426
427 inner := ctxt &^ (fallthroughOk | finalSwitchCase | inTypeSwitch)
428
429 switch s := s.(type) {
430 case *ast.BadStmt, *ast.EmptyStmt:
431
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
442
443
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"}
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
515 check.assignVars(s.Lhs, s.Rhs)
516 }
517
518 default:
519
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
546
547 if len(s.Results) == 0 && res.Len() > 0 && res.vars[0].name != "" {
548
549
550
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
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
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
617
618 switch s.Else.(type) {
619 case nil, *ast.BadStmt:
620
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
637
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
645
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)
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
681
682
683
684
685
686
687
688 var lhs *ast.Ident
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
707 check.softErrorf(lhs, NoNewVar, "no new variable on left side of :=")
708 lhs = nil
709 } else {
710 check.recordDef(lhs, nil)
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
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
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
745 seen := make(map[Type]ast.Expr)
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
753 T := check.caseTypes(sx, clause.List, seen)
754 check.openScope(clause, "case")
755
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
761
762
763 lhsVars = append(lhsVars, obj)
764 }
765 check.stmtList(inner, clause.Body)
766 check.closeScope()
767 }
768
769
770
771
772
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
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
795 }
796
797
798 valid := false
799 var rhs ast.Expr
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
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
846
847 if s, _ := s.Post.(*ast.AssignStmt); s != nil && s.Tok == token.DEFINE {
848 check.softErrorf(s, InvalidPostDecl, "cannot declare in post statement")
849
850
851
852 check.use(s.Lhs...)
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