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.
11 "go/internal/typeparams"
13 . "internal/types/errors"
20 // A declInfo describes a package-level const, type, var, or func declaration.
21 type declInfo struct {
22 file *Scope // scope of file containing this declaration
23 lhs []*Var // lhs of n:1 variable declarations, or nil
24 vtyp ast.Expr // type, or nil (for const and var declarations only)
25 init ast.Expr // init/orig expression, or nil (for const and var declarations only)
26 inherited bool // if set, the init expression is inherited from a previous constant declaration
27 tdecl *ast.TypeSpec // type declaration, or nil
28 fdecl *ast.FuncDecl // func declaration, or nil
30 // The deps field tracks initialization expression dependencies.
31 deps map[Object]bool // lazily initialized
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
40 // addDep adds obj to the set of objects d's init expression depends on.
41 func (d *declInfo) addDep(obj Object) {
44 m = make(map[Object]bool)
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) {
61 const code = WrongAssignCount
63 case init == nil && r == 0:
64 // var decl w/o init expr
66 check.error(s, code, "missing type or init expr")
69 if l < len(s.Values) {
72 check.errorf(n, code, "extra init expr %s", n)
73 // TODO(gri) avoid declared and not used error here
75 // init exprs "inherited"
76 check.errorf(s, code, "extra init expr at %s", check.fset.Position(init.Pos()))
77 // TODO(gri) avoid declared and not used error here
79 case l > r && (init != nil || r != 1):
81 check.errorf(n, code, "missing init expr for %s", n)
85 func validatedImportPath(path string) (string, error) {
86 s, err := strconv.Unquote(path)
91 return "", fmt.Errorf("empty string")
93 const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
95 if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
96 return s, fmt.Errorf("invalid character %#U", r)
102 // declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
103 // and updates check.objMap. The object must not be a function or method.
104 func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
105 assert(ident.Name == obj.Name())
107 // spec: "A package-scope or file-scope identifier with name init
108 // may only be declared to be a function with this (func()) signature."
109 if ident.Name == "init" {
110 check.error(ident, InvalidInitDecl, "cannot declare init - must be func")
114 // spec: "The main package must have package name main and declare
115 // a function main that takes no arguments and returns no value."
116 if ident.Name == "main" && check.pkg.name == "main" {
117 check.error(ident, InvalidMainDecl, "cannot declare main - must be func")
121 check.declare(check.pkg.scope, ident, obj, nopos)
122 check.objMap[obj] = d
123 obj.setOrder(uint32(len(check.objMap)))
126 // filename returns a filename suitable for debugging output.
127 func (check *Checker) filename(fileNo int) string {
128 file := check.files[fileNo]
129 if pos := file.Pos(); pos.IsValid() {
130 return check.fset.File(pos).Name()
132 return fmt.Sprintf("file[%d]", fileNo)
135 func (check *Checker) importPackage(at positioner, path, dir string) *Package {
136 // If we already have a package for the given (path, dir)
137 // pair, use it instead of doing a full import.
138 // Checker.impMap only caches packages that are marked Complete
139 // or fake (dummy packages for failed imports). Incomplete but
140 // non-fake packages do require an import to complete them.
141 key := importKey{path, dir}
142 imp := check.impMap[key]
147 // no package yet => import it
148 if path == "C" && (check.conf.FakeImportC || check.conf.go115UsesCgo) {
149 imp = NewPackage("C", "C")
150 imp.fake = true // package scope is not populated
151 imp.cgo = check.conf.go115UsesCgo
155 if importer := check.conf.Importer; importer == nil {
156 err = fmt.Errorf("Config.Importer not installed")
157 } else if importerFrom, ok := importer.(ImporterFrom); ok {
158 imp, err = importerFrom.ImportFrom(path, dir, 0)
159 if imp == nil && err == nil {
160 err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
163 imp, err = importer.Import(path)
164 if imp == nil && err == nil {
165 err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
168 // make sure we have a valid package name
169 // (errors here can only happen through manipulation of packages after creation)
170 if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
171 err = fmt.Errorf("invalid package name: %q", imp.name)
172 imp = nil // create fake package below
175 check.errorf(at, BrokenImport, "could not import %s (%s)", path, err)
177 // create a new fake package
178 // come up with a sensible package name (heuristic)
180 if i := len(name); i > 0 && name[i-1] == '/' {
183 if i := strings.LastIndex(name, "/"); i >= 0 {
186 imp = NewPackage(path, name)
188 // continue to use the package as best as we can
189 imp.fake = true // avoid follow-up lookup failures
193 // package should be complete or marked fake, but be cautious
194 if imp.complete || imp.fake {
195 check.impMap[key] = imp
196 // Once we've formatted an error message, keep the pkgPathMap
197 // up-to-date on subsequent imports. It is used for package
198 // qualification in error messages.
199 if check.pkgPathMap != nil {
200 check.markImports(imp)
205 // something went wrong (importer may have returned incomplete package without error)
209 // collectObjects collects all file and package objects and inserts them
210 // into their respective scopes. It also performs imports and associates
211 // methods with receiver base type names.
212 func (check *Checker) collectObjects() {
215 // pkgImports is the set of packages already imported by any package file seen
216 // so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
217 // it (pkg.imports may not be empty if we are checking test files incrementally).
218 // Note that pkgImports is keyed by package (and thus package path), not by an
219 // importKey value. Two different importKey values may map to the same package
220 // which is why we cannot use the check.impMap here.
221 var pkgImports = make(map[*Package]bool)
222 for _, imp := range pkg.imports {
223 pkgImports[imp] = true
226 type methodInfo struct {
228 ptr bool // true if pointer receiver
229 recv *ast.Ident // receiver type name
231 var methods []methodInfo // collected methods with valid receivers and non-blank _ names
232 var fileScopes []*Scope
233 for fileNo, file := range check.files {
234 // The package identifier denotes the current package,
235 // but there is no corresponding package object.
236 check.recordDef(file.Name, nil)
238 // Use the actual source file extent rather than *ast.File extent since the
239 // latter doesn't include comments which appear at the start or end of the file.
240 // Be conservative and use the *ast.File extent if we don't have a *token.File.
241 pos, end := file.Pos(), file.End()
242 if f := check.fset.File(file.Pos()); f != nil {
243 pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
245 fileScope := NewScope(pkg.scope, pos, end, check.filename(fileNo))
246 fileScopes = append(fileScopes, fileScope)
247 check.recordScope(file, fileScope)
249 // determine file directory, necessary to resolve imports
250 // FileName may be "" (typically for tests) in which case
251 // we get "." as the directory which is what we would want.
252 fileDir := dir(check.fset.Position(file.Name.Pos()).Filename)
254 check.walkDecls(file.Decls, func(d decl) {
255 switch d := d.(type) {
258 if d.spec.Path.Value == "" {
259 return // error reported by parser
261 path, err := validatedImportPath(d.spec.Path.Value)
263 check.errorf(d.spec.Path, BadImportPath, "invalid import path (%s)", err)
267 imp := check.importPackage(d.spec.Path, path, fileDir)
272 // local name overrides imported package name
274 if d.spec.Name != nil {
275 name = d.spec.Name.Name
277 // match 1.17 cmd/compile (not prescribed by spec)
278 check.error(d.spec.Name, ImportCRenamed, `cannot rename import "C"`)
284 check.error(d.spec, InvalidInitDecl, "cannot import package as init - init must be a func")
288 // add package to list of explicit imports
289 // (this functionality is provided as a convenience
290 // for clients; it is not needed for type-checking)
291 if !pkgImports[imp] {
292 pkgImports[imp] = true
293 pkg.imports = append(pkg.imports, imp)
296 pkgName := NewPkgName(d.spec.Pos(), pkg, name, imp)
297 if d.spec.Name != nil {
298 // in a dot-import, the dot represents the package
299 check.recordDef(d.spec.Name, pkgName)
301 check.recordImplicit(d.spec, pkgName)
305 // match 1.17 cmd/compile (not prescribed by spec)
309 // add import to file scope
310 check.imports = append(check.imports, pkgName)
313 if check.dotImportMap == nil {
314 check.dotImportMap = make(map[dotImportKey]*PkgName)
316 // merge imported scope with file scope
317 for name, obj := range imp.scope.elems {
318 // Note: Avoid eager resolve(name, obj) here, so we only
319 // resolve dot-imported objects as needed.
321 // A package scope may contain non-exported objects,
322 // do not import them!
323 if token.IsExported(name) {
324 // declare dot-imported object
325 // (Do not use check.declare because it modifies the object
326 // via Object.setScopePos, which leads to a race condition;
327 // the object may be imported into more than one file scope
328 // concurrently. See go.dev/issue/32154.)
329 if alt := fileScope.Lookup(name); alt != nil {
330 check.errorf(d.spec.Name, DuplicateDecl, "%s redeclared in this block", alt.Name())
331 check.reportAltDecl(alt)
333 fileScope.insert(name, obj)
334 check.dotImportMap[dotImportKey{fileScope, name}] = pkgName
339 // declare imported package object in file scope
340 // (no need to provide s.Name since we called check.recordDef earlier)
341 check.declare(fileScope, nil, pkgName, nopos)
344 // declare all constants
345 for i, name := range d.spec.Names {
346 obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(d.iota)))
353 d := &declInfo{file: fileScope, vtyp: d.typ, init: init, inherited: d.inherited}
354 check.declarePkgObj(name, obj, d)
358 lhs := make([]*Var, len(d.spec.Names))
359 // If there's exactly one rhs initializer, use
360 // the same declInfo d1 for all lhs variables
361 // so that each lhs variable depends on the same
362 // rhs initializer (n:1 var declaration).
364 if len(d.spec.Values) == 1 {
365 // The lhs elements are only set up after the for loop below,
366 // but that's ok because declareVar only collects the declInfo
367 // for a later phase.
368 d1 = &declInfo{file: fileScope, lhs: lhs, vtyp: d.spec.Type, init: d.spec.Values[0]}
371 // declare all variables
372 for i, name := range d.spec.Names {
373 obj := NewVar(name.Pos(), pkg, name.Name, nil)
378 // individual assignments
380 if i < len(d.spec.Values) {
381 init = d.spec.Values[i]
383 di = &declInfo{file: fileScope, vtyp: d.spec.Type, init: init}
386 check.declarePkgObj(name, obj, di)
389 _ = d.spec.TypeParams.NumFields() != 0 && check.verifyVersionf(d.spec.TypeParams.List[0], go1_18, "type parameter")
390 obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
391 check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, tdecl: d.spec})
393 name := d.decl.Name.Name
394 obj := NewFunc(d.decl.Name.Pos(), pkg, name, nil)
395 hasTParamError := false // avoid duplicate type parameter errors
396 if d.decl.Recv.NumFields() == 0 {
398 if d.decl.Recv != nil {
399 check.error(d.decl.Recv, BadRecv, "method has no receiver")
402 if name == "init" || (name == "main" && check.pkg.name == "main") {
403 code := InvalidInitDecl
405 code = InvalidMainDecl
407 if d.decl.Type.TypeParams.NumFields() != 0 {
408 check.softErrorf(d.decl.Type.TypeParams.List[0], code, "func %s must have no type parameters", name)
409 hasTParamError = true
411 if t := d.decl.Type; t.Params.NumFields() != 0 || t.Results != nil {
412 // TODO(rFindley) Should this be a hard error?
413 check.softErrorf(d.decl.Name, code, "func %s must have no arguments and no return values", name)
417 // don't declare init functions in the package scope - they are invisible
418 obj.parent = pkg.scope
419 check.recordDef(d.decl.Name, obj)
420 // init functions must have a body
421 if d.decl.Body == nil {
422 // TODO(gri) make this error message consistent with the others above
423 check.softErrorf(obj, MissingInitBody, "missing function body")
426 check.declare(pkg.scope, d.decl.Name, obj, nopos)
431 // TODO(rFindley) earlier versions of this code checked that methods
432 // have no type parameters, but this is checked later
433 // when type checking the function type. Confirm that
434 // we don't need to check tparams here.
436 ptr, recv, _ := check.unpackRecv(d.decl.Recv.List[0].Type, false)
437 // (Methods with invalid receiver cannot be associated to a type, and
438 // methods with blank _ names are never found; no need to collect any
439 // of them. They will still be type-checked with all the other functions.)
440 if recv != nil && name != "_" {
441 methods = append(methods, methodInfo{obj, ptr, recv})
443 check.recordDef(d.decl.Name, obj)
445 _ = d.decl.Type.TypeParams.NumFields() != 0 && !hasTParamError && check.verifyVersionf(d.decl.Type.TypeParams.List[0], go1_18, "type parameter")
446 info := &declInfo{file: fileScope, fdecl: d.decl}
447 // Methods are not package-level objects but we still track them in the
448 // object map so that we can handle them like regular functions (if the
449 // receiver is invalid); also we need their fdecl info when associating
450 // them with their receiver base type, below.
451 check.objMap[obj] = info
452 obj.setOrder(uint32(len(check.objMap)))
457 // verify that objects in package and file scopes have different names
458 for _, scope := range fileScopes {
459 for name, obj := range scope.elems {
460 if alt := pkg.scope.Lookup(name); alt != nil {
461 obj = resolve(name, obj)
462 if pkg, ok := obj.(*PkgName); ok {
463 check.errorf(alt, DuplicateDecl, "%s already declared through import of %s", alt.Name(), pkg.Imported())
464 check.reportAltDecl(pkg)
466 check.errorf(alt, DuplicateDecl, "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
467 // TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
468 check.reportAltDecl(obj)
474 // Now that we have all package scope objects and all methods,
475 // associate methods with receiver base type name where possible.
476 // Ignore methods that have an invalid receiver. They will be
477 // type-checked later, with regular functions.
479 return // nothing to do
481 check.methods = make(map[*TypeName][]*Func)
482 for i := range methods {
484 // Determine the receiver base type and associate m with it.
485 ptr, base := check.resolveBaseTypeName(m.ptr, m.recv)
487 m.obj.hasPtrRecv_ = ptr
488 check.methods[base] = append(check.methods[base], m.obj)
493 // unpackRecv unpacks a receiver type and returns its components: ptr indicates whether
494 // rtyp is a pointer receiver, rname is the receiver type name, and tparams are its
495 // type parameters, if any. The type parameters are only unpacked if unpackParams is
496 // set. If rname is nil, the receiver is unusable (i.e., the source has a bug which we
497 // cannot easily work around).
498 func (check *Checker) unpackRecv(rtyp ast.Expr, unpackParams bool) (ptr bool, rname *ast.Ident, tparams []*ast.Ident) {
499 L: // unpack receiver type
500 // This accepts invalid receivers such as ***T and does not
501 // work for other invalid receivers, but we don't care. The
502 // validity of receiver expressions is checked elsewhere.
504 switch t := rtyp.(type) {
515 // unpack type parameters, if any
517 case *ast.IndexExpr, *ast.IndexListExpr:
518 ix := typeparams.UnpackIndexExpr(rtyp)
521 for _, arg := range ix.Indices {
523 switch arg := arg.(type) {
527 // ignore - error already reported by parser
529 check.error(ix.Orig, InvalidSyntaxTree, "parameterized receiver contains nil parameters")
531 check.errorf(arg, BadDecl, "receiver type parameter %s must be an identifier", arg)
534 par = &ast.Ident{NamePos: arg.Pos(), Name: "_"}
536 tparams = append(tparams, par)
541 // unpack receiver name
542 if name, _ := rtyp.(*ast.Ident); name != nil {
549 // resolveBaseTypeName returns the non-alias base type name for typ, and whether
550 // there was a pointer indirection to get to it. The base type name must be declared
551 // in package scope, and there can be at most one pointer indirection. If no such type
552 // name exists, the returned base is nil.
553 func (check *Checker) resolveBaseTypeName(seenPtr bool, name *ast.Ident) (ptr bool, base *TypeName) {
554 // Algorithm: Starting from a type expression, which may be a name,
555 // we follow that type through alias declarations until we reach a
556 // non-alias type name. If we encounter anything but pointer types or
557 // parentheses we're done. If we encounter more than one pointer type
560 var seen map[*TypeName]bool
561 var typ ast.Expr = name
565 // check if we have a pointer type
566 if pexpr, _ := typ.(*ast.StarExpr); pexpr != nil {
567 // if we've already seen a pointer, we're done
572 typ = unparen(pexpr.X) // continue with pointer base type
575 // typ must be a name
576 name, _ := typ.(*ast.Ident)
581 // name must denote an object found in the current package scope
582 // (note that dot-imported objects are not in the package scope!)
583 obj := check.pkg.scope.Lookup(name.Name)
588 // the object must be a type name...
589 tname, _ := obj.(*TypeName)
594 // ... which we have not seen before
599 // we're done if tdecl defined tname as a new type
600 // (rather than an alias)
601 tdecl := check.objMap[tname].tdecl // must exist for objects in package scope
602 if !tdecl.Assign.IsValid() {
606 // otherwise, continue resolving
609 seen = make(map[*TypeName]bool)
615 // packageObjects typechecks all package objects, but not function bodies.
616 func (check *Checker) packageObjects() {
617 // process package objects in source order for reproducible results
618 objList := make([]Object, len(check.objMap))
620 for obj := range check.objMap {
624 sort.Sort(inSourceOrder(objList))
626 // add new methods to already type-checked types (from a prior Checker.Files call)
627 for _, obj := range objList {
628 if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
629 check.collectMethods(obj)
633 // We process non-alias type declarations first, followed by alias declarations,
634 // and then everything else. This appears to avoid most situations where the type
635 // of an alias is needed before it is available.
636 // There may still be cases where this is not good enough (see also go.dev/issue/25838).
637 // In those cases Checker.ident will report an error ("invalid use of type alias").
638 var aliasList []*TypeName
639 var othersList []Object // everything that's not a type
640 // phase 1: non-alias type declarations
641 for _, obj := range objList {
642 if tname, _ := obj.(*TypeName); tname != nil {
643 if check.objMap[tname].tdecl.Assign.IsValid() {
644 aliasList = append(aliasList, tname)
646 check.objDecl(obj, nil)
649 othersList = append(othersList, obj)
652 // phase 2: alias type declarations
653 for _, obj := range aliasList {
654 check.objDecl(obj, nil)
656 // phase 3: all other declarations
657 for _, obj := range othersList {
658 check.objDecl(obj, nil)
661 // At this point we may have a non-empty check.methods map; this means that not all
662 // entries were deleted at the end of typeDecl because the respective receiver base
663 // types were not found. In that case, an error was reported when declaring those
664 // methods. We can now safely discard this map.
668 // inSourceOrder implements the sort.Sort interface.
669 type inSourceOrder []Object
671 func (a inSourceOrder) Len() int { return len(a) }
672 func (a inSourceOrder) Less(i, j int) bool { return a[i].order() < a[j].order() }
673 func (a inSourceOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
675 // unusedImports checks for unused imports.
676 func (check *Checker) unusedImports() {
677 // If function bodies are not checked, packages' uses are likely missing - don't check.
678 if check.conf.IgnoreFuncBodies {
682 // spec: "It is illegal (...) to directly import a package without referring to
683 // any of its exported identifiers. To import a package solely for its side-effects
684 // (initialization), use the blank identifier as explicit package name."
686 for _, obj := range check.imports {
687 if !obj.used && obj.name != "_" {
688 check.errorUnusedPkg(obj)
693 func (check *Checker) errorUnusedPkg(obj *PkgName) {
694 // If the package was imported with a name other than the final
695 // import path element, show it explicitly in the error message.
696 // Note that this handles both renamed imports and imports of
697 // packages containing unconventional package declarations.
698 // Note that this uses / always, even on Windows, because Go import
699 // paths always use forward slashes.
700 path := obj.imported.path
702 if i := strings.LastIndex(elem, "/"); i >= 0 {
705 if obj.name == "" || obj.name == "." || obj.name == elem {
706 check.softErrorf(obj, UnusedImport, "%q imported and not used", path)
708 check.softErrorf(obj, UnusedImport, "%q imported as %s and not used", path, obj.name)
712 // dir makes a good-faith attempt to return the directory
713 // portion of path. If path is empty, the result is ".".
714 // (Per the go/build package dependency tests, we cannot import
715 // path/filepath and simply use filepath.Dir.)
716 func dir(path string) string {
717 if i := strings.LastIndexAny(path, `/\`); i > 0 {