...
Run Format

Source file src/go/types/resolver.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  package types
     6  
     7  import (
     8  	"fmt"
     9  	"go/ast"
    10  	"go/constant"
    11  	"go/token"
    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  	typ   ast.Expr      // type, or nil
    23  	init  ast.Expr      // init/orig expression, or nil
    24  	fdecl *ast.FuncDecl // func declaration, or nil
    25  	alias bool          // type alias declaration
    26  
    27  	// The deps field tracks initialization expression dependencies.
    28  	deps objSet // lazily initialized
    29  }
    30  
    31  // An objSet is simply a set of objects.
    32  type objSet map[Object]bool
    33  
    34  // hasInitializer reports whether the declared object has an initialization
    35  // expression or function body.
    36  func (d *declInfo) hasInitializer() bool {
    37  	return d.init != nil || d.fdecl != nil && d.fdecl.Body != nil
    38  }
    39  
    40  // addDep adds obj to the set of objects d's init expression depends on.
    41  func (d *declInfo) addDep(obj Object) {
    42  	m := d.deps
    43  	if m == nil {
    44  		m = make(objSet)
    45  		d.deps = m
    46  	}
    47  	m[obj] = true
    48  }
    49  
    50  // arityMatch checks that the lhs and rhs of a const or var decl
    51  // have the appropriate number of names and init exprs. For const
    52  // decls, init is the value spec providing the init exprs; for
    53  // var decls, init is nil (the init exprs are in s in this case).
    54  func (check *Checker) arityMatch(s, init *ast.ValueSpec) {
    55  	l := len(s.Names)
    56  	r := len(s.Values)
    57  	if init != nil {
    58  		r = len(init.Values)
    59  	}
    60  
    61  	switch {
    62  	case init == nil && r == 0:
    63  		// var decl w/o init expr
    64  		if s.Type == nil {
    65  			check.errorf(s.Pos(), "missing type or init expr")
    66  		}
    67  	case l < r:
    68  		if l < len(s.Values) {
    69  			// init exprs from s
    70  			n := s.Values[l]
    71  			check.errorf(n.Pos(), "extra init expr %s", n)
    72  			// TODO(gri) avoid declared but not used error here
    73  		} else {
    74  			// init exprs "inherited"
    75  			check.errorf(s.Pos(), "extra init expr at %s", check.fset.Position(init.Pos()))
    76  			// TODO(gri) avoid declared but not used error here
    77  		}
    78  	case l > r && (init != nil || r != 1):
    79  		n := s.Names[r]
    80  		check.errorf(n.Pos(), "missing init expr for %s", n)
    81  	}
    82  }
    83  
    84  func validatedImportPath(path string) (string, error) {
    85  	s, err := strconv.Unquote(path)
    86  	if err != nil {
    87  		return "", err
    88  	}
    89  	if s == "" {
    90  		return "", fmt.Errorf("empty string")
    91  	}
    92  	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
    93  	for _, r := range s {
    94  		if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
    95  			return s, fmt.Errorf("invalid character %#U", r)
    96  		}
    97  	}
    98  	return s, nil
    99  }
   100  
   101  // declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
   102  // and updates check.objMap. The object must not be a function or method.
   103  func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
   104  	assert(ident.Name == obj.Name())
   105  
   106  	// spec: "A package-scope or file-scope identifier with name init
   107  	// may only be declared to be a function with this (func()) signature."
   108  	if ident.Name == "init" {
   109  		check.errorf(ident.Pos(), "cannot declare init - must be func")
   110  		return
   111  	}
   112  
   113  	// spec: "The main package must have package name main and declare
   114  	// a function main that takes no arguments and returns no value."
   115  	if ident.Name == "main" && check.pkg.name == "main" {
   116  		check.errorf(ident.Pos(), "cannot declare main - must be func")
   117  		return
   118  	}
   119  
   120  	check.declare(check.pkg.scope, ident, obj, token.NoPos)
   121  	check.objMap[obj] = d
   122  	obj.setOrder(uint32(len(check.objMap)))
   123  }
   124  
   125  // filename returns a filename suitable for debugging output.
   126  func (check *Checker) filename(fileNo int) string {
   127  	file := check.files[fileNo]
   128  	if pos := file.Pos(); pos.IsValid() {
   129  		return check.fset.File(pos).Name()
   130  	}
   131  	return fmt.Sprintf("file[%d]", fileNo)
   132  }
   133  
   134  func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
   135  	// If we already have a package for the given (path, dir)
   136  	// pair, use it instead of doing a full import.
   137  	// Checker.impMap only caches packages that are marked Complete
   138  	// or fake (dummy packages for failed imports). Incomplete but
   139  	// non-fake packages do require an import to complete them.
   140  	key := importKey{path, dir}
   141  	imp := check.impMap[key]
   142  	if imp != nil {
   143  		return imp
   144  	}
   145  
   146  	// no package yet => import it
   147  	if path == "C" && check.conf.FakeImportC {
   148  		imp = NewPackage("C", "C")
   149  		imp.fake = true
   150  	} else {
   151  		// ordinary import
   152  		var err error
   153  		if importer := check.conf.Importer; importer == nil {
   154  			err = fmt.Errorf("Config.Importer not installed")
   155  		} else if importerFrom, ok := importer.(ImporterFrom); ok {
   156  			imp, err = importerFrom.ImportFrom(path, dir, 0)
   157  			if imp == nil && err == nil {
   158  				err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
   159  			}
   160  		} else {
   161  			imp, err = importer.Import(path)
   162  			if imp == nil && err == nil {
   163  				err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
   164  			}
   165  		}
   166  		// make sure we have a valid package name
   167  		// (errors here can only happen through manipulation of packages after creation)
   168  		if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
   169  			err = fmt.Errorf("invalid package name: %q", imp.name)
   170  			imp = nil // create fake package below
   171  		}
   172  		if err != nil {
   173  			check.errorf(pos, "could not import %s (%s)", path, err)
   174  			if imp == nil {
   175  				// create a new fake package
   176  				// come up with a sensible package name (heuristic)
   177  				name := path
   178  				if i := len(name); i > 0 && name[i-1] == '/' {
   179  					name = name[:i-1]
   180  				}
   181  				if i := strings.LastIndex(name, "/"); i >= 0 {
   182  					name = name[i+1:]
   183  				}
   184  				imp = NewPackage(path, name)
   185  			}
   186  			// continue to use the package as best as we can
   187  			imp.fake = true // avoid follow-up lookup failures
   188  		}
   189  	}
   190  
   191  	// package should be complete or marked fake, but be cautious
   192  	if imp.complete || imp.fake {
   193  		check.impMap[key] = imp
   194  		return imp
   195  	}
   196  
   197  	// something went wrong (importer may have returned incomplete package without error)
   198  	return nil
   199  }
   200  
   201  // collectObjects collects all file and package objects and inserts them
   202  // into their respective scopes. It also performs imports and associates
   203  // methods with receiver base type names.
   204  func (check *Checker) collectObjects() {
   205  	pkg := check.pkg
   206  
   207  	// pkgImports is the set of packages already imported by any package file seen
   208  	// so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
   209  	// it (pkg.imports may not be empty if we are checking test files incrementally).
   210  	// Note that pkgImports is keyed by package (and thus package path), not by an
   211  	// importKey value. Two different importKey values may map to the same package
   212  	// which is why we cannot use the check.impMap here.
   213  	var pkgImports = make(map[*Package]bool)
   214  	for _, imp := range pkg.imports {
   215  		pkgImports[imp] = true
   216  	}
   217  
   218  	var methods []*Func // list of methods with non-blank _ names
   219  	for fileNo, file := range check.files {
   220  		// The package identifier denotes the current package,
   221  		// but there is no corresponding package object.
   222  		check.recordDef(file.Name, nil)
   223  
   224  		// Use the actual source file extent rather than *ast.File extent since the
   225  		// latter doesn't include comments which appear at the start or end of the file.
   226  		// Be conservative and use the *ast.File extent if we don't have a *token.File.
   227  		pos, end := file.Pos(), file.End()
   228  		if f := check.fset.File(file.Pos()); f != nil {
   229  			pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
   230  		}
   231  		fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo))
   232  		check.recordScope(file, fileScope)
   233  
   234  		// determine file directory, necessary to resolve imports
   235  		// FileName may be "" (typically for tests) in which case
   236  		// we get "." as the directory which is what we would want.
   237  		fileDir := dir(check.fset.Position(file.Name.Pos()).Filename)
   238  
   239  		for _, decl := range file.Decls {
   240  			switch d := decl.(type) {
   241  			case *ast.BadDecl:
   242  				// ignore
   243  
   244  			case *ast.GenDecl:
   245  				var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
   246  				for iota, spec := range d.Specs {
   247  					switch s := spec.(type) {
   248  					case *ast.ImportSpec:
   249  						// import package
   250  						path, err := validatedImportPath(s.Path.Value)
   251  						if err != nil {
   252  							check.errorf(s.Path.Pos(), "invalid import path (%s)", err)
   253  							continue
   254  						}
   255  
   256  						imp := check.importPackage(s.Path.Pos(), path, fileDir)
   257  						if imp == nil {
   258  							continue
   259  						}
   260  
   261  						// add package to list of explicit imports
   262  						// (this functionality is provided as a convenience
   263  						// for clients; it is not needed for type-checking)
   264  						if !pkgImports[imp] {
   265  							pkgImports[imp] = true
   266  							pkg.imports = append(pkg.imports, imp)
   267  						}
   268  
   269  						// local name overrides imported package name
   270  						name := imp.name
   271  						if s.Name != nil {
   272  							name = s.Name.Name
   273  							if path == "C" {
   274  								// match cmd/compile (not prescribed by spec)
   275  								check.errorf(s.Name.Pos(), `cannot rename import "C"`)
   276  								continue
   277  							}
   278  							if name == "init" {
   279  								check.errorf(s.Name.Pos(), "cannot declare init - must be func")
   280  								continue
   281  							}
   282  						}
   283  
   284  						obj := NewPkgName(s.Pos(), pkg, name, imp)
   285  						if s.Name != nil {
   286  							// in a dot-import, the dot represents the package
   287  							check.recordDef(s.Name, obj)
   288  						} else {
   289  							check.recordImplicit(s, obj)
   290  						}
   291  
   292  						if path == "C" {
   293  							// match cmd/compile (not prescribed by spec)
   294  							obj.used = true
   295  						}
   296  
   297  						// add import to file scope
   298  						if name == "." {
   299  							// merge imported scope with file scope
   300  							for _, obj := range imp.scope.elems {
   301  								// A package scope may contain non-exported objects,
   302  								// do not import them!
   303  								if obj.Exported() {
   304  									// TODO(gri) When we import a package, we create
   305  									// a new local package object. We should do the
   306  									// same for each dot-imported object. That way
   307  									// they can have correct position information.
   308  									// (We must not modify their existing position
   309  									// information because the same package - found
   310  									// via Config.Packages - may be dot-imported in
   311  									// another package!)
   312  									check.declare(fileScope, nil, obj, token.NoPos)
   313  								}
   314  							}
   315  							// add position to set of dot-import positions for this file
   316  							// (this is only needed for "imported but not used" errors)
   317  							check.addUnusedDotImport(fileScope, imp, s.Pos())
   318  						} else {
   319  							// declare imported package object in file scope
   320  							check.declare(fileScope, nil, obj, token.NoPos)
   321  						}
   322  
   323  					case *ast.ValueSpec:
   324  						switch d.Tok {
   325  						case token.CONST:
   326  							// determine which initialization expressions to use
   327  							switch {
   328  							case s.Type != nil || len(s.Values) > 0:
   329  								last = s
   330  							case last == nil:
   331  								last = new(ast.ValueSpec) // make sure last exists
   332  							}
   333  
   334  							// declare all constants
   335  							for i, name := range s.Names {
   336  								obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
   337  
   338  								var init ast.Expr
   339  								if i < len(last.Values) {
   340  									init = last.Values[i]
   341  								}
   342  
   343  								d := &declInfo{file: fileScope, typ: last.Type, init: init}
   344  								check.declarePkgObj(name, obj, d)
   345  							}
   346  
   347  							check.arityMatch(s, last)
   348  
   349  						case token.VAR:
   350  							lhs := make([]*Var, len(s.Names))
   351  							// If there's exactly one rhs initializer, use
   352  							// the same declInfo d1 for all lhs variables
   353  							// so that each lhs variable depends on the same
   354  							// rhs initializer (n:1 var declaration).
   355  							var d1 *declInfo
   356  							if len(s.Values) == 1 {
   357  								// The lhs elements are only set up after the for loop below,
   358  								// but that's ok because declareVar only collects the declInfo
   359  								// for a later phase.
   360  								d1 = &declInfo{file: fileScope, lhs: lhs, typ: s.Type, init: s.Values[0]}
   361  							}
   362  
   363  							// declare all variables
   364  							for i, name := range s.Names {
   365  								obj := NewVar(name.Pos(), pkg, name.Name, nil)
   366  								lhs[i] = obj
   367  
   368  								d := d1
   369  								if d == nil {
   370  									// individual assignments
   371  									var init ast.Expr
   372  									if i < len(s.Values) {
   373  										init = s.Values[i]
   374  									}
   375  									d = &declInfo{file: fileScope, typ: s.Type, init: init}
   376  								}
   377  
   378  								check.declarePkgObj(name, obj, d)
   379  							}
   380  
   381  							check.arityMatch(s, nil)
   382  
   383  						default:
   384  							check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
   385  						}
   386  
   387  					case *ast.TypeSpec:
   388  						obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
   389  						check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typ: s.Type, alias: s.Assign.IsValid()})
   390  
   391  					default:
   392  						check.invalidAST(s.Pos(), "unknown ast.Spec node %T", s)
   393  					}
   394  				}
   395  
   396  			case *ast.FuncDecl:
   397  				name := d.Name.Name
   398  				obj := NewFunc(d.Name.Pos(), pkg, name, nil)
   399  				if d.Recv == nil {
   400  					// regular function
   401  					if name == "init" {
   402  						// don't declare init functions in the package scope - they are invisible
   403  						obj.parent = pkg.scope
   404  						check.recordDef(d.Name, obj)
   405  						// init functions must have a body
   406  						if d.Body == nil {
   407  							check.softErrorf(obj.pos, "missing function body")
   408  						}
   409  					} else {
   410  						check.declare(pkg.scope, d.Name, obj, token.NoPos)
   411  					}
   412  				} else {
   413  					// method
   414  					// (Methods with blank _ names are never found; no need to collect
   415  					// them for later type association. They will still be type-checked
   416  					// with all the other functions.)
   417  					if name != "_" {
   418  						methods = append(methods, obj)
   419  					}
   420  					check.recordDef(d.Name, obj)
   421  				}
   422  				info := &declInfo{file: fileScope, fdecl: d}
   423  				check.objMap[obj] = info
   424  				obj.setOrder(uint32(len(check.objMap)))
   425  
   426  			default:
   427  				check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)
   428  			}
   429  		}
   430  	}
   431  
   432  	// verify that objects in package and file scopes have different names
   433  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   434  		for _, obj := range scope.elems {
   435  			if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
   436  				if pkg, ok := obj.(*PkgName); ok {
   437  					check.errorf(alt.Pos(), "%s already declared through import of %s", alt.Name(), pkg.Imported())
   438  					check.reportAltDecl(pkg)
   439  				} else {
   440  					check.errorf(alt.Pos(), "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
   441  					// TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
   442  					check.reportAltDecl(obj)
   443  				}
   444  			}
   445  		}
   446  	}
   447  
   448  	// Now that we have all package scope objects and all methods,
   449  	// associate methods with receiver base type name where possible.
   450  	// Ignore methods that have an invalid receiver. They will be
   451  	// type-checked later, with regular functions.
   452  	if methods == nil {
   453  		return // nothing to do
   454  	}
   455  	check.methods = make(map[*TypeName][]*Func)
   456  	for _, f := range methods {
   457  		fdecl := check.objMap[f].fdecl
   458  		if list := fdecl.Recv.List; len(list) > 0 {
   459  			// f is a method
   460  			// receiver may be of the form T or *T, possibly with parentheses
   461  			typ := unparen(list[0].Type)
   462  			if ptr, _ := typ.(*ast.StarExpr); ptr != nil {
   463  				typ = unparen(ptr.X)
   464  			}
   465  			if base, _ := typ.(*ast.Ident); base != nil {
   466  				// base is a potential base type name; determine
   467  				// "underlying" defined type and associate f with it
   468  				if tname := check.resolveBaseTypeName(base); tname != nil {
   469  					check.methods[tname] = append(check.methods[tname], f)
   470  				}
   471  			}
   472  		}
   473  	}
   474  }
   475  
   476  // resolveBaseTypeName returns the non-alias receiver base type name,
   477  // explicitly declared in the package scope, for the given receiver
   478  // type name; or nil.
   479  func (check *Checker) resolveBaseTypeName(name *ast.Ident) *TypeName {
   480  	var path []*TypeName
   481  	for {
   482  		// name must denote an object found in the current package scope
   483  		// (note that dot-imported objects are not in the package scope!)
   484  		obj := check.pkg.scope.Lookup(name.Name)
   485  		if obj == nil {
   486  			return nil
   487  		}
   488  		// the object must be a type name...
   489  		tname, _ := obj.(*TypeName)
   490  		if tname == nil {
   491  			return nil
   492  		}
   493  
   494  		// ... which we have not seen before
   495  		if check.cycle(tname, path, false) {
   496  			return nil
   497  		}
   498  
   499  		// we're done if tdecl defined tname as a new type
   500  		// (rather than an alias)
   501  		tdecl := check.objMap[tname] // must exist for objects in package scope
   502  		if !tdecl.alias {
   503  			return tname
   504  		}
   505  
   506  		// Otherwise, if tdecl defined an alias for a (possibly parenthesized)
   507  		// type which is not an (unqualified) named type, we're done because
   508  		// receiver base types must be named types declared in this package.
   509  		typ := unparen(tdecl.typ) // a type may be parenthesized
   510  		name, _ = typ.(*ast.Ident)
   511  		if name == nil {
   512  			return nil
   513  		}
   514  
   515  		// continue resolving name
   516  		path = append(path, tname)
   517  	}
   518  }
   519  
   520  // packageObjects typechecks all package objects, but not function bodies.
   521  func (check *Checker) packageObjects() {
   522  	// process package objects in source order for reproducible results
   523  	objList := make([]Object, len(check.objMap))
   524  	i := 0
   525  	for obj := range check.objMap {
   526  		objList[i] = obj
   527  		i++
   528  	}
   529  	sort.Sort(inSourceOrder(objList))
   530  
   531  	// add new methods to already type-checked types (from a prior Checker.Files call)
   532  	for _, obj := range objList {
   533  		if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
   534  			check.addMethodDecls(obj)
   535  		}
   536  	}
   537  
   538  	// pre-allocate space for type declaration paths so that the underlying array is reused
   539  	typePath := make([]*TypeName, 0, 8)
   540  
   541  	// We process non-alias declarations first, in order to avoid situations where
   542  	// the type of an alias declaration is needed before it is available. In general
   543  	// this is still not enough, as it is possible to create sufficiently convoluted
   544  	// recursive type definitions that will cause a type alias to be needed before it
   545  	// is available (see issue #25838 for examples).
   546  	// As an aside, the cmd/compiler suffers from the same problem (#25838).
   547  	var aliasList []*TypeName
   548  	// phase 1
   549  	for _, obj := range objList {
   550  		// If we have a type alias, collect it for the 2nd phase.
   551  		if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].alias {
   552  			aliasList = append(aliasList, tname)
   553  			continue
   554  		}
   555  
   556  		check.objDecl(obj, nil, typePath)
   557  	}
   558  	// phase 2
   559  	for _, obj := range aliasList {
   560  		check.objDecl(obj, nil, typePath)
   561  	}
   562  
   563  	// At this point we may have a non-empty check.methods map; this means that not all
   564  	// entries were deleted at the end of typeDecl because the respective receiver base
   565  	// types were not found. In that case, an error was reported when declaring those
   566  	// methods. We can now safely discard this map.
   567  	check.methods = nil
   568  }
   569  
   570  // inSourceOrder implements the sort.Sort interface.
   571  type inSourceOrder []Object
   572  
   573  func (a inSourceOrder) Len() int           { return len(a) }
   574  func (a inSourceOrder) Less(i, j int) bool { return a[i].order() < a[j].order() }
   575  func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   576  
   577  // processDelayed processes all delayed actions pushed after top.
   578  func (check *Checker) processDelayed(top int) {
   579  	for len(check.delayed) > top {
   580  		i := len(check.delayed) - 1
   581  		f := check.delayed[i]
   582  		check.delayed = check.delayed[:i]
   583  		f() // may append to check.delayed
   584  	}
   585  }
   586  
   587  // unusedImports checks for unused imports.
   588  func (check *Checker) unusedImports() {
   589  	// if function bodies are not checked, packages' uses are likely missing - don't check
   590  	if check.conf.IgnoreFuncBodies {
   591  		return
   592  	}
   593  
   594  	// spec: "It is illegal (...) to directly import a package without referring to
   595  	// any of its exported identifiers. To import a package solely for its side-effects
   596  	// (initialization), use the blank identifier as explicit package name."
   597  
   598  	// check use of regular imported packages
   599  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   600  		for _, obj := range scope.elems {
   601  			if obj, ok := obj.(*PkgName); ok {
   602  				// Unused "blank imports" are automatically ignored
   603  				// since _ identifiers are not entered into scopes.
   604  				if !obj.used {
   605  					path := obj.imported.path
   606  					base := pkgName(path)
   607  					if obj.name == base {
   608  						check.softErrorf(obj.pos, "%q imported but not used", path)
   609  					} else {
   610  						check.softErrorf(obj.pos, "%q imported but not used as %s", path, obj.name)
   611  					}
   612  				}
   613  			}
   614  		}
   615  	}
   616  
   617  	// check use of dot-imported packages
   618  	for _, unusedDotImports := range check.unusedDotImports {
   619  		for pkg, pos := range unusedDotImports {
   620  			check.softErrorf(pos, "%q imported but not used", pkg.path)
   621  		}
   622  	}
   623  }
   624  
   625  // pkgName returns the package name (last element) of an import path.
   626  func pkgName(path string) string {
   627  	if i := strings.LastIndex(path, "/"); i >= 0 {
   628  		path = path[i+1:]
   629  	}
   630  	return path
   631  }
   632  
   633  // dir makes a good-faith attempt to return the directory
   634  // portion of path. If path is empty, the result is ".".
   635  // (Per the go/build package dependency tests, we cannot import
   636  // path/filepath and simply use filepath.Dir.)
   637  func dir(path string) string {
   638  	if i := strings.LastIndexAny(path, `/\`); i > 0 {
   639  		return path[:i]
   640  	}
   641  	// i <= 0
   642  	return "."
   643  }
   644  

View as plain text