1 // Copyright 2013 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.
5 // This file implements type-checking of identifiers and type expressions.
17 // ident type-checks identifier e and initializes x with the value or type of e.
18 // If an error occurred, x.mode is set to invalid.
19 // For the meaning of def, see Checker.definedType, below.
20 // If wantType is set, the identifier e is expected to denote a type.
22 func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool) {
26 // Note that we cannot use check.lookup here because the returned scope
27 // may be different from obj.Parent(). See also Scope.LookupParent doc.
28 scope, obj := check.scope.LookupParent(e.Name, check.pos)
31 check.errorf(e, _InvalidBlank, "cannot use _ as value or type")
33 check.errorf(e, _UndeclaredName, "undeclared name: %s", e.Name)
37 check.recordUse(e, obj)
39 // Type-check the object.
40 // Only call Checker.objDecl if the object doesn't have a type yet
41 // (in which case we must actually determine it) or the object is a
42 // TypeName and we also want a type (in which case we might detect
43 // a cycle which needs to be reported). Otherwise we can skip the
44 // call and avoid a possible cycle error in favor of the more
45 // informative "not a type/value" error that this function's caller
46 // will issue (see issue #25790).
48 if _, gotType := obj.(*TypeName); typ == nil || gotType && wantType {
49 check.objDecl(obj, def)
50 typ = obj.Type() // type must have been assigned by Checker.objDecl
54 // The object may have been dot-imported.
55 // If so, mark the respective package as used.
56 // (This code is only needed for dot-imports. Without them,
57 // we only have to mark variables, see *Var case below).
58 if pkgName := check.dotImportMap[dotImportKey{scope, obj}]; pkgName != nil {
62 switch obj := obj.(type) {
64 check.errorf(e, _InvalidPkgUse, "use of package %s not in selector", obj.name)
69 if typ == Typ[Invalid] {
72 if obj == universeIota {
73 if check.iota == nil {
74 check.errorf(e, _InvalidIota, "cannot use iota outside constant declaration")
88 // It's ok to mark non-local variables, but ignore variables
89 // from other packages to avoid potential race conditions with
90 // dot-imported variables.
91 if obj.pkg == check.pkg {
95 if typ == Typ[Invalid] {
101 check.addDeclDep(obj)
118 // typ type-checks the type expression e and returns its type, or Typ[Invalid].
119 func (check *Checker) typ(e ast.Expr) Type {
120 return check.definedType(e, nil)
123 // definedType is like typ but also accepts a type name def.
124 // If def != nil, e is the type specification for the defined type def, declared
125 // in a type declaration, and def.underlying will be set to the type of e before
126 // any components of e are type-checked.
128 func (check *Checker) definedType(e ast.Expr, def *Named) (T Type) {
130 check.trace(e.Pos(), "%s", e)
134 check.trace(e.Pos(), "=> %s", T)
138 T = check.typInternal(e, def)
140 check.recordTypeAndValue(e, typexpr, T, nil)
145 // funcType type-checks a function or method type.
146 func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast.FuncType) {
147 scope := NewScope(check.scope, token.NoPos, token.NoPos, "function")
149 check.recordScope(ftyp, scope)
151 recvList, _ := check.collectParams(scope, recvPar, false)
152 params, variadic := check.collectParams(scope, ftyp.Params, true)
153 results, _ := check.collectParams(scope, ftyp.Results, false)
156 // recv parameter list present (may be empty)
157 // spec: "The receiver is specified via an extra parameter section preceding the
158 // method name. That parameter section must declare a single parameter, the receiver."
160 switch len(recvList) {
162 check.error(recvPar, _BadRecv, "method is missing receiver")
163 recv = NewParam(0, nil, "", Typ[Invalid]) // ignore recv below
165 // more than one receiver
166 check.error(recvList[len(recvList)-1], _BadRecv, "method must have exactly one receiver")
167 fallthrough // continue with first receiver
171 // spec: "The receiver type must be of the form T or *T where T is a type name."
172 // (ignore invalid types - error was reported before)
173 if t, _ := deref(recv.typ); t != Typ[Invalid] {
175 if T, _ := t.(*Named); T != nil {
176 // spec: "The type denoted by T is called the receiver base type; it must not
177 // be a pointer or interface type and it must be declared in the same package
179 if T.obj.pkg != check.pkg {
180 err = "type not defined in this package"
182 // TODO(gri) This is not correct if the underlying type is unknown yet.
183 switch u := T.underlying.(type) {
185 // unsafe.Pointer is treated like a regular pointer
186 if u.kind == UnsafePointer {
187 err = "unsafe.Pointer"
189 case *Pointer, *Interface:
190 err = "pointer or interface type"
194 err = "basic or unnamed type"
197 check.errorf(recv, _InvalidRecv, "invalid receiver %s (%s)", recv.typ, err)
205 sig.params = NewTuple(params...)
206 sig.results = NewTuple(results...)
207 sig.variadic = variadic
210 // typInternal drives type checking of types.
211 // Must only be called by definedType.
213 func (check *Checker) typInternal(e ast.Expr, def *Named) Type {
214 switch e := e.(type) {
216 // ignore - error reported before
220 check.ident(&x, e, def, true)
225 def.setUnderlying(typ)
228 // ignore - error reported before
230 check.errorf(&x, _NotAType, "%s used as type", &x)
232 check.errorf(&x, _NotAType, "%s is not a type", &x)
235 case *ast.SelectorExpr:
237 check.selector(&x, e)
242 def.setUnderlying(typ)
245 // ignore - error reported before
247 check.errorf(&x, _NotAType, "%s used as type", &x)
249 check.errorf(&x, _NotAType, "%s is not a type", &x)
253 return check.definedType(e.X, def)
258 def.setUnderlying(typ)
259 typ.len = check.arrayLength(e.Len)
260 typ.elem = check.typ(e.Elt)
265 def.setUnderlying(typ)
266 typ.elem = check.typ(e.Elt)
270 case *ast.StructType:
272 def.setUnderlying(typ)
273 check.structType(typ, e)
278 def.setUnderlying(typ)
279 typ.base = check.typ(e.X)
283 typ := new(Signature)
284 def.setUnderlying(typ)
285 check.funcType(typ, nil, e)
288 case *ast.InterfaceType:
289 typ := new(Interface)
290 def.setUnderlying(typ)
291 check.interfaceType(typ, e, def)
296 def.setUnderlying(typ)
298 typ.key = check.typ(e.Key)
299 typ.elem = check.typ(e.Value)
301 // spec: "The comparison operators == and != must be fully defined
302 // for operands of the key type; thus the key type must not be a
303 // function, map, or slice."
305 // Delay this check because it requires fully setup types;
306 // it is safe to continue in any case (was issue 6667).
308 if !Comparable(typ.key) {
309 check.errorf(e.Key, _IncomparableMapKey, "incomparable map key type %s", typ.key)
317 def.setUnderlying(typ)
321 case ast.SEND | ast.RECV:
328 check.invalidAST(e, "unknown channel direction %d", e.Dir)
333 typ.elem = check.typ(e.Value)
337 check.errorf(e, _NotAType, "%s is not a type", e)
341 def.setUnderlying(typ)
345 // typeOrNil type-checks the type expression (or nil value) e
346 // and returns the typ of e, or nil.
347 // If e is neither a type nor nil, typOrNil returns Typ[Invalid].
349 func (check *Checker) typOrNil(e ast.Expr) Type {
351 check.rawExpr(&x, e, nil)
354 // ignore - error reported before
356 check.errorf(&x, _NotAType, "%s used as type", &x)
365 check.errorf(&x, _NotAType, "%s is not a type", &x)
370 // arrayLength type-checks the array length expression e
371 // and returns the constant length >= 0, or a value < 0
372 // to indicate an error (and thus an unknown length).
373 func (check *Checker) arrayLength(e ast.Expr) int64 {
376 if x.mode != constant_ {
377 if x.mode != invalid {
378 check.errorf(&x, _InvalidArrayLen, "array length %s must be constant", &x)
382 if isUntyped(x.typ) || isInteger(x.typ) {
383 if val := constant.ToInt(x.val); val.Kind() == constant.Int {
384 if representableConst(val, check, Typ[Int], nil) {
385 if n, ok := constant.Int64Val(val); ok && n >= 0 {
388 check.errorf(&x, _InvalidArrayLen, "invalid array length %s", &x)
393 check.errorf(&x, _InvalidArrayLen, "array length %s must be integer", &x)
397 func (check *Checker) collectParams(scope *Scope, list *ast.FieldList, variadicOk bool) (params []*Var, variadic bool) {
402 var named, anonymous bool
403 for i, field := range list.List {
405 if t, _ := ftype.(*ast.Ellipsis); t != nil {
407 if variadicOk && i == len(list.List)-1 && len(field.Names) <= 1 {
410 check.softErrorf(t, _MisplacedDotDotDot, "can only use ... with final parameter in list")
411 // ignore ... and continue
414 typ := check.typ(ftype)
415 // The parser ensures that f.Tag is nil and we don't
416 // care if a constructed AST contains a non-nil tag.
417 if len(field.Names) > 0 {
419 for _, name := range field.Names {
421 check.invalidAST(name, "anonymous parameter")
424 par := NewParam(name.Pos(), check.pkg, name.Name, typ)
425 check.declare(scope, name, par, scope.pos)
426 params = append(params, par)
430 // anonymous parameter
431 par := NewParam(ftype.Pos(), check.pkg, "", typ)
432 check.recordImplicit(field, par)
433 params = append(params, par)
438 if named && anonymous {
439 check.invalidAST(list, "list contains both named and anonymous parameters")
443 // For a variadic function, change the last parameter's type from T to []T.
444 // Since we type-checked T rather than ...T, we also need to retro-actively
445 // record the type for ...T.
447 last := params[len(params)-1]
448 last.typ = &Slice{elem: last.typ}
449 check.recordTypeAndValue(list.List[len(list.List)-1].Type, typexpr, last.typ, nil)
455 func (check *Checker) declareInSet(oset *objset, pos token.Pos, obj Object) bool {
456 if alt := oset.insert(obj); alt != nil {
457 check.errorf(atPos(pos), _DuplicateDecl, "%s redeclared", obj.Name())
458 check.reportAltDecl(alt)
464 func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, def *Named) {
465 for _, f := range iface.Methods.List {
466 if len(f.Names) > 0 {
467 // We have a method with name f.Names[0].
468 // (The parser ensures that there's only one method
469 // and we don't care if a constructed AST has more.)
471 if name.Name == "_" {
472 check.errorf(name, _BlankIfaceMethod, "invalid method name _")
476 typ := check.typ(f.Type)
477 sig, _ := typ.(*Signature)
479 if typ != Typ[Invalid] {
480 check.invalidAST(f.Type, "%s is not a method signature", typ)
485 // use named receiver type if available (for better error messages)
486 var recvTyp Type = ityp
490 sig.recv = NewVar(name.Pos(), check.pkg, "", recvTyp)
492 m := NewFunc(name.Pos(), check.pkg, name.Name, sig)
493 check.recordDef(name, m)
494 ityp.methods = append(ityp.methods, m)
496 // We have an embedded interface and f.Type is its
497 // (possibly qualified) embedded type name. Collect
498 // it if it's a valid interface.
499 typ := check.typ(f.Type)
501 utyp := check.underlying(typ)
502 if _, ok := utyp.(*Interface); !ok {
503 if utyp != Typ[Invalid] {
504 check.errorf(f.Type, _InvalidIfaceEmbed, "%s is not an interface", typ)
509 ityp.embeddeds = append(ityp.embeddeds, typ)
510 check.posMap[ityp] = append(check.posMap[ityp], f.Type.Pos())
514 if len(ityp.methods) == 0 && len(ityp.embeddeds) == 0 {
516 ityp.allMethods = markComplete
520 // sort for API stability
521 sortMethods(ityp.methods)
522 sortTypes(ityp.embeddeds)
524 check.later(func() { check.completeInterface(ityp) })
527 func (check *Checker) completeInterface(ityp *Interface) {
528 if ityp.allMethods != nil {
532 // completeInterface may be called via the LookupFieldOrMethod,
533 // MissingMethod, Identical, or IdenticalIgnoreTags external API
534 // in which case check will be nil. In this case, type-checking
535 // must be finished and all interfaces should have been completed.
537 panic("internal error: incomplete interface")
541 check.trace(token.NoPos, "complete %s", ityp)
545 check.trace(token.NoPos, "=> %s", ityp)
549 // An infinitely expanding interface (due to a cycle) is detected
550 // elsewhere (Checker.validType), so here we simply assume we only
551 // have valid interfaces. Mark the interface as complete to avoid
552 // infinite recursion if the validType check occurs later for some
554 ityp.allMethods = markComplete
556 // Methods of embedded interfaces are collected unchanged; i.e., the identity
557 // of a method I.m's Func Object of an interface I is the same as that of
558 // the method m in an interface that embeds interface I. On the other hand,
559 // if a method is embedded via multiple overlapping embedded interfaces, we
560 // don't provide a guarantee which "original m" got chosen for the embedding
561 // interface. See also issue #34421.
563 // If we don't care to provide this identity guarantee anymore, instead of
564 // reusing the original method in embeddings, we can clone the method's Func
565 // Object and give it the position of a corresponding embedded interface. Then
566 // we can get rid of the mpos map below and simply use the cloned method's
571 mpos := make(map[*Func]token.Pos) // method specification or method embedding position, for good error messages
572 addMethod := func(pos token.Pos, m *Func, explicit bool) {
573 switch other := seen.insert(m); {
575 methods = append(methods, m)
578 check.errorf(atPos(pos), _DuplicateDecl, "duplicate method %s", m.name)
579 check.errorf(atPos(mpos[other.(*Func)]), _DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented
581 // We have a duplicate method name in an embedded (not explicitly declared) method.
582 // Check method signatures after all types are computed (issue #33656).
583 // If we're pre-go1.14 (overlapping embeddings are not permitted), report that
584 // error here as well (even though we could do it eagerly) because it's the same
587 if !check.allowVersion(m.pkg, 1, 14) || !check.identical(m.typ, other.Type()) {
588 check.errorf(atPos(pos), _DuplicateDecl, "duplicate method %s", m.name)
589 check.errorf(atPos(mpos[other.(*Func)]), _DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented
595 for _, m := range ityp.methods {
596 addMethod(m.pos, m, true)
599 posList := check.posMap[ityp]
600 for i, typ := range ityp.embeddeds {
601 pos := posList[i] // embedding position
602 typ, ok := check.underlying(typ).(*Interface)
604 // An error was reported when collecting the embedded types.
608 check.completeInterface(typ)
609 for _, m := range typ.allMethods {
610 addMethod(pos, m, false) // use embedding position pos rather than m.pos
615 sort.Sort(byUniqueMethodName(methods))
616 ityp.allMethods = methods
620 func sortTypes(list []Type) {
621 sort.Stable(byUniqueTypeName(list))
624 // byUniqueTypeName named type lists can be sorted by their unique type names.
625 type byUniqueTypeName []Type
627 func (a byUniqueTypeName) Len() int { return len(a) }
628 func (a byUniqueTypeName) Less(i, j int) bool { return sortName(a[i]) < sortName(a[j]) }
629 func (a byUniqueTypeName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
631 func sortName(t Type) string {
632 if named, _ := t.(*Named); named != nil {
633 return named.obj.Id()
638 func sortMethods(list []*Func) {
639 sort.Sort(byUniqueMethodName(list))
642 func assertSortedMethods(list []*Func) {
644 panic("internal error: assertSortedMethods called outside debug mode")
646 if !sort.IsSorted(byUniqueMethodName(list)) {
647 panic("internal error: methods not sorted")
651 // byUniqueMethodName method lists can be sorted by their unique method names.
652 type byUniqueMethodName []*Func
654 func (a byUniqueMethodName) Len() int { return len(a) }
655 func (a byUniqueMethodName) Less(i, j int) bool { return a[i].Id() < a[j].Id() }
656 func (a byUniqueMethodName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
658 func (check *Checker) tag(t *ast.BasicLit) string {
660 if t.Kind == token.STRING {
661 if val, err := strconv.Unquote(t.Value); err == nil {
665 check.invalidAST(t, "incorrect tag syntax: %q", t.Value)
670 func (check *Checker) structType(styp *Struct, e *ast.StructType) {
676 // struct fields and tags
680 // for double-declaration checks
683 // current field typ and tag
686 add := func(ident *ast.Ident, embedded bool, pos token.Pos) {
687 if tag != "" && tags == nil {
688 tags = make([]string, len(fields))
691 tags = append(tags, tag)
695 fld := NewField(pos, check.pkg, name, typ, embedded)
696 // spec: "Within a struct, non-blank field names must be unique."
697 if name == "_" || check.declareInSet(&fset, pos, fld) {
698 fields = append(fields, fld)
699 check.recordDef(ident, fld)
703 // addInvalid adds an embedded field of invalid type to the struct for
704 // fields with errors; this keeps the number of struct fields in sync
705 // with the source as long as the fields are _ or have different names
707 addInvalid := func(ident *ast.Ident, pos token.Pos) {
710 add(ident, true, pos)
713 for _, f := range list.List {
714 typ = check.typ(f.Type)
715 tag = check.tag(f.Tag)
716 if len(f.Names) > 0 {
718 for _, name := range f.Names {
719 add(name, false, name.Pos())
723 // spec: "An embedded type must be specified as a type name T or as a pointer
724 // to a non-interface type name *T, and T itself may not be a pointer type."
726 name := embeddedFieldIdent(f.Type)
728 check.invalidAST(f.Type, "embedded field type %s has no name", f.Type)
729 name = ast.NewIdent("_")
731 addInvalid(name, pos)
734 t, isPtr := deref(typ)
735 // Because we have a name, typ must be of the form T or *T, where T is the name
736 // of a (named or alias) type, and t (= deref(typ)) must be the type of T.
737 switch t := t.Underlying().(type) {
739 if t == Typ[Invalid] {
740 // error was reported before
741 addInvalid(name, pos)
745 // unsafe.Pointer is treated like a regular pointer
746 if t.kind == UnsafePointer {
747 check.errorf(f.Type, _InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
748 addInvalid(name, pos)
753 check.errorf(f.Type, _InvalidPtrEmbed, "embedded field type cannot be a pointer")
754 addInvalid(name, pos)
759 check.errorf(f.Type, _InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
760 addInvalid(name, pos)
772 func embeddedFieldIdent(e ast.Expr) *ast.Ident {
773 switch e := e.(type) {
777 // *T is valid, but **T is not
778 if _, ok := e.X.(*ast.StarExpr); !ok {
779 return embeddedFieldIdent(e.X)
781 case *ast.SelectorExpr:
784 return nil // invalid embedded field