1 // Copyright 2014 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.
12 . "internal/types/errors"
15 func (check *Checker) reportAltDecl(obj Object) {
16 if pos := obj.Pos(); pos.IsValid() {
17 // We use "other" rather than "previous" here because
18 // the first declaration seen may not be textually
19 // earlier in the source.
20 check.errorf(obj, DuplicateDecl, "\tother declaration of %s", obj.Name()) // secondary error, \t indented
24 func (check *Checker) declare(scope *Scope, id *ast.Ident, obj Object, pos token.Pos) {
25 // spec: "The blank identifier, represented by the underscore
26 // character _, may be used in a declaration like any other
27 // identifier but the declaration does not introduce a new
29 if obj.Name() != "_" {
30 if alt := scope.Insert(obj); alt != nil {
31 check.errorf(obj, DuplicateDecl, "%s redeclared in this block", obj.Name())
32 check.reportAltDecl(alt)
38 check.recordDef(id, obj)
42 // pathString returns a string of the form a->b-> ... ->g for a path [a, b, ... g].
43 func pathString(path []Object) string {
45 for i, p := range path {
54 // objDecl type-checks the declaration of obj in its respective (file) environment.
55 // For the meaning of def, see Checker.definedType, in typexpr.go.
56 func (check *Checker) objDecl(obj Object, def *TypeName) {
57 if check.conf._Trace && obj.Type() == nil {
58 if check.indent == 0 {
59 fmt.Println() // empty line between top-level objects for readability
61 check.trace(obj.Pos(), "-- checking %s (%s, objPath = %s)", obj, obj.color(), pathString(check.objPath))
65 check.trace(obj.Pos(), "=> %s (%s)", obj, obj.color())
69 // Checking the declaration of obj means inferring its type
70 // (and possibly its value, for constants).
71 // An object's type (and thus the object) may be in one of
72 // three states which are expressed by colors:
74 // - an object whose type is not yet known is painted white (initial color)
75 // - an object whose type is in the process of being inferred is painted grey
76 // - an object whose type is fully inferred is painted black
78 // During type inference, an object's color changes from white to grey
79 // to black (pre-declared objects are painted black from the start).
80 // A black object (i.e., its type) can only depend on (refer to) other black
81 // ones. White and grey objects may depend on white and black objects.
82 // A dependency on a grey object indicates a cycle which may or may not be
85 // When objects turn grey, they are pushed on the object path (a stack);
86 // they are popped again when they turn black. Thus, if a grey object (a
87 // cycle) is encountered, it is on the object path, and all the objects
88 // it depends on are the remaining objects on that path. Color encoding
89 // is such that the color value of a grey object indicates the index of
90 // that object in the object path.
92 // During type-checking, white objects may be assigned a type without
93 // traversing through objDecl; e.g., when initializing constants and
94 // variables. Update the colors of those objects here (rather than
95 // everywhere where we set the type) to satisfy the color invariants.
96 if obj.color() == white && obj.Type() != nil {
103 assert(obj.Type() == nil)
104 // All color values other than white and black are considered grey.
105 // Because black and white are < grey, all values >= grey are grey.
106 // Use those values to encode the object's index into the object path.
107 obj.setColor(grey + color(check.push(obj)))
109 check.pop().setColor(black)
113 assert(obj.Type() != nil)
117 // Color values other than white or black are considered grey.
121 // We have a (possibly invalid) cycle.
122 // In the existing code, this is marked by a non-nil type
123 // for the object except for constants and variables whose
124 // type may be non-nil (known), or nil if it depends on the
125 // not-yet known initialization value.
126 // In the former case, set the type to Typ[Invalid] because
127 // we have an initialization cycle. The cycle error will be
128 // reported later, when determining initialization order.
129 // TODO(gri) Report cycle here and simplify initialization
131 switch obj := obj.(type) {
133 if !check.validCycle(obj) || obj.typ == nil {
134 obj.typ = Typ[Invalid]
138 if !check.validCycle(obj) || obj.typ == nil {
139 obj.typ = Typ[Invalid]
143 if !check.validCycle(obj) {
145 // (without this, calling underlying()
146 // below may lead to an endless loop
147 // if we have a cycle for a defined
149 obj.typ = Typ[Invalid]
153 if !check.validCycle(obj) {
154 // Don't set obj.typ to Typ[Invalid] here
155 // because plenty of code type-asserts that
156 // functions have a *Signature type. Grey
157 // functions have their type set to an empty
158 // signature which makes it impossible to
159 // initialize a variable with the function.
165 assert(obj.Type() != nil)
169 d := check.objMap[obj]
171 check.dump("%v: %s should have been declared", obj.Pos(), obj)
175 // save/restore current environment and set up object environment
176 defer func(env environment) {
177 check.environment = env
179 check.environment = environment{
183 // Const and var declarations must not have initialization
184 // cycles. We track them by remembering the current declaration
185 // in check.decl. Initialization expressions depending on other
186 // consts, vars, or functions, add dependencies to the current
188 switch obj := obj.(type) {
190 check.decl = d // new package-level const decl
191 check.constDecl(obj, d.vtyp, d.init, d.inherited)
193 check.decl = d // new package-level var decl
194 check.varDecl(obj, d.lhs, d.vtyp, d.init)
196 // invalid recursive types are detected via path
197 check.typeDecl(obj, d.tdecl, def)
198 check.collectMethods(obj) // methods can only be added to top-level types
200 // functions may be recursive - no need to track dependencies
201 check.funcDecl(obj, d)
207 // validCycle checks if the cycle starting with obj is valid and
208 // reports an error if it is not.
209 func (check *Checker) validCycle(obj Object) (valid bool) {
210 // The object map contains the package scope objects and the non-interface methods.
212 info := check.objMap[obj]
213 inObjMap := info != nil && (info.fdecl == nil || info.fdecl.Recv == nil) // exclude methods
214 isPkgObj := obj.Parent() == check.pkg.scope
215 if isPkgObj != inObjMap {
216 check.dump("%v: inconsistent object map for %s (isPkgObj = %v, inObjMap = %v)", obj.Pos(), obj, isPkgObj, inObjMap)
221 // Count cycle objects.
222 assert(obj.color() >= grey)
223 start := obj.color() - grey // index of obj in objPath
224 cycle := check.objPath[start:]
225 tparCycle := false // if set, the cycle is through a type parameter list
226 nval := 0 // number of (constant or variable) values in the cycle; valid if !generic
227 ndef := 0 // number of type definitions in the cycle; valid if !generic
229 for _, obj := range cycle {
230 switch obj := obj.(type) {
234 // If we reach a generic type that is part of a cycle
235 // and we are in a type parameter list, we have a cycle
236 // through a type parameter list, which is invalid.
237 if check.inTParamList && isGeneric(obj.typ) {
242 // Determine if the type name is an alias or not. For
243 // package-level objects, use the object map which
244 // provides syntactic information (which doesn't rely
245 // on the order in which the objects are set up). For
246 // local objects, we can rely on the order, so use
247 // the object's predicate.
248 // TODO(gri) It would be less fragile to always access
249 // the syntactic information. We should consider storing
250 // this information explicitly in the object.
252 if check.conf._EnableAlias {
253 alias = obj.IsAlias()
255 if d := check.objMap[obj]; d != nil {
256 alias = d.tdecl.Assign.IsValid() // package-level object
258 alias = obj.IsAlias() // function local object
271 if check.conf._Trace {
272 check.trace(obj.Pos(), "## cycle detected: objPath = %s->%s (len = %d)", pathString(cycle), obj.Name(), len(cycle))
274 check.trace(obj.Pos(), "## cycle contains: generic type in a type parameter list")
276 check.trace(obj.Pos(), "## cycle contains: %d values, %d type definitions", nval, ndef)
280 check.trace(obj.Pos(), "=> cycle is valid")
282 check.trace(obj.Pos(), "=> error: cycle is invalid")
288 // A cycle involving only constants and variables is invalid but we
289 // ignore them here because they are reported via the initialization
291 if nval == len(cycle) {
295 // A cycle involving only types (and possibly functions) must have at least
296 // one type definition to be permitted: If there is no type definition, we
297 // have a sequence of alias type names which will expand ad infinitum.
298 if nval == 0 && ndef > 0 {
303 check.cycleError(cycle)
307 // cycleError reports a declaration cycle starting with
308 // the object in cycle that is "first" in the source.
309 func (check *Checker) cycleError(cycle []Object) {
310 // name returns the (possibly qualified) object name.
311 // This is needed because with generic types, cycles
312 // may refer to imported types. See go.dev/issue/50788.
313 // TODO(gri) Thus functionality is used elsewhere. Factor it out.
314 name := func(obj Object) string {
315 return packagePrefix(obj.Pkg(), check.qualifier) + obj.Name()
318 // TODO(gri) Should we start with the last (rather than the first) object in the cycle
319 // since that is the earliest point in the source where we start seeing the
320 // cycle? That would be more consistent with other error messages.
321 i := firstInSrc(cycle)
324 // If obj is a type alias, mark it as valid (not broken) in order to avoid follow-on errors.
325 tname, _ := obj.(*TypeName)
326 if tname != nil && tname.IsAlias() {
327 // If we use Alias nodes, it is initialized with Typ[Invalid].
328 // TODO(gri) Adjust this code if we initialize with nil.
329 if !check.conf._EnableAlias {
330 check.validAlias(tname, Typ[Invalid])
334 // report a more concise error for self references
337 check.errorf(obj, InvalidDeclCycle, "invalid recursive type: %s refers to itself", objName)
339 check.errorf(obj, InvalidDeclCycle, "invalid cycle in declaration: %s refers to itself", objName)
345 check.errorf(obj, InvalidDeclCycle, "invalid recursive type %s", objName)
347 check.errorf(obj, InvalidDeclCycle, "invalid cycle in declaration of %s", objName)
350 check.errorf(obj, InvalidDeclCycle, "\t%s refers to", objName) // secondary error, \t indented
358 check.errorf(obj, InvalidDeclCycle, "\t%s", objName)
361 // firstInSrc reports the index of the object with the "smallest"
362 // source position in path. path must not be empty.
363 func firstInSrc(path []Object) int {
364 fst, pos := 0, path[0].Pos()
365 for i, t := range path[1:] {
366 if cmpPos(t.Pos(), pos) < 0 {
367 fst, pos = i+1, t.Pos()
378 importDecl struct{ spec *ast.ImportSpec }
386 varDecl struct{ spec *ast.ValueSpec }
387 typeDecl struct{ spec *ast.TypeSpec }
388 funcDecl struct{ decl *ast.FuncDecl }
391 func (d importDecl) node() ast.Node { return d.spec }
392 func (d constDecl) node() ast.Node { return d.spec }
393 func (d varDecl) node() ast.Node { return d.spec }
394 func (d typeDecl) node() ast.Node { return d.spec }
395 func (d funcDecl) node() ast.Node { return d.decl }
397 func (check *Checker) walkDecls(decls []ast.Decl, f func(decl)) {
398 for _, d := range decls {
403 func (check *Checker) walkDecl(d ast.Decl, f func(decl)) {
404 switch d := d.(type) {
408 var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
409 for iota, s := range d.Specs {
410 switch s := s.(type) {
411 case *ast.ImportSpec:
416 // determine which initialization expressions to use
419 case s.Type != nil || len(s.Values) > 0:
423 last = new(ast.ValueSpec) // make sure last exists
426 check.arityMatch(s, last)
427 f(constDecl{spec: s, iota: iota, typ: last.Type, init: last.Values, inherited: inherited})
429 check.arityMatch(s, nil)
432 check.errorf(s, InvalidSyntaxTree, "invalid token %s", d.Tok)
437 check.errorf(s, InvalidSyntaxTree, "unknown ast.Spec node %T", s)
443 check.errorf(d, InvalidSyntaxTree, "unknown ast.Decl node %T", d)
447 func (check *Checker) constDecl(obj *Const, typ, init ast.Expr, inherited bool) {
448 assert(obj.typ == nil)
450 // use the correct value of iota
451 defer func(iota constant.Value, errpos positioner) {
453 check.errpos = errpos
454 }(check.iota, check.errpos)
458 // provide valid constant value under all circumstances
459 obj.val = constant.MakeUnknown()
461 // determine type, if any
465 // don't report an error if the type is an invalid C (defined) type
466 // (go.dev/issue/22090)
467 if isValid(under(t)) {
468 check.errorf(typ, InvalidConstType, "invalid constant type %s", t)
470 obj.typ = Typ[Invalid]
476 // check initialization
480 // The initialization expression is inherited from a previous
481 // constant declaration, and (error) positions refer to that
482 // expression and not the current constant declaration. Use
483 // the constant identifier position for any errors during
484 // init expression evaluation since that is all we have
485 // (see issues go.dev/issue/42991, go.dev/issue/42992).
486 check.errpos = atPos(obj.pos)
488 check.expr(nil, &x, init)
490 check.initConst(obj, &x)
493 func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
494 assert(obj.typ == nil)
496 // determine type, if any
498 obj.typ = check.varType(typ)
499 // We cannot spread the type to all lhs variables if there
500 // are more than one since that would mark them as checked
501 // (see Checker.objDecl) and the assignment of init exprs,
502 // if any, would not be checked.
504 // TODO(gri) If we have no init expr, we should distribute
505 // a given type otherwise we need to re-evalate the type
506 // expr for each lhs variable, leading to duplicate work.
509 // check initialization
512 // error reported before by arityMatch
513 obj.typ = Typ[Invalid]
518 if lhs == nil || len(lhs) == 1 {
519 assert(lhs == nil || lhs[0] == obj)
521 check.expr(obj.typ, &x, init)
522 check.initVar(obj, &x, "variable declaration")
527 // obj must be one of lhs
529 for _, lhs := range lhs {
536 panic("inconsistent lhs")
540 // We have multiple variables on the lhs and one init expr.
541 // Make sure all variables have been given the same type if
542 // one was specified, otherwise they assume the type of the
543 // init expression values (was go.dev/issue/15755).
545 for _, lhs := range lhs {
550 check.initVars(lhs, []ast.Expr{init}, nil)
553 // isImportedConstraint reports whether typ is an imported type constraint.
554 func (check *Checker) isImportedConstraint(typ Type) bool {
555 named := asNamed(typ)
556 if named == nil || named.obj.pkg == check.pkg || named.obj.pkg == nil {
559 u, _ := named.under().(*Interface)
560 return u != nil && !u.IsMethodSet()
563 func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName) {
564 assert(obj.typ == nil)
568 if t := asNamed(obj.typ); t != nil { // type may be invalid
571 // If typ is local, an error was already reported where typ is specified/defined.
572 _ = check.isImportedConstraint(rhs) && check.verifyVersionf(tdecl.Type, go1_18, "using type constraint %s", rhs)
573 }).describef(obj, "validType(%s)", obj.Name())
575 aliasDecl := tdecl.Assign.IsValid()
576 if aliasDecl && tdecl.TypeParams.NumFields() != 0 {
577 // The parser will ensure this but we may still get an invalid AST.
578 // Complain and continue as regular type definition.
579 check.error(atPos(tdecl.Assign), BadDecl, "generic type cannot be alias")
585 check.verifyVersionf(atPos(tdecl.Assign), go1_9, "type aliases")
586 if check.conf._EnableAlias {
587 // TODO(gri) Should be able to use nil instead of Typ[Invalid] to mark
588 // the alias as incomplete. Currently this causes problems
589 // with certain cycles. Investigate.
590 alias := check.newAlias(obj, Typ[Invalid])
591 setDefType(def, alias)
592 rhs = check.definedType(tdecl.Type, obj)
595 _Unalias(alias) // resolve alias.actual
597 check.brokenAlias(obj)
598 rhs = check.typ(tdecl.Type)
599 check.validAlias(obj, rhs)
604 // type definition or generic type declaration
605 named := check.newNamed(obj, nil, nil)
606 setDefType(def, named)
608 if tdecl.TypeParams != nil {
609 check.openScope(tdecl, "type parameters")
610 defer check.closeScope()
611 check.collectTypeParams(&named.tparams, tdecl.TypeParams)
614 // determine underlying type of named
615 rhs = check.definedType(tdecl.Type, obj)
619 // If the underlying type was not set while type-checking the right-hand
620 // side, it is invalid and an error should have been reported elsewhere.
621 if named.underlying == nil {
622 named.underlying = Typ[Invalid]
625 // Disallow a lone type parameter as the RHS of a type declaration (go.dev/issue/45639).
626 // We don't need this restriction anymore if we make the underlying type of a type
627 // parameter its constraint interface: if the RHS is a lone type parameter, we will
628 // use its underlying type (like we do for any RHS in a type declaration), and its
629 // underlying type is an interface and the type declaration is well defined.
630 if isTypeParam(rhs) {
631 check.error(tdecl.Type, MisplacedTypeParam, "cannot use a type parameter as RHS in type declaration")
632 named.underlying = Typ[Invalid]
636 func (check *Checker) collectTypeParams(dst **TypeParamList, list *ast.FieldList) {
637 var tparams []*TypeParam
638 // Declare type parameters up-front, with empty interface as type bound.
639 // The scope of type parameters starts at the beginning of the type parameter
640 // list (so we can have mutually recursive parameterized interfaces).
641 for _, f := range list.List {
642 tparams = check.declareTypeParams(tparams, f.Names)
645 // Set the type parameters before collecting the type constraints because
646 // the parameterized type may be used by the constraints (go.dev/issue/47887).
647 // Example: type T[P T[P]] interface{}
648 *dst = bindTParams(tparams)
650 // Signal to cycle detection that we are in a type parameter list.
651 // We can only be inside one type parameter list at any given time:
652 // function closures may appear inside a type parameter list but they
653 // cannot be generic, and their bodies are processed in delayed and
654 // sequential fashion. Note that with each new declaration, we save
655 // the existing environment and restore it when done; thus inTPList is
656 // true exactly only when we are in a specific type parameter list.
657 assert(!check.inTParamList)
658 check.inTParamList = true
660 check.inTParamList = false
664 for _, f := range list.List {
666 // NOTE: we may be able to assert that f.Type != nil here, but this is not
667 // an invariant of the AST, so we are cautious.
669 bound = check.bound(f.Type)
670 if isTypeParam(bound) {
671 // We may be able to allow this since it is now well-defined what
672 // the underlying type and thus type set of a type parameter is.
673 // But we may need some additional form of cycle detection within
674 // type parameter lists.
675 check.error(f.Type, MisplacedTypeParam, "cannot use a type parameter as constraint")
681 for i := range f.Names {
682 tparams[index+i].bound = bound
684 index += len(f.Names)
688 func (check *Checker) bound(x ast.Expr) Type {
689 // A type set literal of the form ~T and A|B may only appear as constraint;
690 // embed it in an implicit interface so that only interface type-checking
691 // needs to take care of such type expressions.
693 switch op := x.(type) {
695 wrap = op.Op == token.TILDE
696 case *ast.BinaryExpr:
697 wrap = op.Op == token.OR
700 x = &ast.InterfaceType{Methods: &ast.FieldList{List: []*ast.Field{{Type: x}}}}
702 // mark t as implicit interface if all went well
703 if t, _ := t.(*Interface); t != nil {
711 func (check *Checker) declareTypeParams(tparams []*TypeParam, names []*ast.Ident) []*TypeParam {
712 // Use Typ[Invalid] for the type constraint to ensure that a type
713 // is present even if the actual constraint has not been assigned
715 // TODO(gri) Need to systematically review all uses of type parameter
716 // constraints to make sure we don't rely on them if they
717 // are not properly set yet.
718 for _, name := range names {
719 tname := NewTypeName(name.Pos(), check.pkg, name.Name, nil)
720 tpar := check.newTypeParam(tname, Typ[Invalid]) // assigns type to tpar as a side-effect
721 check.declare(check.scope, name, tname, check.scope.pos) // TODO(gri) check scope position
722 tparams = append(tparams, tpar)
725 if check.conf._Trace && len(names) > 0 {
726 check.trace(names[0].Pos(), "type params = %v", tparams[len(tparams)-len(names):])
732 func (check *Checker) collectMethods(obj *TypeName) {
733 // get associated methods
734 // (Checker.collectObjects only collects methods with non-blank names;
735 // Checker.resolveBaseTypeName ensures that obj is not an alias name
736 // if it has attached methods.)
737 methods := check.methods[obj]
741 delete(check.methods, obj)
742 assert(!check.objMap[obj].tdecl.Assign.IsValid()) // don't use TypeName.IsAlias (requires fully set up object)
744 // use an objset to check for name conflicts
747 // spec: "If the base type is a struct type, the non-blank method
748 // and field names must be distinct."
749 base := asNamed(obj.typ) // shouldn't fail but be conservative
751 assert(base.TypeArgs().Len() == 0) // collectMethods should not be called on an instantiated type
753 // See go.dev/issue/52529: we must delay the expansion of underlying here, as
754 // base may not be fully set-up.
756 check.checkFieldUniqueness(base)
757 }).describef(obj, "verifying field uniqueness for %v", base)
759 // Checker.Files may be called multiple times; additional package files
760 // may add methods to already type-checked types. Add pre-existing methods
761 // so that we can detect redeclarations.
762 for i := 0; i < base.NumMethods(); i++ {
764 assert(m.name != "_")
765 assert(mset.insert(m) == nil)
770 for _, m := range methods {
771 // spec: "For a base type, the non-blank names of methods bound
772 // to it must be unique."
773 assert(m.name != "_")
774 if alt := mset.insert(m); alt != nil {
775 if alt.Pos().IsValid() {
776 check.errorf(m, DuplicateMethod, "method %s.%s already declared at %s", obj.Name(), m.name, alt.Pos())
778 check.errorf(m, DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
789 func (check *Checker) checkFieldUniqueness(base *Named) {
790 if t, _ := base.under().(*Struct); t != nil {
792 for i := 0; i < base.NumMethods(); i++ {
794 assert(m.name != "_")
795 assert(mset.insert(m) == nil)
798 // Check that any non-blank field names of base are distinct from its
800 for _, fld := range t.fields {
802 if alt := mset.insert(fld); alt != nil {
803 // Struct fields should already be unique, so we should only
804 // encounter an alternate via collision with a method name.
807 // For historical consistency, we report the primary error on the
808 // method, and the alt decl on the field.
809 check.errorf(alt, DuplicateFieldAndMethod, "field and method with the same name %s", fld.name)
810 check.reportAltDecl(fld)
817 func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
818 assert(obj.typ == nil)
820 // func declarations cannot use iota
821 assert(check.iota == nil)
823 sig := new(Signature)
824 obj.typ = sig // guard against cycles
826 // Avoid cycle error when referring to method while type-checking the signature.
827 // This avoids a nuisance in the best case (non-parameterized receiver type) and
828 // since the method is not a type, we get an error. If we have a parameterized
829 // receiver type, instantiating the receiver type leads to the instantiation of
830 // its methods, and we don't want a cycle error in that case.
831 // TODO(gri) review if this is correct and/or whether we still need this?
835 check.funcType(sig, fdecl.Recv, fdecl.Type)
838 if fdecl.Type.TypeParams.NumFields() > 0 && fdecl.Body == nil {
839 check.softErrorf(fdecl.Name, BadDecl, "generic function is missing function body")
842 // function body must be type-checked after global declarations
843 // (functions implemented elsewhere have no body)
844 if !check.conf.IgnoreFuncBodies && fdecl.Body != nil {
846 check.funcBody(decl, obj.name, sig, fdecl.Body, nil)
847 }).describef(obj, "func %s", obj.name)
851 func (check *Checker) declStmt(d ast.Decl) {
854 check.walkDecl(d, func(d decl) {
855 switch d := d.(type) {
857 top := len(check.delayed)
859 // declare all constants
860 lhs := make([]*Const, len(d.spec.Names))
861 for i, name := range d.spec.Names {
862 obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(d.iota)))
870 check.constDecl(obj, d.typ, init, d.inherited)
873 // process function literals in init expressions before scope changes
874 check.processDelayed(top)
876 // spec: "The scope of a constant or variable identifier declared
877 // inside a function begins at the end of the ConstSpec or VarSpec
878 // (ShortVarDecl for short variable declarations) and ends at the
879 // end of the innermost containing block."
880 scopePos := d.spec.End()
881 for i, name := range d.spec.Names {
882 check.declare(check.scope, name, lhs[i], scopePos)
886 top := len(check.delayed)
888 lhs0 := make([]*Var, len(d.spec.Names))
889 for i, name := range d.spec.Names {
890 lhs0[i] = NewVar(name.Pos(), pkg, name.Name, nil)
893 // initialize all variables
894 for i, obj := range lhs0 {
897 switch len(d.spec.Values) {
898 case len(d.spec.Names):
900 init = d.spec.Values[i]
902 // rhs is expected to be a multi-valued expression
904 init = d.spec.Values[0]
906 if i < len(d.spec.Values) {
907 init = d.spec.Values[i]
910 check.varDecl(obj, lhs, d.spec.Type, init)
911 if len(d.spec.Values) == 1 {
912 // If we have a single lhs variable we are done either way.
913 // If we have a single rhs expression, it must be a multi-
914 // valued expression, in which case handling the first lhs
915 // variable will cause all lhs variables to have a type
916 // assigned, and we are done as well.
918 for _, obj := range lhs0 {
919 assert(obj.typ != nil)
926 // process function literals in init expressions before scope changes
927 check.processDelayed(top)
929 // declare all variables
930 // (only at this point are the variable scopes (parents) set)
931 scopePos := d.spec.End() // see constant declarations
932 for i, name := range d.spec.Names {
933 // see constant declarations
934 check.declare(check.scope, name, lhs0[i], scopePos)
938 obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
939 // spec: "The scope of a type identifier declared inside a function
940 // begins at the identifier in the TypeSpec and ends at the end of
941 // the innermost containing block."
942 scopePos := d.spec.Name.Pos()
943 check.declare(check.scope, d.spec.Name, obj, scopePos)
944 // mark and unmark type before calling typeDecl; its type is still nil (see Checker.objDecl)
945 obj.setColor(grey + color(check.push(obj)))
946 check.typeDecl(obj, d.spec, nil)
947 check.pop().setColor(black)
949 check.errorf(d.node(), InvalidSyntaxTree, "unknown ast.Decl node %T", d.node())