packageObjects typechecks all package objects, but not function bodies.
()
| 654 | |
| 655 | // packageObjects typechecks all package objects, but not function bodies. |
| 656 | func (check *Checker) packageObjects() { |
| 657 | // process package objects in source order for reproducible results |
| 658 | objList := make([]Object, len(check.objMap)) |
| 659 | i := 0 |
| 660 | for obj := range check.objMap { |
| 661 | objList[i] = obj |
| 662 | i++ |
| 663 | } |
| 664 | sort.Sort(inSourceOrder(objList)) |
| 665 | |
| 666 | // add new methods to already type-checked types (from a prior Checker.Files call) |
| 667 | for _, obj := range objList { |
| 668 | if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil { |
| 669 | check.collectMethods(obj) |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // We process non-alias type declarations first, followed by alias declarations, |
| 674 | // and then everything else. This appears to avoid most situations where the type |
| 675 | // of an alias is needed before it is available. |
| 676 | // There may still be cases where this is not good enough (see also issue #25838). |
| 677 | // In those cases Checker.ident will report an error ("invalid use of type alias"). |
| 678 | var aliasList []*TypeName |
| 679 | var othersList []Object // everything that's not a type |
| 680 | // phase 1: non-alias type declarations |
| 681 | for _, obj := range objList { |
| 682 | if tname, _ := obj.(*TypeName); tname != nil { |
| 683 | if check.objMap[tname].tdecl.Alias { |
| 684 | aliasList = append(aliasList, tname) |
| 685 | } else { |
| 686 | check.objDecl(obj, nil) |
| 687 | } |
| 688 | } else { |
| 689 | othersList = append(othersList, obj) |
| 690 | } |
| 691 | } |
| 692 | // phase 2: alias type declarations |
| 693 | for _, obj := range aliasList { |
| 694 | check.objDecl(obj, nil) |
| 695 | } |
| 696 | // phase 3: all other declarations |
| 697 | for _, obj := range othersList { |
| 698 | check.objDecl(obj, nil) |
| 699 | } |
| 700 | |
| 701 | // At this point we may have a non-empty check.methods map; this means that not all |
| 702 | // entries were deleted at the end of typeDecl because the respective receiver base |
| 703 | // types were not found. In that case, an error was reported when declaring those |
| 704 | // methods. We can now safely discard this map. |
| 705 | check.methods = nil |
| 706 | } |
| 707 | |
| 708 | // inSourceOrder implements the sort.Sort interface. |
| 709 | type inSourceOrder []Object |
no test coverage detected