...

Source file src/cmd/compile/internal/types2/resolver.go

Documentation: cmd/compile/internal/types2

     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  package types2
     6  
     7  import (
     8  	"cmd/compile/internal/syntax"
     9  	"fmt"
    10  	"go/constant"
    11  	. "internal/types/errors"
    12  	"sort"
    13  	"strconv"
    14  	"strings"
    15  	"unicode"
    16  )
    17  
    18  // A declInfo describes a package-level const, type, var, or func declaration.
    19  type declInfo struct {
    20  	file      *Scope           // scope of file containing this declaration
    21  	lhs       []*Var           // lhs of n:1 variable declarations, or nil
    22  	vtyp      syntax.Expr      // type, or nil (for const and var declarations only)
    23  	init      syntax.Expr      // init/orig expression, or nil (for const and var declarations only)
    24  	inherited bool             // if set, the init expression is inherited from a previous constant declaration
    25  	tdecl     *syntax.TypeDecl // type declaration, or nil
    26  	fdecl     *syntax.FuncDecl // func declaration, or nil
    27  
    28  	// The deps field tracks initialization expression dependencies.
    29  	deps map[Object]bool // lazily initialized
    30  }
    31  
    32  // hasInitializer reports whether the declared object has an initialization
    33  // expression or function body.
    34  func (d *declInfo) hasInitializer() bool {
    35  	return d.init != nil || d.fdecl != nil && d.fdecl.Body != nil
    36  }
    37  
    38  // addDep adds obj to the set of objects d's init expression depends on.
    39  func (d *declInfo) addDep(obj Object) {
    40  	m := d.deps
    41  	if m == nil {
    42  		m = make(map[Object]bool)
    43  		d.deps = m
    44  	}
    45  	m[obj] = true
    46  }
    47  
    48  // arity checks that the lhs and rhs of a const or var decl
    49  // have a matching number of names and initialization values.
    50  // If inherited is set, the initialization values are from
    51  // another (constant) declaration.
    52  func (check *Checker) arity(pos syntax.Pos, names []*syntax.Name, inits []syntax.Expr, constDecl, inherited bool) {
    53  	l := len(names)
    54  	r := len(inits)
    55  
    56  	const code = WrongAssignCount
    57  	switch {
    58  	case l < r:
    59  		n := inits[l]
    60  		if inherited {
    61  			check.errorf(pos, code, "extra init expr at %s", n.Pos())
    62  		} else {
    63  			check.errorf(n, code, "extra init expr %s", n)
    64  		}
    65  	case l > r && (constDecl || r != 1): // if r == 1 it may be a multi-valued function and we can't say anything yet
    66  		n := names[r]
    67  		check.errorf(n, code, "missing init expr for %s", n.Value)
    68  	}
    69  }
    70  
    71  func validatedImportPath(path string) (string, error) {
    72  	s, err := strconv.Unquote(path)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  	if s == "" {
    77  		return "", fmt.Errorf("empty string")
    78  	}
    79  	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
    80  	for _, r := range s {
    81  		if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
    82  			return s, fmt.Errorf("invalid character %#U", r)
    83  		}
    84  	}
    85  	return s, nil
    86  }
    87  
    88  // declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
    89  // and updates check.objMap. The object must not be a function or method.
    90  func (check *Checker) declarePkgObj(ident *syntax.Name, obj Object, d *declInfo) {
    91  	assert(ident.Value == obj.Name())
    92  
    93  	// spec: "A package-scope or file-scope identifier with name init
    94  	// may only be declared to be a function with this (func()) signature."
    95  	if ident.Value == "init" {
    96  		check.error(ident, InvalidInitDecl, "cannot declare init - must be func")
    97  		return
    98  	}
    99  
   100  	// spec: "The main package must have package name main and declare
   101  	// a function main that takes no arguments and returns no value."
   102  	if ident.Value == "main" && check.pkg.name == "main" {
   103  		check.error(ident, InvalidMainDecl, "cannot declare main - must be func")
   104  		return
   105  	}
   106  
   107  	check.declare(check.pkg.scope, ident, obj, nopos)
   108  	check.objMap[obj] = d
   109  	obj.setOrder(uint32(len(check.objMap)))
   110  }
   111  
   112  // filename returns a filename suitable for debugging output.
   113  func (check *Checker) filename(fileNo int) string {
   114  	file := check.files[fileNo]
   115  	if pos := file.Pos(); pos.IsKnown() {
   116  		// return check.fset.File(pos).Name()
   117  		// TODO(gri) do we need the actual file name here?
   118  		return pos.RelFilename()
   119  	}
   120  	return fmt.Sprintf("file[%d]", fileNo)
   121  }
   122  
   123  func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
   124  	// If we already have a package for the given (path, dir)
   125  	// pair, use it instead of doing a full import.
   126  	// Checker.impMap only caches packages that are marked Complete
   127  	// or fake (dummy packages for failed imports). Incomplete but
   128  	// non-fake packages do require an import to complete them.
   129  	key := importKey{path, dir}
   130  	imp := check.impMap[key]
   131  	if imp != nil {
   132  		return imp
   133  	}
   134  
   135  	// no package yet => import it
   136  	if path == "C" && (check.conf.FakeImportC || check.conf.go115UsesCgo) {
   137  		if check.conf.FakeImportC && check.conf.go115UsesCgo {
   138  			check.error(pos, BadImportPath, "cannot use FakeImportC and go115UsesCgo together")
   139  		}
   140  		imp = NewPackage("C", "C")
   141  		imp.fake = true // package scope is not populated
   142  		imp.cgo = check.conf.go115UsesCgo
   143  	} else {
   144  		// ordinary import
   145  		var err error
   146  		if importer := check.conf.Importer; importer == nil {
   147  			err = fmt.Errorf("Config.Importer not installed")
   148  		} else if importerFrom, ok := importer.(ImporterFrom); ok {
   149  			imp, err = importerFrom.ImportFrom(path, dir, 0)
   150  			if imp == nil && err == nil {
   151  				err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
   152  			}
   153  		} else {
   154  			imp, err = importer.Import(path)
   155  			if imp == nil && err == nil {
   156  				err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
   157  			}
   158  		}
   159  		// make sure we have a valid package name
   160  		// (errors here can only happen through manipulation of packages after creation)
   161  		if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
   162  			err = fmt.Errorf("invalid package name: %q", imp.name)
   163  			imp = nil // create fake package below
   164  		}
   165  		if err != nil {
   166  			check.errorf(pos, BrokenImport, "could not import %s (%s)", path, err)
   167  			if imp == nil {
   168  				// create a new fake package
   169  				// come up with a sensible package name (heuristic)
   170  				name := path
   171  				if i := len(name); i > 0 && name[i-1] == '/' {
   172  					name = name[:i-1]
   173  				}
   174  				if i := strings.LastIndex(name, "/"); i >= 0 {
   175  					name = name[i+1:]
   176  				}
   177  				imp = NewPackage(path, name)
   178  			}
   179  			// continue to use the package as best as we can
   180  			imp.fake = true // avoid follow-up lookup failures
   181  		}
   182  	}
   183  
   184  	// package should be complete or marked fake, but be cautious
   185  	if imp.complete || imp.fake {
   186  		check.impMap[key] = imp
   187  		// Once we've formatted an error message, keep the pkgPathMap
   188  		// up-to-date on subsequent imports. It is used for package
   189  		// qualification in error messages.
   190  		if check.pkgPathMap != nil {
   191  			check.markImports(imp)
   192  		}
   193  		return imp
   194  	}
   195  
   196  	// something went wrong (importer may have returned incomplete package without error)
   197  	return nil
   198  }
   199  
   200  // collectObjects collects all file and package objects and inserts them
   201  // into their respective scopes. It also performs imports and associates
   202  // methods with receiver base type names.
   203  func (check *Checker) collectObjects() {
   204  	pkg := check.pkg
   205  
   206  	// pkgImports is the set of packages already imported by any package file seen
   207  	// so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
   208  	// it (pkg.imports may not be empty if we are checking test files incrementally).
   209  	// Note that pkgImports is keyed by package (and thus package path), not by an
   210  	// importKey value. Two different importKey values may map to the same package
   211  	// which is why we cannot use the check.impMap here.
   212  	var pkgImports = make(map[*Package]bool)
   213  	for _, imp := range pkg.imports {
   214  		pkgImports[imp] = true
   215  	}
   216  
   217  	type methodInfo struct {
   218  		obj  *Func        // method
   219  		ptr  bool         // true if pointer receiver
   220  		recv *syntax.Name // receiver type name
   221  	}
   222  	var methods []methodInfo // collected methods with valid receivers and non-blank _ names
   223  	var fileScopes []*Scope
   224  	for fileNo, file := range check.files {
   225  		// The package identifier denotes the current package,
   226  		// but there is no corresponding package object.
   227  		check.recordDef(file.PkgName, nil)
   228  
   229  		fileScope := NewScope(pkg.scope, syntax.StartPos(file), syntax.EndPos(file), check.filename(fileNo))
   230  		fileScopes = append(fileScopes, fileScope)
   231  		check.recordScope(file, fileScope)
   232  
   233  		// determine file directory, necessary to resolve imports
   234  		// FileName may be "" (typically for tests) in which case
   235  		// we get "." as the directory which is what we would want.
   236  		fileDir := dir(file.PkgName.Pos().RelFilename()) // TODO(gri) should this be filename?
   237  
   238  		first := -1                // index of first ConstDecl in the current group, or -1
   239  		var last *syntax.ConstDecl // last ConstDecl with init expressions, or nil
   240  		for index, decl := range file.DeclList {
   241  			if _, ok := decl.(*syntax.ConstDecl); !ok {
   242  				first = -1 // we're not in a constant declaration
   243  			}
   244  
   245  			switch s := decl.(type) {
   246  			case *syntax.ImportDecl:
   247  				// import package
   248  				if s.Path == nil || s.Path.Bad {
   249  					continue // error reported during parsing
   250  				}
   251  				path, err := validatedImportPath(s.Path.Value)
   252  				if err != nil {
   253  					check.errorf(s.Path, BadImportPath, "invalid import path (%s)", err)
   254  					continue
   255  				}
   256  
   257  				imp := check.importPackage(s.Path.Pos(), path, fileDir)
   258  				if imp == nil {
   259  					continue
   260  				}
   261  
   262  				// local name overrides imported package name
   263  				name := imp.name
   264  				if s.LocalPkgName != nil {
   265  					name = s.LocalPkgName.Value
   266  					if path == "C" {
   267  						// match 1.17 cmd/compile (not prescribed by spec)
   268  						check.error(s.LocalPkgName, ImportCRenamed, `cannot rename import "C"`)
   269  						continue
   270  					}
   271  				}
   272  
   273  				if name == "init" {
   274  					check.error(s, InvalidInitDecl, "cannot import package as init - init must be a func")
   275  					continue
   276  				}
   277  
   278  				// add package to list of explicit imports
   279  				// (this functionality is provided as a convenience
   280  				// for clients; it is not needed for type-checking)
   281  				if !pkgImports[imp] {
   282  					pkgImports[imp] = true
   283  					pkg.imports = append(pkg.imports, imp)
   284  				}
   285  
   286  				pkgName := NewPkgName(s.Pos(), pkg, name, imp)
   287  				if s.LocalPkgName != nil {
   288  					// in a dot-import, the dot represents the package
   289  					check.recordDef(s.LocalPkgName, pkgName)
   290  				} else {
   291  					check.recordImplicit(s, pkgName)
   292  				}
   293  
   294  				if imp.fake {
   295  					// match 1.17 cmd/compile (not prescribed by spec)
   296  					pkgName.used = true
   297  				}
   298  
   299  				// add import to file scope
   300  				check.imports = append(check.imports, pkgName)
   301  				if name == "." {
   302  					// dot-import
   303  					if check.dotImportMap == nil {
   304  						check.dotImportMap = make(map[dotImportKey]*PkgName)
   305  					}
   306  					// merge imported scope with file scope
   307  					for name, obj := range imp.scope.elems {
   308  						// Note: Avoid eager resolve(name, obj) here, so we only
   309  						// resolve dot-imported objects as needed.
   310  
   311  						// A package scope may contain non-exported objects,
   312  						// do not import them!
   313  						if isExported(name) {
   314  							// declare dot-imported object
   315  							// (Do not use check.declare because it modifies the object
   316  							// via Object.setScopePos, which leads to a race condition;
   317  							// the object may be imported into more than one file scope
   318  							// concurrently. See go.dev/issue/32154.)
   319  							if alt := fileScope.Lookup(name); alt != nil {
   320  								err := check.newError(DuplicateDecl)
   321  								err.addf(s.LocalPkgName, "%s redeclared in this block", alt.Name())
   322  								err.addAltDecl(alt)
   323  								err.report()
   324  							} else {
   325  								fileScope.insert(name, obj)
   326  								check.dotImportMap[dotImportKey{fileScope, name}] = pkgName
   327  							}
   328  						}
   329  					}
   330  				} else {
   331  					// declare imported package object in file scope
   332  					// (no need to provide s.LocalPkgName since we called check.recordDef earlier)
   333  					check.declare(fileScope, nil, pkgName, nopos)
   334  				}
   335  
   336  			case *syntax.ConstDecl:
   337  				// iota is the index of the current constDecl within the group
   338  				if first < 0 || s.Group == nil || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
   339  					first = index
   340  					last = nil
   341  				}
   342  				iota := constant.MakeInt64(int64(index - first))
   343  
   344  				// determine which initialization expressions to use
   345  				inherited := true
   346  				switch {
   347  				case s.Type != nil || s.Values != nil:
   348  					last = s
   349  					inherited = false
   350  				case last == nil:
   351  					last = new(syntax.ConstDecl) // make sure last exists
   352  					inherited = false
   353  				}
   354  
   355  				// declare all constants
   356  				values := syntax.UnpackListExpr(last.Values)
   357  				for i, name := range s.NameList {
   358  					obj := NewConst(name.Pos(), pkg, name.Value, nil, iota)
   359  
   360  					var init syntax.Expr
   361  					if i < len(values) {
   362  						init = values[i]
   363  					}
   364  
   365  					d := &declInfo{file: fileScope, vtyp: last.Type, init: init, inherited: inherited}
   366  					check.declarePkgObj(name, obj, d)
   367  				}
   368  
   369  				// Constants must always have init values.
   370  				check.arity(s.Pos(), s.NameList, values, true, inherited)
   371  
   372  			case *syntax.VarDecl:
   373  				lhs := make([]*Var, len(s.NameList))
   374  				// If there's exactly one rhs initializer, use
   375  				// the same declInfo d1 for all lhs variables
   376  				// so that each lhs variable depends on the same
   377  				// rhs initializer (n:1 var declaration).
   378  				var d1 *declInfo
   379  				if _, ok := s.Values.(*syntax.ListExpr); !ok {
   380  					// The lhs elements are only set up after the for loop below,
   381  					// but that's ok because declarePkgObj only collects the declInfo
   382  					// for a later phase.
   383  					d1 = &declInfo{file: fileScope, lhs: lhs, vtyp: s.Type, init: s.Values}
   384  				}
   385  
   386  				// declare all variables
   387  				values := syntax.UnpackListExpr(s.Values)
   388  				for i, name := range s.NameList {
   389  					obj := NewVar(name.Pos(), pkg, name.Value, nil)
   390  					lhs[i] = obj
   391  
   392  					d := d1
   393  					if d == nil {
   394  						// individual assignments
   395  						var init syntax.Expr
   396  						if i < len(values) {
   397  							init = values[i]
   398  						}
   399  						d = &declInfo{file: fileScope, vtyp: s.Type, init: init}
   400  					}
   401  
   402  					check.declarePkgObj(name, obj, d)
   403  				}
   404  
   405  				// If we have no type, we must have values.
   406  				if s.Type == nil || values != nil {
   407  					check.arity(s.Pos(), s.NameList, values, false, false)
   408  				}
   409  
   410  			case *syntax.TypeDecl:
   411  				obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Value, nil)
   412  				check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, tdecl: s})
   413  
   414  			case *syntax.FuncDecl:
   415  				name := s.Name.Value
   416  				obj := NewFunc(s.Name.Pos(), pkg, name, nil)
   417  				hasTParamError := false // avoid duplicate type parameter errors
   418  				if s.Recv == nil {
   419  					// regular function
   420  					if name == "init" || name == "main" && pkg.name == "main" {
   421  						code := InvalidInitDecl
   422  						if name == "main" {
   423  							code = InvalidMainDecl
   424  						}
   425  						if len(s.TParamList) != 0 {
   426  							check.softErrorf(s.TParamList[0], code, "func %s must have no type parameters", name)
   427  							hasTParamError = true
   428  						}
   429  						if t := s.Type; len(t.ParamList) != 0 || len(t.ResultList) != 0 {
   430  							check.softErrorf(s.Name, code, "func %s must have no arguments and no return values", name)
   431  						}
   432  					}
   433  					// don't declare init functions in the package scope - they are invisible
   434  					if name == "init" {
   435  						obj.parent = pkg.scope
   436  						check.recordDef(s.Name, obj)
   437  						// init functions must have a body
   438  						if s.Body == nil {
   439  							// TODO(gri) make this error message consistent with the others above
   440  							check.softErrorf(obj.pos, MissingInitBody, "missing function body")
   441  						}
   442  					} else {
   443  						check.declare(pkg.scope, s.Name, obj, nopos)
   444  					}
   445  				} else {
   446  					// method
   447  					// d.Recv != nil
   448  					ptr, recv, _ := check.unpackRecv(s.Recv.Type, false)
   449  					// Methods with invalid receiver cannot be associated to a type, and
   450  					// methods with blank _ names are never found; no need to collect any
   451  					// of them. They will still be type-checked with all the other functions.
   452  					if recv != nil && name != "_" {
   453  						methods = append(methods, methodInfo{obj, ptr, recv})
   454  					}
   455  					check.recordDef(s.Name, obj)
   456  				}
   457  				_ = len(s.TParamList) != 0 && !hasTParamError && check.verifyVersionf(s.TParamList[0], go1_18, "type parameter")
   458  				info := &declInfo{file: fileScope, fdecl: s}
   459  				// Methods are not package-level objects but we still track them in the
   460  				// object map so that we can handle them like regular functions (if the
   461  				// receiver is invalid); also we need their fdecl info when associating
   462  				// them with their receiver base type, below.
   463  				check.objMap[obj] = info
   464  				obj.setOrder(uint32(len(check.objMap)))
   465  
   466  			default:
   467  				check.errorf(s, InvalidSyntaxTree, "unknown syntax.Decl node %T", s)
   468  			}
   469  		}
   470  	}
   471  
   472  	// verify that objects in package and file scopes have different names
   473  	for _, scope := range fileScopes {
   474  		for name, obj := range scope.elems {
   475  			if alt := pkg.scope.Lookup(name); alt != nil {
   476  				obj = resolve(name, obj)
   477  				err := check.newError(DuplicateDecl)
   478  				if pkg, ok := obj.(*PkgName); ok {
   479  					err.addf(alt, "%s already declared through import of %s", alt.Name(), pkg.Imported())
   480  					err.addAltDecl(pkg)
   481  				} else {
   482  					err.addf(alt, "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
   483  					// TODO(gri) dot-imported objects don't have a position; addAltDecl won't print anything
   484  					err.addAltDecl(obj)
   485  				}
   486  				err.report()
   487  			}
   488  		}
   489  	}
   490  
   491  	// Now that we have all package scope objects and all methods,
   492  	// associate methods with receiver base type name where possible.
   493  	// Ignore methods that have an invalid receiver. They will be
   494  	// type-checked later, with regular functions.
   495  	if methods != nil {
   496  		check.methods = make(map[*TypeName][]*Func)
   497  		for i := range methods {
   498  			m := &methods[i]
   499  			// Determine the receiver base type and associate m with it.
   500  			ptr, base := check.resolveBaseTypeName(m.ptr, m.recv, fileScopes)
   501  			if base != nil {
   502  				m.obj.hasPtrRecv_ = ptr
   503  				check.methods[base] = append(check.methods[base], m.obj)
   504  			}
   505  		}
   506  	}
   507  }
   508  
   509  // unpackRecv unpacks a receiver type and returns its components: ptr indicates whether
   510  // rtyp is a pointer receiver, rname is the receiver type name, and tparams are its
   511  // type parameters, if any. The type parameters are only unpacked if unpackParams is
   512  // set. If rname is nil, the receiver is unusable (i.e., the source has a bug which we
   513  // cannot easily work around).
   514  func (check *Checker) unpackRecv(rtyp syntax.Expr, unpackParams bool) (ptr bool, rname *syntax.Name, tparams []*syntax.Name) {
   515  L: // unpack receiver type
   516  	// This accepts invalid receivers such as ***T and does not
   517  	// work for other invalid receivers, but we don't care. The
   518  	// validity of receiver expressions is checked elsewhere.
   519  	for {
   520  		switch t := rtyp.(type) {
   521  		case *syntax.ParenExpr:
   522  			rtyp = t.X
   523  		// case *ast.StarExpr:
   524  		//      ptr = true
   525  		// 	rtyp = t.X
   526  		case *syntax.Operation:
   527  			if t.Op != syntax.Mul || t.Y != nil {
   528  				break
   529  			}
   530  			ptr = true
   531  			rtyp = t.X
   532  		default:
   533  			break L
   534  		}
   535  	}
   536  
   537  	// unpack type parameters, if any
   538  	if ptyp, _ := rtyp.(*syntax.IndexExpr); ptyp != nil {
   539  		rtyp = ptyp.X
   540  		if unpackParams {
   541  			for _, arg := range syntax.UnpackListExpr(ptyp.Index) {
   542  				var par *syntax.Name
   543  				switch arg := arg.(type) {
   544  				case *syntax.Name:
   545  					par = arg
   546  				case *syntax.BadExpr:
   547  					// ignore - error already reported by parser
   548  				case nil:
   549  					check.error(ptyp, InvalidSyntaxTree, "parameterized receiver contains nil parameters")
   550  				default:
   551  					check.errorf(arg, BadDecl, "receiver type parameter %s must be an identifier", arg)
   552  				}
   553  				if par == nil {
   554  					par = syntax.NewName(arg.Pos(), "_")
   555  				}
   556  				tparams = append(tparams, par)
   557  			}
   558  
   559  		}
   560  	}
   561  
   562  	// unpack receiver name
   563  	if name, _ := rtyp.(*syntax.Name); name != nil {
   564  		rname = name
   565  	}
   566  
   567  	return
   568  }
   569  
   570  // resolveBaseTypeName returns the non-alias base type name for typ, and whether
   571  // there was a pointer indirection to get to it. The base type name must be declared
   572  // in package scope, and there can be at most one pointer indirection. If no such type
   573  // name exists, the returned base is nil.
   574  func (check *Checker) resolveBaseTypeName(seenPtr bool, typ syntax.Expr, fileScopes []*Scope) (ptr bool, base *TypeName) {
   575  	// Algorithm: Starting from a type expression, which may be a name,
   576  	// we follow that type through alias declarations until we reach a
   577  	// non-alias type name. If we encounter anything but pointer types or
   578  	// parentheses we're done. If we encounter more than one pointer type
   579  	// we're done.
   580  	ptr = seenPtr
   581  	var seen map[*TypeName]bool
   582  	for {
   583  		// check if we have a pointer type
   584  		// if pexpr, _ := typ.(*ast.StarExpr); pexpr != nil {
   585  		if pexpr, _ := typ.(*syntax.Operation); pexpr != nil && pexpr.Op == syntax.Mul && pexpr.Y == nil {
   586  			// if we've already seen a pointer, we're done
   587  			if ptr {
   588  				return false, nil
   589  			}
   590  			ptr = true
   591  			typ = syntax.Unparen(pexpr.X) // continue with pointer base type
   592  		}
   593  
   594  		// typ must be a name, or a C.name cgo selector.
   595  		var name string
   596  		switch typ := typ.(type) {
   597  		case *syntax.Name:
   598  			name = typ.Value
   599  		case *syntax.SelectorExpr:
   600  			// C.struct_foo is a valid type name for packages using cgo.
   601  			//
   602  			// Detect this case, and adjust name so that the correct TypeName is
   603  			// resolved below.
   604  			if ident, _ := typ.X.(*syntax.Name); ident != nil && ident.Value == "C" {
   605  				// Check whether "C" actually resolves to an import of "C", by looking
   606  				// in the appropriate file scope.
   607  				var obj Object
   608  				for _, scope := range fileScopes {
   609  					if scope.Contains(ident.Pos()) {
   610  						obj = scope.Lookup(ident.Value)
   611  					}
   612  				}
   613  				// If Config.go115UsesCgo is set, the typechecker will resolve Cgo
   614  				// selectors to their cgo name. We must do the same here.
   615  				if pname, _ := obj.(*PkgName); pname != nil {
   616  					if pname.imported.cgo { // only set if Config.go115UsesCgo is set
   617  						name = "_Ctype_" + typ.Sel.Value
   618  					}
   619  				}
   620  			}
   621  			if name == "" {
   622  				return false, nil
   623  			}
   624  		default:
   625  			return false, nil
   626  		}
   627  
   628  		// name must denote an object found in the current package scope
   629  		// (note that dot-imported objects are not in the package scope!)
   630  		obj := check.pkg.scope.Lookup(name)
   631  		if obj == nil {
   632  			return false, nil
   633  		}
   634  
   635  		// the object must be a type name...
   636  		tname, _ := obj.(*TypeName)
   637  		if tname == nil {
   638  			return false, nil
   639  		}
   640  
   641  		// ... which we have not seen before
   642  		if seen[tname] {
   643  			return false, nil
   644  		}
   645  
   646  		// we're done if tdecl defined tname as a new type
   647  		// (rather than an alias)
   648  		tdecl := check.objMap[tname].tdecl // must exist for objects in package scope
   649  		if !tdecl.Alias {
   650  			return ptr, tname
   651  		}
   652  
   653  		// otherwise, continue resolving
   654  		typ = tdecl.Type
   655  		if seen == nil {
   656  			seen = make(map[*TypeName]bool)
   657  		}
   658  		seen[tname] = true
   659  	}
   660  }
   661  
   662  // packageObjects typechecks all package objects, but not function bodies.
   663  func (check *Checker) packageObjects() {
   664  	// process package objects in source order for reproducible results
   665  	objList := make([]Object, len(check.objMap))
   666  	i := 0
   667  	for obj := range check.objMap {
   668  		objList[i] = obj
   669  		i++
   670  	}
   671  	sort.Sort(inSourceOrder(objList))
   672  
   673  	// add new methods to already type-checked types (from a prior Checker.Files call)
   674  	for _, obj := range objList {
   675  		if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
   676  			check.collectMethods(obj)
   677  		}
   678  	}
   679  
   680  	if false && check.conf.EnableAlias {
   681  		// With Alias nodes we can process declarations in any order.
   682  		//
   683  		// TODO(adonovan): unfortunately, Alias nodes
   684  		// (GODEBUG=gotypesalias=1) don't entirely resolve
   685  		// problems with cycles. For example, in
   686  		// GOROOT/test/typeparam/issue50259.go,
   687  		//
   688  		// 	type T[_ any] struct{}
   689  		// 	type A T[B]
   690  		// 	type B = T[A]
   691  		//
   692  		// TypeName A has Type Named during checking, but by
   693  		// the time the unified export data is written out,
   694  		// its Type is Invalid.
   695  		//
   696  		// Investigate and reenable this branch.
   697  		for _, obj := range objList {
   698  			check.objDecl(obj, nil)
   699  		}
   700  	} else {
   701  		// Without Alias nodes, we process non-alias type declarations first, followed by
   702  		// alias declarations, and then everything else. This appears to avoid most situations
   703  		// where the type of an alias is needed before it is available.
   704  		// There may still be cases where this is not good enough (see also go.dev/issue/25838).
   705  		// In those cases Checker.ident will report an error ("invalid use of type alias").
   706  		var aliasList []*TypeName
   707  		var othersList []Object // everything that's not a type
   708  		// phase 1: non-alias type declarations
   709  		for _, obj := range objList {
   710  			if tname, _ := obj.(*TypeName); tname != nil {
   711  				if check.objMap[tname].tdecl.Alias {
   712  					aliasList = append(aliasList, tname)
   713  				} else {
   714  					check.objDecl(obj, nil)
   715  				}
   716  			} else {
   717  				othersList = append(othersList, obj)
   718  			}
   719  		}
   720  		// phase 2: alias type declarations
   721  		for _, obj := range aliasList {
   722  			check.objDecl(obj, nil)
   723  		}
   724  		// phase 3: all other declarations
   725  		for _, obj := range othersList {
   726  			check.objDecl(obj, nil)
   727  		}
   728  	}
   729  
   730  	// At this point we may have a non-empty check.methods map; this means that not all
   731  	// entries were deleted at the end of typeDecl because the respective receiver base
   732  	// types were not found. In that case, an error was reported when declaring those
   733  	// methods. We can now safely discard this map.
   734  	check.methods = nil
   735  }
   736  
   737  // inSourceOrder implements the sort.Sort interface.
   738  type inSourceOrder []Object
   739  
   740  func (a inSourceOrder) Len() int           { return len(a) }
   741  func (a inSourceOrder) Less(i, j int) bool { return a[i].order() < a[j].order() }
   742  func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   743  
   744  // unusedImports checks for unused imports.
   745  func (check *Checker) unusedImports() {
   746  	// If function bodies are not checked, packages' uses are likely missing - don't check.
   747  	if check.conf.IgnoreFuncBodies {
   748  		return
   749  	}
   750  
   751  	// spec: "It is illegal (...) to directly import a package without referring to
   752  	// any of its exported identifiers. To import a package solely for its side-effects
   753  	// (initialization), use the blank identifier as explicit package name."
   754  
   755  	for _, obj := range check.imports {
   756  		if !obj.used && obj.name != "_" {
   757  			check.errorUnusedPkg(obj)
   758  		}
   759  	}
   760  }
   761  
   762  func (check *Checker) errorUnusedPkg(obj *PkgName) {
   763  	// If the package was imported with a name other than the final
   764  	// import path element, show it explicitly in the error message.
   765  	// Note that this handles both renamed imports and imports of
   766  	// packages containing unconventional package declarations.
   767  	// Note that this uses / always, even on Windows, because Go import
   768  	// paths always use forward slashes.
   769  	path := obj.imported.path
   770  	elem := path
   771  	if i := strings.LastIndex(elem, "/"); i >= 0 {
   772  		elem = elem[i+1:]
   773  	}
   774  	if obj.name == "" || obj.name == "." || obj.name == elem {
   775  		check.softErrorf(obj, UnusedImport, "%q imported and not used", path)
   776  	} else {
   777  		check.softErrorf(obj, UnusedImport, "%q imported as %s and not used", path, obj.name)
   778  	}
   779  }
   780  
   781  // dir makes a good-faith attempt to return the directory
   782  // portion of path. If path is empty, the result is ".".
   783  // (Per the go/build package dependency tests, we cannot import
   784  // path/filepath and simply use filepath.Dir.)
   785  func dir(path string) string {
   786  	if i := strings.LastIndexAny(path, `/\`); i > 0 {
   787  		return path[:i]
   788  	}
   789  	// i <= 0
   790  	return "."
   791  }
   792  

View as plain text