...
Run Format

Source file src/go/types/typexpr.go

Documentation: go/types

     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.
     4  
     5  // This file implements type-checking of identifiers and type expressions.
     6  
     7  package types
     8  
     9  import (
    10  	"go/ast"
    11  	"go/constant"
    12  	"go/token"
    13  	"sort"
    14  	"strconv"
    15  )
    16  
    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 and path, see check.typ, below.
    20  //
    21  func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, path []*TypeName) {
    22  	x.mode = invalid
    23  	x.expr = e
    24  
    25  	// Note that we cannot use check.lookup here because the returned scope
    26  	// may be different from obj.Parent(). See also Scope.LookupParent doc.
    27  	scope, obj := check.scope.LookupParent(e.Name, check.pos)
    28  	if obj == nil {
    29  		if e.Name == "_" {
    30  			check.errorf(e.Pos(), "cannot use _ as value or type")
    31  		} else {
    32  			check.errorf(e.Pos(), "undeclared name: %s", e.Name)
    33  		}
    34  		return
    35  	}
    36  	check.recordUse(e, obj)
    37  
    38  	check.objDecl(obj, def, path)
    39  	typ := obj.Type()
    40  	assert(typ != nil)
    41  
    42  	// The object may be dot-imported: If so, remove its package from
    43  	// the map of unused dot imports for the respective file scope.
    44  	// (This code is only needed for dot-imports. Without them,
    45  	// we only have to mark variables, see *Var case below).
    46  	if pkg := obj.Pkg(); pkg != check.pkg && pkg != nil {
    47  		delete(check.unusedDotImports[scope], pkg)
    48  	}
    49  
    50  	switch obj := obj.(type) {
    51  	case *PkgName:
    52  		check.errorf(e.Pos(), "use of package %s not in selector", obj.name)
    53  		return
    54  
    55  	case *Const:
    56  		check.addDeclDep(obj)
    57  		if typ == Typ[Invalid] {
    58  			return
    59  		}
    60  		if obj == universeIota {
    61  			if check.iota == nil {
    62  				check.errorf(e.Pos(), "cannot use iota outside constant declaration")
    63  				return
    64  			}
    65  			x.val = check.iota
    66  		} else {
    67  			x.val = obj.val
    68  		}
    69  		assert(x.val != nil)
    70  		x.mode = constant_
    71  
    72  	case *TypeName:
    73  		x.mode = typexpr
    74  		// package-level alias cycles are now checked by Checker.objDecl
    75  		if useCycleMarking {
    76  			if check.objMap[obj] != nil {
    77  				break
    78  			}
    79  		}
    80  		if check.cycle(obj, path, true) {
    81  			// maintain x.mode == typexpr despite error
    82  			typ = Typ[Invalid]
    83  			break
    84  		}
    85  
    86  	case *Var:
    87  		// It's ok to mark non-local variables, but ignore variables
    88  		// from other packages to avoid potential race conditions with
    89  		// dot-imported variables.
    90  		if obj.pkg == check.pkg {
    91  			obj.used = true
    92  		}
    93  		check.addDeclDep(obj)
    94  		if typ == Typ[Invalid] {
    95  			return
    96  		}
    97  		x.mode = variable
    98  
    99  	case *Func:
   100  		check.addDeclDep(obj)
   101  		x.mode = value
   102  
   103  	case *Builtin:
   104  		x.id = obj.id
   105  		x.mode = builtin
   106  
   107  	case *Nil:
   108  		x.mode = value
   109  
   110  	default:
   111  		unreachable()
   112  	}
   113  
   114  	x.typ = typ
   115  }
   116  
   117  // cycle reports whether obj appears in path or not.
   118  // If it does, and report is set, it also reports a cycle error.
   119  func (check *Checker) cycle(obj *TypeName, path []*TypeName, report bool) bool {
   120  	// (it's ok to iterate forward because each named type appears at most once in path)
   121  	for i, prev := range path {
   122  		if prev == obj {
   123  			if report {
   124  				check.errorf(obj.pos, "illegal cycle in declaration of %s", obj.name)
   125  				// print cycle
   126  				for _, obj := range path[i:] {
   127  					check.errorf(obj.Pos(), "\t%s refers to", obj.Name()) // secondary error, \t indented
   128  				}
   129  				check.errorf(obj.Pos(), "\t%s", obj.Name())
   130  			}
   131  			return true
   132  		}
   133  	}
   134  	return false
   135  }
   136  
   137  // typExpr type-checks the type expression e and returns its type, or Typ[Invalid].
   138  // If def != nil, e is the type specification for the named type def, declared
   139  // in a type declaration, and def.underlying will be set to the type of e before
   140  // any components of e are type-checked. Path contains the path of named types
   141  // referring to this type; i.e. it is the path of named types directly containing
   142  // each other and leading to the current type e. Indirect containment (e.g. via
   143  // pointer indirection, function parameter, etc.) breaks the path (leads to a new
   144  // path, and usually via calling Checker.typ below) and those types are not found
   145  // in the path.
   146  //
   147  func (check *Checker) typExpr(e ast.Expr, def *Named, path []*TypeName) (T Type) {
   148  	if trace {
   149  		check.trace(e.Pos(), "%s", e)
   150  		check.indent++
   151  		defer func() {
   152  			check.indent--
   153  			check.trace(e.Pos(), "=> %s", T)
   154  		}()
   155  	}
   156  
   157  	T = check.typExprInternal(e, def, path)
   158  	assert(isTyped(T))
   159  	check.recordTypeAndValue(e, typexpr, T, nil)
   160  
   161  	return
   162  }
   163  
   164  // typ is like typExpr (with a nil argument for the def parameter),
   165  // but typ breaks type cycles. It should be called for components of
   166  // types that break cycles, such as pointer base types, slice or map
   167  // element types, etc. See the comment in typExpr for details.
   168  //
   169  func (check *Checker) typ(e ast.Expr) Type {
   170  	// typExpr is called with a nil path indicating an indirection:
   171  	// push indir sentinel on object path
   172  	if useCycleMarking {
   173  		check.push(indir)
   174  		defer check.pop()
   175  	}
   176  	return check.typExpr(e, nil, nil)
   177  }
   178  
   179  // funcType type-checks a function or method type.
   180  func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast.FuncType) {
   181  	scope := NewScope(check.scope, token.NoPos, token.NoPos, "function")
   182  	scope.isFunc = true
   183  	check.recordScope(ftyp, scope)
   184  
   185  	recvList, _ := check.collectParams(scope, recvPar, false)
   186  	params, variadic := check.collectParams(scope, ftyp.Params, true)
   187  	results, _ := check.collectParams(scope, ftyp.Results, false)
   188  
   189  	if recvPar != nil {
   190  		// recv parameter list present (may be empty)
   191  		// spec: "The receiver is specified via an extra parameter section preceding the
   192  		// method name. That parameter section must declare a single parameter, the receiver."
   193  		var recv *Var
   194  		switch len(recvList) {
   195  		case 0:
   196  			check.error(recvPar.Pos(), "method is missing receiver")
   197  			recv = NewParam(0, nil, "", Typ[Invalid]) // ignore recv below
   198  		default:
   199  			// more than one receiver
   200  			check.error(recvList[len(recvList)-1].Pos(), "method must have exactly one receiver")
   201  			fallthrough // continue with first receiver
   202  		case 1:
   203  			recv = recvList[0]
   204  		}
   205  		// spec: "The receiver type must be of the form T or *T where T is a type name."
   206  		// (ignore invalid types - error was reported before)
   207  		if t, _ := deref(recv.typ); t != Typ[Invalid] {
   208  			var err string
   209  			if T, _ := t.(*Named); T != nil {
   210  				// spec: "The type denoted by T is called the receiver base type; it must not
   211  				// be a pointer or interface type and it must be declared in the same package
   212  				// as the method."
   213  				if T.obj.pkg != check.pkg {
   214  					err = "type not defined in this package"
   215  				} else {
   216  					// TODO(gri) This is not correct if the underlying type is unknown yet.
   217  					switch u := T.underlying.(type) {
   218  					case *Basic:
   219  						// unsafe.Pointer is treated like a regular pointer
   220  						if u.kind == UnsafePointer {
   221  							err = "unsafe.Pointer"
   222  						}
   223  					case *Pointer, *Interface:
   224  						err = "pointer or interface type"
   225  					}
   226  				}
   227  			} else {
   228  				err = "basic or unnamed type"
   229  			}
   230  			if err != "" {
   231  				check.errorf(recv.pos, "invalid receiver %s (%s)", recv.typ, err)
   232  				// ok to continue
   233  			}
   234  		}
   235  		sig.recv = recv
   236  	}
   237  
   238  	sig.scope = scope
   239  	sig.params = NewTuple(params...)
   240  	sig.results = NewTuple(results...)
   241  	sig.variadic = variadic
   242  }
   243  
   244  // typExprInternal drives type checking of types.
   245  // Must only be called by typExpr.
   246  //
   247  func (check *Checker) typExprInternal(e ast.Expr, def *Named, path []*TypeName) Type {
   248  	switch e := e.(type) {
   249  	case *ast.BadExpr:
   250  		// ignore - error reported before
   251  
   252  	case *ast.Ident:
   253  		var x operand
   254  		check.ident(&x, e, def, path)
   255  
   256  		switch x.mode {
   257  		case typexpr:
   258  			typ := x.typ
   259  			def.setUnderlying(typ)
   260  			return typ
   261  		case invalid:
   262  			// ignore - error reported before
   263  		case novalue:
   264  			check.errorf(x.pos(), "%s used as type", &x)
   265  		default:
   266  			check.errorf(x.pos(), "%s is not a type", &x)
   267  		}
   268  
   269  	case *ast.SelectorExpr:
   270  		var x operand
   271  		check.selector(&x, e)
   272  
   273  		switch x.mode {
   274  		case typexpr:
   275  			typ := x.typ
   276  			def.setUnderlying(typ)
   277  			return typ
   278  		case invalid:
   279  			// ignore - error reported before
   280  		case novalue:
   281  			check.errorf(x.pos(), "%s used as type", &x)
   282  		default:
   283  			check.errorf(x.pos(), "%s is not a type", &x)
   284  		}
   285  
   286  	case *ast.ParenExpr:
   287  		return check.typExpr(e.X, def, path)
   288  
   289  	case *ast.ArrayType:
   290  		if e.Len != nil {
   291  			typ := new(Array)
   292  			def.setUnderlying(typ)
   293  			typ.len = check.arrayLength(e.Len)
   294  			typ.elem = check.typExpr(e.Elt, nil, path)
   295  			return typ
   296  
   297  		} else {
   298  			typ := new(Slice)
   299  			def.setUnderlying(typ)
   300  			typ.elem = check.typ(e.Elt)
   301  			return typ
   302  		}
   303  
   304  	case *ast.StructType:
   305  		typ := new(Struct)
   306  		def.setUnderlying(typ)
   307  		check.structType(typ, e, path)
   308  		return typ
   309  
   310  	case *ast.StarExpr:
   311  		typ := new(Pointer)
   312  		def.setUnderlying(typ)
   313  		typ.base = check.typ(e.X)
   314  		return typ
   315  
   316  	case *ast.FuncType:
   317  		typ := new(Signature)
   318  		def.setUnderlying(typ)
   319  		check.funcType(typ, nil, e)
   320  		return typ
   321  
   322  	case *ast.InterfaceType:
   323  		typ := new(Interface)
   324  		def.setUnderlying(typ)
   325  		check.interfaceType(typ, e, def, path)
   326  		return typ
   327  
   328  	case *ast.MapType:
   329  		typ := new(Map)
   330  		def.setUnderlying(typ)
   331  
   332  		typ.key = check.typ(e.Key)
   333  		typ.elem = check.typ(e.Value)
   334  
   335  		// spec: "The comparison operators == and != must be fully defined
   336  		// for operands of the key type; thus the key type must not be a
   337  		// function, map, or slice."
   338  		//
   339  		// Delay this check because it requires fully setup types;
   340  		// it is safe to continue in any case (was issue 6667).
   341  		check.later(func() {
   342  			if !Comparable(typ.key) {
   343  				check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key)
   344  			}
   345  		})
   346  
   347  		return typ
   348  
   349  	case *ast.ChanType:
   350  		typ := new(Chan)
   351  		def.setUnderlying(typ)
   352  
   353  		dir := SendRecv
   354  		switch e.Dir {
   355  		case ast.SEND | ast.RECV:
   356  			// nothing to do
   357  		case ast.SEND:
   358  			dir = SendOnly
   359  		case ast.RECV:
   360  			dir = RecvOnly
   361  		default:
   362  			check.invalidAST(e.Pos(), "unknown channel direction %d", e.Dir)
   363  			// ok to continue
   364  		}
   365  
   366  		typ.dir = dir
   367  		typ.elem = check.typ(e.Value)
   368  		return typ
   369  
   370  	default:
   371  		check.errorf(e.Pos(), "%s is not a type", e)
   372  	}
   373  
   374  	typ := Typ[Invalid]
   375  	def.setUnderlying(typ)
   376  	return typ
   377  }
   378  
   379  // typeOrNil type-checks the type expression (or nil value) e
   380  // and returns the typ of e, or nil.
   381  // If e is neither a type nor nil, typOrNil returns Typ[Invalid].
   382  //
   383  func (check *Checker) typOrNil(e ast.Expr) Type {
   384  	var x operand
   385  	check.rawExpr(&x, e, nil)
   386  	switch x.mode {
   387  	case invalid:
   388  		// ignore - error reported before
   389  	case novalue:
   390  		check.errorf(x.pos(), "%s used as type", &x)
   391  	case typexpr:
   392  		return x.typ
   393  	case value:
   394  		if x.isNil() {
   395  			return nil
   396  		}
   397  		fallthrough
   398  	default:
   399  		check.errorf(x.pos(), "%s is not a type", &x)
   400  	}
   401  	return Typ[Invalid]
   402  }
   403  
   404  // arrayLength type-checks the array length expression e
   405  // and returns the constant length >= 0, or a value < 0
   406  // to indicate an error (and thus an unknown length).
   407  func (check *Checker) arrayLength(e ast.Expr) int64 {
   408  	var x operand
   409  	check.expr(&x, e)
   410  	if x.mode != constant_ {
   411  		if x.mode != invalid {
   412  			check.errorf(x.pos(), "array length %s must be constant", &x)
   413  		}
   414  		return -1
   415  	}
   416  	if isUntyped(x.typ) || isInteger(x.typ) {
   417  		if val := constant.ToInt(x.val); val.Kind() == constant.Int {
   418  			if representableConst(val, check.conf, Typ[Int], nil) {
   419  				if n, ok := constant.Int64Val(val); ok && n >= 0 {
   420  					return n
   421  				}
   422  				check.errorf(x.pos(), "invalid array length %s", &x)
   423  				return -1
   424  			}
   425  		}
   426  	}
   427  	check.errorf(x.pos(), "array length %s must be integer", &x)
   428  	return -1
   429  }
   430  
   431  func (check *Checker) collectParams(scope *Scope, list *ast.FieldList, variadicOk bool) (params []*Var, variadic bool) {
   432  	if list == nil {
   433  		return
   434  	}
   435  
   436  	var named, anonymous bool
   437  	for i, field := range list.List {
   438  		ftype := field.Type
   439  		if t, _ := ftype.(*ast.Ellipsis); t != nil {
   440  			ftype = t.Elt
   441  			if variadicOk && i == len(list.List)-1 {
   442  				variadic = true
   443  			} else {
   444  				check.invalidAST(field.Pos(), "... not permitted")
   445  				// ignore ... and continue
   446  			}
   447  		}
   448  		typ := check.typ(ftype)
   449  		// The parser ensures that f.Tag is nil and we don't
   450  		// care if a constructed AST contains a non-nil tag.
   451  		if len(field.Names) > 0 {
   452  			// named parameter
   453  			for _, name := range field.Names {
   454  				if name.Name == "" {
   455  					check.invalidAST(name.Pos(), "anonymous parameter")
   456  					// ok to continue
   457  				}
   458  				par := NewParam(name.Pos(), check.pkg, name.Name, typ)
   459  				check.declare(scope, name, par, scope.pos)
   460  				params = append(params, par)
   461  			}
   462  			named = true
   463  		} else {
   464  			// anonymous parameter
   465  			par := NewParam(ftype.Pos(), check.pkg, "", typ)
   466  			check.recordImplicit(field, par)
   467  			params = append(params, par)
   468  			anonymous = true
   469  		}
   470  	}
   471  
   472  	if named && anonymous {
   473  		check.invalidAST(list.Pos(), "list contains both named and anonymous parameters")
   474  		// ok to continue
   475  	}
   476  
   477  	// For a variadic function, change the last parameter's type from T to []T.
   478  	if variadic && len(params) > 0 {
   479  		last := params[len(params)-1]
   480  		last.typ = &Slice{elem: last.typ}
   481  	}
   482  
   483  	return
   484  }
   485  
   486  func (check *Checker) declareInSet(oset *objset, pos token.Pos, obj Object) bool {
   487  	if alt := oset.insert(obj); alt != nil {
   488  		check.errorf(pos, "%s redeclared", obj.Name())
   489  		check.reportAltDecl(alt)
   490  		return false
   491  	}
   492  	return true
   493  }
   494  
   495  func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, def *Named, path []*TypeName) {
   496  	// fast-track empty interface
   497  	if iface.Methods.List == nil {
   498  		ityp.allMethods = markComplete
   499  		return
   500  	}
   501  
   502  	// collect embedded interfaces
   503  	// Only needed for printing and API. Delay collection
   504  	// to end of type-checking (for package-global interfaces)
   505  	// when all types are complete. Local interfaces are handled
   506  	// after each statement (as each statement processes delayed
   507  	// functions).
   508  	interfaceContext := check.context // capture for use in closure below
   509  	check.later(func() {
   510  		if trace {
   511  			check.trace(iface.Pos(), "-- delayed checking embedded interfaces of %v", iface)
   512  			check.indent++
   513  			defer func() {
   514  				check.indent--
   515  			}()
   516  		}
   517  
   518  		// The context must be restored since for local interfaces
   519  		// delayed functions are processed after each statement
   520  		// (was issue #24140).
   521  		defer func(ctxt context) {
   522  			check.context = ctxt
   523  		}(check.context)
   524  		check.context = interfaceContext
   525  
   526  		for _, f := range iface.Methods.List {
   527  			if len(f.Names) == 0 {
   528  				typ := check.typ(f.Type)
   529  				// typ should be a named type denoting an interface
   530  				// (the parser will make sure it's a named type but
   531  				// constructed ASTs may be wrong).
   532  				if typ == Typ[Invalid] {
   533  					continue // error reported before
   534  				}
   535  				embed, _ := typ.Underlying().(*Interface)
   536  				if embed == nil {
   537  					check.errorf(f.Type.Pos(), "%s is not an interface", typ)
   538  					continue
   539  				}
   540  				// Correct embedded interfaces must be complete -
   541  				// don't just assert, but report error since this
   542  				// used to be the underlying cause for issue #18395.
   543  				if embed.allMethods == nil {
   544  					check.dump("%v: incomplete embedded interface %s", f.Type.Pos(), typ)
   545  					unreachable()
   546  				}
   547  				// collect interface
   548  				ityp.embeddeds = append(ityp.embeddeds, typ)
   549  			}
   550  		}
   551  		// sort to match NewInterface/NewInterface2
   552  		// TODO(gri) we may be able to switch to source order
   553  		sort.Stable(byUniqueTypeName(ityp.embeddeds))
   554  	})
   555  
   556  	// compute method set
   557  	var tname *TypeName
   558  	if def != nil {
   559  		tname = def.obj
   560  	}
   561  	info := check.infoFromTypeLit(check.scope, iface, tname, path)
   562  	if info == nil || info == &emptyIfaceInfo {
   563  		// error or empty interface - exit early
   564  		ityp.allMethods = markComplete
   565  		return
   566  	}
   567  
   568  	// use named receiver type if available (for better error messages)
   569  	var recvTyp Type = ityp
   570  	if def != nil {
   571  		recvTyp = def
   572  	}
   573  
   574  	// collect methods
   575  	var sigfix []*methodInfo
   576  	for i, minfo := range info.methods {
   577  		fun := minfo.fun
   578  		if fun == nil {
   579  			name := minfo.src.Names[0]
   580  			pos := name.Pos()
   581  			// Don't type-check signature yet - use an
   582  			// empty signature now and update it later.
   583  			// Since we know the receiver, set it up now
   584  			// (required to avoid crash in ptrRecv; see
   585  			// e.g. test case for issue 6638).
   586  			// TODO(gri) Consider marking methods signatures
   587  			// as incomplete, for better error messages. See
   588  			// also the T4 and T5 tests in testdata/cycles2.src.
   589  			sig := new(Signature)
   590  			sig.recv = NewVar(pos, check.pkg, "", recvTyp)
   591  			fun = NewFunc(pos, check.pkg, name.Name, sig)
   592  			minfo.fun = fun
   593  			check.recordDef(name, fun)
   594  			sigfix = append(sigfix, minfo)
   595  		}
   596  		// fun != nil
   597  		if i < info.explicits {
   598  			ityp.methods = append(ityp.methods, fun)
   599  		}
   600  		ityp.allMethods = append(ityp.allMethods, fun)
   601  	}
   602  
   603  	// fix signatures now that we have collected all methods
   604  	savedContext := check.context
   605  	for _, minfo := range sigfix {
   606  		// (possibly embedded) methods must be type-checked within their scope and
   607  		// type-checking them must not affect the current context (was issue #23914)
   608  		check.context = context{scope: minfo.scope}
   609  		typ := check.typ(minfo.src.Type)
   610  		sig, _ := typ.(*Signature)
   611  		if sig == nil {
   612  			if typ != Typ[Invalid] {
   613  				check.invalidAST(minfo.src.Type.Pos(), "%s is not a method signature", typ)
   614  			}
   615  			continue // keep method with empty method signature
   616  		}
   617  		// update signature, but keep recv that was set up before
   618  		old := minfo.fun.typ.(*Signature)
   619  		sig.recv = old.recv
   620  		*old = *sig // update signature (don't replace pointer!)
   621  	}
   622  	check.context = savedContext
   623  
   624  	// sort to match NewInterface/NewInterface2
   625  	// TODO(gri) we may be able to switch to source order
   626  	sort.Sort(byUniqueMethodName(ityp.methods))
   627  
   628  	if ityp.allMethods == nil {
   629  		ityp.allMethods = markComplete
   630  	} else {
   631  		sort.Sort(byUniqueMethodName(ityp.allMethods))
   632  	}
   633  }
   634  
   635  // byUniqueTypeName named type lists can be sorted by their unique type names.
   636  type byUniqueTypeName []Type
   637  
   638  func (a byUniqueTypeName) Len() int           { return len(a) }
   639  func (a byUniqueTypeName) Less(i, j int) bool { return sortName(a[i]) < sortName(a[j]) }
   640  func (a byUniqueTypeName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   641  
   642  func sortName(t Type) string {
   643  	if named, _ := t.(*Named); named != nil {
   644  		return named.obj.Id()
   645  	}
   646  	return ""
   647  }
   648  
   649  // byUniqueMethodName method lists can be sorted by their unique method names.
   650  type byUniqueMethodName []*Func
   651  
   652  func (a byUniqueMethodName) Len() int           { return len(a) }
   653  func (a byUniqueMethodName) Less(i, j int) bool { return a[i].Id() < a[j].Id() }
   654  func (a byUniqueMethodName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   655  
   656  func (check *Checker) tag(t *ast.BasicLit) string {
   657  	if t != nil {
   658  		if t.Kind == token.STRING {
   659  			if val, err := strconv.Unquote(t.Value); err == nil {
   660  				return val
   661  			}
   662  		}
   663  		check.invalidAST(t.Pos(), "incorrect tag syntax: %q", t.Value)
   664  	}
   665  	return ""
   666  }
   667  
   668  func (check *Checker) structType(styp *Struct, e *ast.StructType, path []*TypeName) {
   669  	list := e.Fields
   670  	if list == nil {
   671  		return
   672  	}
   673  
   674  	// struct fields and tags
   675  	var fields []*Var
   676  	var tags []string
   677  
   678  	// for double-declaration checks
   679  	var fset objset
   680  
   681  	// current field typ and tag
   682  	var typ Type
   683  	var tag string
   684  	add := func(ident *ast.Ident, embedded bool, pos token.Pos) {
   685  		if tag != "" && tags == nil {
   686  			tags = make([]string, len(fields))
   687  		}
   688  		if tags != nil {
   689  			tags = append(tags, tag)
   690  		}
   691  
   692  		name := ident.Name
   693  		fld := NewField(pos, check.pkg, name, typ, embedded)
   694  		// spec: "Within a struct, non-blank field names must be unique."
   695  		if name == "_" || check.declareInSet(&fset, pos, fld) {
   696  			fields = append(fields, fld)
   697  			check.recordDef(ident, fld)
   698  		}
   699  	}
   700  
   701  	// addInvalid adds an embedded field of invalid type to the struct for
   702  	// fields with errors; this keeps the number of struct fields in sync
   703  	// with the source as long as the fields are _ or have different names
   704  	// (issue #25627).
   705  	addInvalid := func(ident *ast.Ident, pos token.Pos) {
   706  		typ = Typ[Invalid]
   707  		tag = ""
   708  		add(ident, true, pos)
   709  	}
   710  
   711  	for _, f := range list.List {
   712  		typ = check.typExpr(f.Type, nil, path)
   713  		tag = check.tag(f.Tag)
   714  		if len(f.Names) > 0 {
   715  			// named fields
   716  			for _, name := range f.Names {
   717  				add(name, false, name.Pos())
   718  			}
   719  		} else {
   720  			// embedded field
   721  			// spec: "An embedded type must be specified as a type name T or as a pointer
   722  			// to a non-interface type name *T, and T itself may not be a pointer type."
   723  			pos := f.Type.Pos()
   724  			name := embeddedFieldIdent(f.Type)
   725  			if name == nil {
   726  				check.invalidAST(pos, "embedded field type %s has no name", f.Type)
   727  				name = ast.NewIdent("_")
   728  				name.NamePos = pos
   729  				addInvalid(name, pos)
   730  				continue
   731  			}
   732  			t, isPtr := deref(typ)
   733  			// Because we have a name, typ must be of the form T or *T, where T is the name
   734  			// of a (named or alias) type, and t (= deref(typ)) must be the type of T.
   735  			switch t := t.Underlying().(type) {
   736  			case *Basic:
   737  				if t == Typ[Invalid] {
   738  					// error was reported before
   739  					addInvalid(name, pos)
   740  					continue
   741  				}
   742  
   743  				// unsafe.Pointer is treated like a regular pointer
   744  				if t.kind == UnsafePointer {
   745  					check.errorf(pos, "embedded field type cannot be unsafe.Pointer")
   746  					addInvalid(name, pos)
   747  					continue
   748  				}
   749  
   750  			case *Pointer:
   751  				check.errorf(pos, "embedded field type cannot be a pointer")
   752  				addInvalid(name, pos)
   753  				continue
   754  
   755  			case *Interface:
   756  				if isPtr {
   757  					check.errorf(pos, "embedded field type cannot be a pointer to an interface")
   758  					addInvalid(name, pos)
   759  					continue
   760  				}
   761  			}
   762  			add(name, true, pos)
   763  		}
   764  	}
   765  
   766  	styp.fields = fields
   767  	styp.tags = tags
   768  }
   769  
   770  func embeddedFieldIdent(e ast.Expr) *ast.Ident {
   771  	switch e := e.(type) {
   772  	case *ast.Ident:
   773  		return e
   774  	case *ast.StarExpr:
   775  		// *T is valid, but **T is not
   776  		if _, ok := e.X.(*ast.StarExpr); !ok {
   777  			return embeddedFieldIdent(e.X)
   778  		}
   779  	case *ast.SelectorExpr:
   780  		return e.Sel
   781  	}
   782  	return nil // invalid embedded field
   783  }
   784  

View as plain text