]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/resolver.go
go/types, types2: introduce _Alias type node
[gostls13.git] / src / go / types / resolver.go
index e4411592e82a0ae6c77ce570d8106a7828ceb155..8b6a1b4b79e0717021a114ab7484aaf021817acd 100644 (file)
@@ -8,7 +8,9 @@ import (
        "fmt"
        "go/ast"
        "go/constant"
+       "go/internal/typeparams"
        "go/token"
+       . "internal/types/errors"
        "sort"
        "strconv"
        "strings"
@@ -19,11 +21,11 @@ import (
 type declInfo struct {
        file      *Scope        // scope of file containing this declaration
        lhs       []*Var        // lhs of n:1 variable declarations, or nil
-       typ       ast.Expr      // type, or nil
-       init      ast.Expr      // init/orig expression, or nil
+       vtyp      ast.Expr      // type, or nil (for const and var declarations only)
+       init      ast.Expr      // init/orig expression, or nil (for const and var declarations only)
        inherited bool          // if set, the init expression is inherited from a previous constant declaration
+       tdecl     *ast.TypeSpec // type declaration, or nil
        fdecl     *ast.FuncDecl // func declaration, or nil
-       aliasPos  token.Pos     // If valid, the decl is a type alias and aliasPos is the position of '='.
 
        // The deps field tracks initialization expression dependencies.
        deps map[Object]bool // lazily initialized
@@ -56,23 +58,23 @@ func (check *Checker) arityMatch(s, init *ast.ValueSpec) {
                r = len(init.Values)
        }
 
-       const code = _WrongAssignCount
+       const code = WrongAssignCount
        switch {
        case init == nil && r == 0:
                // var decl w/o init expr
                if s.Type == nil {
-                       check.errorf(s, code, "missing type or init expr")
+                       check.error(s, code, "missing type or init expr")
                }
        case l < r:
                if l < len(s.Values) {
                        // init exprs from s
                        n := s.Values[l]
                        check.errorf(n, code, "extra init expr %s", n)
-                       // TODO(gri) avoid declared but not used error here
+                       // TODO(gri) avoid declared and not used error here
                } else {
                        // init exprs "inherited"
                        check.errorf(s, code, "extra init expr at %s", check.fset.Position(init.Pos()))
-                       // TODO(gri) avoid declared but not used error here
+                       // TODO(gri) avoid declared and not used error here
                }
        case l > r && (init != nil || r != 1):
                n := s.Names[r]
@@ -105,18 +107,18 @@ func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
        // spec: "A package-scope or file-scope identifier with name init
        // may only be declared to be a function with this (func()) signature."
        if ident.Name == "init" {
-               check.errorf(ident, _InvalidInitDecl, "cannot declare init - must be func")
+               check.error(ident, InvalidInitDecl, "cannot declare init - must be func")
                return
        }
 
        // spec: "The main package must have package name main and declare
        // a function main that takes no arguments and returns no value."
        if ident.Name == "main" && check.pkg.name == "main" {
-               check.errorf(ident, _InvalidMainDecl, "cannot declare main - must be func")
+               check.error(ident, InvalidMainDecl, "cannot declare main - must be func")
                return
        }
 
-       check.declare(check.pkg.scope, ident, obj, token.NoPos)
+       check.declare(check.pkg.scope, ident, obj, nopos)
        check.objMap[obj] = d
        obj.setOrder(uint32(len(check.objMap)))
 }
@@ -130,7 +132,7 @@ func (check *Checker) filename(fileNo int) string {
        return fmt.Sprintf("file[%d]", fileNo)
 }
 
-func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
+func (check *Checker) importPackage(at positioner, path, dir string) *Package {
        // If we already have a package for the given (path, dir)
        // pair, use it instead of doing a full import.
        // Checker.impMap only caches packages that are marked Complete
@@ -170,7 +172,7 @@ func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
                        imp = nil // create fake package below
                }
                if err != nil {
-                       check.errorf(atPos(pos), _BrokenImport, "could not import %s (%s)", path, err)
+                       check.errorf(atBrokenImport, "could not import %s (%s)", path, err)
                        if imp == nil {
                                // create a new fake package
                                // come up with a sensible package name (heuristic)
@@ -191,7 +193,12 @@ func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
        // package should be complete or marked fake, but be cautious
        if imp.complete || imp.fake {
                check.impMap[key] = imp
-               check.pkgCnt[imp.name]++
+               // Once we've formatted an error message, keep the pkgPathMap
+               // up-to-date on subsequent imports. It is used for package
+               // qualification in error messages.
+               if check.pkgPathMap != nil {
+                       check.markImports(imp)
+               }
                return imp
        }
 
@@ -216,7 +223,13 @@ func (check *Checker) collectObjects() {
                pkgImports[imp] = true
        }
 
-       var methods []*Func // list of methods with non-blank _ names
+       type methodInfo struct {
+               obj  *Func      // method
+               ptr  bool       // true if pointer receiver
+               recv *ast.Ident // receiver type name
+       }
+       var methods []methodInfo // collected methods with valid receivers and non-blank _ names
+       var fileScopes []*Scope
        for fileNo, file := range check.files {
                // The package identifier denotes the current package,
                // but there is no corresponding package object.
@@ -229,7 +242,8 @@ func (check *Checker) collectObjects() {
                if f := check.fset.File(file.Pos()); f != nil {
                        pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
                }
-               fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo))
+               fileScope := NewScope(pkg.scope, pos, end, check.filename(fileNo))
+               fileScopes = append(fileScopes, fileScope)
                check.recordScope(file, fileScope)
 
                // determine file directory, necessary to resolve imports
@@ -241,13 +255,16 @@ func (check *Checker) collectObjects() {
                        switch d := d.(type) {
                        case importDecl:
                                // import package
+                               if d.spec.Path.Value == "" {
+                                       return // error reported by parser
+                               }
                                path, err := validatedImportPath(d.spec.Path.Value)
                                if err != nil {
-                                       check.errorf(d.spec.Path, _BadImportPath, "invalid import path (%s)", err)
+                                       check.errorf(d.spec.Path, BadImportPath, "invalid import path (%s)", err)
                                        return
                                }
 
-                               imp := check.importPackage(d.spec.Path.Pos(), path, fileDir)
+                               imp := check.importPackage(d.spec.Path, path, fileDir)
                                if imp == nil {
                                        return
                                }
@@ -257,14 +274,14 @@ func (check *Checker) collectObjects() {
                                if d.spec.Name != nil {
                                        name = d.spec.Name.Name
                                        if path == "C" {
-                                               // match cmd/compile (not prescribed by spec)
-                                               check.errorf(d.spec.Name, _ImportCRenamed, `cannot rename import "C"`)
+                                               // match 1.17 cmd/compile (not prescribed by spec)
+                                               check.error(d.spec.Name, ImportCRenamed, `cannot rename import "C"`)
                                                return
                                        }
                                }
 
                                if name == "init" {
-                                       check.errorf(d.spec.Name, _InvalidInitDecl, "cannot import package as init - init must be a func")
+                                       check.error(d.spec, InvalidInitDecl, "cannot import package as init - init must be a func")
                                        return
                                }
 
@@ -284,8 +301,8 @@ func (check *Checker) collectObjects() {
                                        check.recordImplicit(d.spec, pkgName)
                                }
 
-                               if path == "C" {
-                                       // match cmd/compile (not prescribed by spec)
+                               if imp.fake {
+                                       // match 1.17 cmd/compile (not prescribed by spec)
                                        pkgName.used = true
                                }
 
@@ -297,27 +314,31 @@ func (check *Checker) collectObjects() {
                                                check.dotImportMap = make(map[dotImportKey]*PkgName)
                                        }
                                        // merge imported scope with file scope
-                                       for _, obj := range imp.scope.elems {
+                                       for name, obj := range imp.scope.elems {
+                                               // Note: Avoid eager resolve(name, obj) here, so we only
+                                               // resolve dot-imported objects as needed.
+
                                                // A package scope may contain non-exported objects,
                                                // do not import them!
-                                               if obj.Exported() {
+                                               if token.IsExported(name) {
                                                        // declare dot-imported object
                                                        // (Do not use check.declare because it modifies the object
                                                        // via Object.setScopePos, which leads to a race condition;
                                                        // the object may be imported into more than one file scope
-                                                       // concurrently. See issue #32154.)
-                                                       if alt := fileScope.Insert(obj); alt != nil {
-                                                               check.errorf(d.spec.Name, _DuplicateDecl, "%s redeclared in this block", obj.Name())
+                                                       // concurrently. See go.dev/issue/32154.)
+                                                       if alt := fileScope.Lookup(name); alt != nil {
+                                                               check.errorf(d.spec.Name, DuplicateDecl, "%s redeclared in this block", alt.Name())
                                                                check.reportAltDecl(alt)
                                                        } else {
-                                                               check.dotImportMap[dotImportKey{fileScope, obj}] = pkgName
+                                                               fileScope.insert(name, obj)
+                                                               check.dotImportMap[dotImportKey{fileScope, name}] = pkgName
                                                        }
                                                }
                                        }
                                } else {
                                        // declare imported package object in file scope
                                        // (no need to provide s.Name since we called check.recordDef earlier)
-                                       check.declare(fileScope, nil, pkgName, token.NoPos)
+                                       check.declare(fileScope, nil, pkgName, nopos)
                                }
                        case constDecl:
                                // declare all constants
@@ -329,7 +350,7 @@ func (check *Checker) collectObjects() {
                                                init = d.init[i]
                                        }
 
-                                       d := &declInfo{file: fileScope, typ: d.typ, init: init, inherited: d.inherited}
+                                       d := &declInfo{file: fileScope, vtyp: d.typ, init: init, inherited: d.inherited}
                                        check.declarePkgObj(name, obj, d)
                                }
 
@@ -344,7 +365,7 @@ func (check *Checker) collectObjects() {
                                        // The lhs elements are only set up after the for loop below,
                                        // but that's ok because declareVar only collects the declInfo
                                        // for a later phase.
-                                       d1 = &declInfo{file: fileScope, lhs: lhs, typ: d.spec.Type, init: d.spec.Values[0]}
+                                       d1 = &declInfo{file: fileScope, lhs: lhs, vtyp: d.spec.Type, init: d.spec.Values[0]}
                                }
 
                                // declare all variables
@@ -359,41 +380,70 @@ func (check *Checker) collectObjects() {
                                                if i < len(d.spec.Values) {
                                                        init = d.spec.Values[i]
                                                }
-                                               di = &declInfo{file: fileScope, typ: d.spec.Type, init: init}
+                                               di = &declInfo{file: fileScope, vtyp: d.spec.Type, init: init}
                                        }
 
                                        check.declarePkgObj(name, obj, di)
                                }
                        case typeDecl:
+                               _ = d.spec.TypeParams.NumFields() != 0 && check.verifyVersionf(d.spec.TypeParams.List[0], go1_18, "type parameter")
                                obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
-                               check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, typ: d.spec.Type, aliasPos: d.spec.Assign})
+                               check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, tdecl: d.spec})
                        case funcDecl:
-                               info := &declInfo{file: fileScope, fdecl: d.decl}
                                name := d.decl.Name.Name
                                obj := NewFunc(d.decl.Name.Pos(), pkg, name, nil)
-                               if d.decl.Recv == nil {
+                               hasTParamError := false // avoid duplicate type parameter errors
+                               if d.decl.Recv.NumFields() == 0 {
                                        // regular function
+                                       if d.decl.Recv != nil {
+                                               check.error(d.decl.Recv, BadRecv, "method has no receiver")
+                                               // treat as function
+                                       }
+                                       if name == "init" || (name == "main" && check.pkg.name == "main") {
+                                               code := InvalidInitDecl
+                                               if name == "main" {
+                                                       code = InvalidMainDecl
+                                               }
+                                               if d.decl.Type.TypeParams.NumFields() != 0 {
+                                                       check.softErrorf(d.decl.Type.TypeParams.List[0], code, "func %s must have no type parameters", name)
+                                                       hasTParamError = true
+                                               }
+                                               if t := d.decl.Type; t.Params.NumFields() != 0 || t.Results != nil {
+                                                       // TODO(rFindley) Should this be a hard error?
+                                                       check.softErrorf(d.decl.Name, code, "func %s must have no arguments and no return values", name)
+                                               }
+                                       }
                                        if name == "init" {
                                                // don't declare init functions in the package scope - they are invisible
                                                obj.parent = pkg.scope
                                                check.recordDef(d.decl.Name, obj)
                                                // init functions must have a body
                                                if d.decl.Body == nil {
-                                                       check.softErrorf(obj, _MissingInitBody, "missing function body")
+                                                       // TODO(gri) make this error message consistent with the others above
+                                                       check.softErrorf(obj, MissingInitBody, "missing function body")
                                                }
                                        } else {
-                                               check.declare(pkg.scope, d.decl.Name, obj, token.NoPos)
+                                               check.declare(pkg.scope, d.decl.Name, obj, nopos)
                                        }
                                } else {
                                        // method
-                                       // (Methods with blank _ names are never found; no need to collect
-                                       // them for later type association. They will still be type-checked
-                                       // with all the other functions.)
-                                       if name != "_" {
-                                               methods = append(methods, obj)
+
+                                       // TODO(rFindley) earlier versions of this code checked that methods
+                                       //                have no type parameters, but this is checked later
+                                       //                when type checking the function type. Confirm that
+                                       //                we don't need to check tparams here.
+
+                                       ptr, recv, _ := check.unpackRecv(d.decl.Recv.List[0].Type, false)
+                                       // (Methods with invalid receiver cannot be associated to a type, and
+                                       // methods with blank _ names are never found; no need to collect any
+                                       // of them. They will still be type-checked with all the other functions.)
+                                       if recv != nil && name != "_" {
+                                               methods = append(methods, methodInfo{obj, ptr, recv})
                                        }
                                        check.recordDef(d.decl.Name, obj)
                                }
+                               _ = d.decl.Type.TypeParams.NumFields() != 0 && !hasTParamError && check.verifyVersionf(d.decl.Type.TypeParams.List[0], go1_18, "type parameter")
+                               info := &declInfo{file: fileScope, fdecl: d.decl}
                                // Methods are not package-level objects but we still track them in the
                                // object map so that we can handle them like regular functions (if the
                                // receiver is invalid); also we need their fdecl info when associating
@@ -405,14 +455,15 @@ func (check *Checker) collectObjects() {
        }
 
        // verify that objects in package and file scopes have different names
-       for _, scope := range check.pkg.scope.children /* file scopes */ {
-               for _, obj := range scope.elems {
-                       if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
+       for _, scope := range fileScopes {
+               for name, obj := range scope.elems {
+                       if alt := pkg.scope.Lookup(name); alt != nil {
+                               obj = resolve(name, obj)
                                if pkg, ok := obj.(*PkgName); ok {
-                                       check.errorf(alt, _DuplicateDecl, "%s already declared through import of %s", alt.Name(), pkg.Imported())
+                                       check.errorf(alt, DuplicateDecl, "%s already declared through import of %s", alt.Name(), pkg.Imported())
                                        check.reportAltDecl(pkg)
                                } else {
-                                       check.errorf(alt, _DuplicateDecl, "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
+                                       check.errorf(alt, DuplicateDecl, "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
                                        // TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
                                        check.reportAltDecl(obj)
                                }
@@ -428,32 +479,88 @@ func (check *Checker) collectObjects() {
                return // nothing to do
        }
        check.methods = make(map[*TypeName][]*Func)
-       for _, f := range methods {
-               fdecl := check.objMap[f].fdecl
-               if list := fdecl.Recv.List; len(list) > 0 {
-                       // f is a method.
-                       // Determine the receiver base type and associate f with it.
-                       ptr, base := check.resolveBaseTypeName(list[0].Type)
-                       if base != nil {
-                               f.hasPtrRecv = ptr
-                               check.methods[base] = append(check.methods[base], f)
+       for i := range methods {
+               m := &methods[i]
+               // Determine the receiver base type and associate m with it.
+               ptr, base := check.resolveBaseTypeName(m.ptr, m.recv, fileScopes)
+               if base != nil {
+                       m.obj.hasPtrRecv_ = ptr
+                       check.methods[base] = append(check.methods[base], m.obj)
+               }
+       }
+}
+
+// unpackRecv unpacks a receiver type and returns its components: ptr indicates whether
+// rtyp is a pointer receiver, rname is the receiver type name, and tparams are its
+// type parameters, if any. The type parameters are only unpacked if unpackParams is
+// set. If rname is nil, the receiver is unusable (i.e., the source has a bug which we
+// cannot easily work around).
+func (check *Checker) unpackRecv(rtyp ast.Expr, unpackParams bool) (ptr bool, rname *ast.Ident, tparams []*ast.Ident) {
+L: // unpack receiver type
+       // This accepts invalid receivers such as ***T and does not
+       // work for other invalid receivers, but we don't care. The
+       // validity of receiver expressions is checked elsewhere.
+       for {
+               switch t := rtyp.(type) {
+               case *ast.ParenExpr:
+                       rtyp = t.X
+               case *ast.StarExpr:
+                       ptr = true
+                       rtyp = t.X
+               default:
+                       break L
+               }
+       }
+
+       // unpack type parameters, if any
+       switch rtyp.(type) {
+       case *ast.IndexExpr, *ast.IndexListExpr:
+               ix := typeparams.UnpackIndexExpr(rtyp)
+               rtyp = ix.X
+               if unpackParams {
+                       for _, arg := range ix.Indices {
+                               var par *ast.Ident
+                               switch arg := arg.(type) {
+                               case *ast.Ident:
+                                       par = arg
+                               case *ast.BadExpr:
+                                       // ignore - error already reported by parser
+                               case nil:
+                                       check.error(ix.Orig, InvalidSyntaxTree, "parameterized receiver contains nil parameters")
+                               default:
+                                       check.errorf(arg, BadDecl, "receiver type parameter %s must be an identifier", arg)
+                               }
+                               if par == nil {
+                                       par = &ast.Ident{NamePos: arg.Pos(), Name: "_"}
+                               }
+                               tparams = append(tparams, par)
                        }
                }
        }
+
+       // unpack receiver name
+       if name, _ := rtyp.(*ast.Ident); name != nil {
+               rname = name
+       }
+
+       return
 }
 
 // resolveBaseTypeName returns the non-alias base type name for typ, and whether
 // there was a pointer indirection to get to it. The base type name must be declared
 // in package scope, and there can be at most one pointer indirection. If no such type
 // name exists, the returned base is nil.
-func (check *Checker) resolveBaseTypeName(typ ast.Expr) (ptr bool, base *TypeName) {
+func (check *Checker) resolveBaseTypeName(seenPtr bool, typ ast.Expr, fileScopes []*Scope) (ptr bool, base *TypeName) {
        // Algorithm: Starting from a type expression, which may be a name,
        // we follow that type through alias declarations until we reach a
        // non-alias type name. If we encounter anything but pointer types or
        // parentheses we're done. If we encounter more than one pointer type
        // we're done.
+       ptr = seenPtr
        var seen map[*TypeName]bool
        for {
+               // Note: this differs from types2, but is necessary. The syntax parser
+               // strips unnecessary parens.
                typ = unparen(typ)
 
                // check if we have a pointer type
@@ -466,15 +573,43 @@ func (check *Checker) resolveBaseTypeName(typ ast.Expr) (ptr bool, base *TypeNam
                        typ = unparen(pexpr.X) // continue with pointer base type
                }
 
-               // typ must be a name
-               name, _ := typ.(*ast.Ident)
-               if name == nil {
+               // typ must be a name, or a C.name cgo selector.
+               var name string
+               switch typ := typ.(type) {
+               case *ast.Ident:
+                       name = typ.Name
+               case *ast.SelectorExpr:
+                       // C.struct_foo is a valid type name for packages using cgo.
+                       //
+                       // Detect this case, and adjust name so that the correct TypeName is
+                       // resolved below.
+                       if ident, _ := typ.X.(*ast.Ident); ident != nil && ident.Name == "C" {
+                               // Check whether "C" actually resolves to an import of "C", by looking
+                               // in the appropriate file scope.
+                               var obj Object
+                               for _, scope := range fileScopes {
+                                       if scope.Contains(ident.Pos()) {
+                                               obj = scope.Lookup(ident.Name)
+                                       }
+                               }
+                               // If Config.go115UsesCgo is set, the typechecker will resolve Cgo
+                               // selectors to their cgo name. We must do the same here.
+                               if pname, _ := obj.(*PkgName); pname != nil {
+                                       if pname.imported.cgo { // only set if Config.go115UsesCgo is set
+                                               name = "_Ctype_" + typ.Sel.Name
+                                       }
+                               }
+                       }
+                       if name == "" {
+                               return false, nil
+                       }
+               default:
                        return false, nil
                }
 
                // name must denote an object found in the current package scope
                // (note that dot-imported objects are not in the package scope!)
-               obj := check.pkg.scope.Lookup(name.Name)
+               obj := check.pkg.scope.Lookup(name)
                if obj == nil {
                        return false, nil
                }
@@ -492,13 +627,13 @@ func (check *Checker) resolveBaseTypeName(typ ast.Expr) (ptr bool, base *TypeNam
 
                // we're done if tdecl defined tname as a new type
                // (rather than an alias)
-               tdecl := check.objMap[tname] // must exist for objects in package scope
-               if !tdecl.aliasPos.IsValid() {
+               tdecl := check.objMap[tname].tdecl // must exist for objects in package scope
+               if !tdecl.Assign.IsValid() {
                        return ptr, tname
                }
 
                // otherwise, continue resolving
-               typ = tdecl.typ
+               typ = tdecl.Type
                if seen == nil {
                        seen = make(map[*TypeName]bool)
                }
@@ -520,30 +655,43 @@ func (check *Checker) packageObjects() {
        // add new methods to already type-checked types (from a prior Checker.Files call)
        for _, obj := range objList {
                if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
-                       check.addMethodDecls(obj)
+                       check.collectMethods(obj)
                }
        }
 
-       // We process non-alias declarations first, in order to avoid situations where
-       // the type of an alias declaration is needed before it is available. In general
-       // this is still not enough, as it is possible to create sufficiently convoluted
-       // recursive type definitions that will cause a type alias to be needed before it
-       // is available (see issue #25838 for examples).
-       // As an aside, the cmd/compiler suffers from the same problem (#25838).
-       var aliasList []*TypeName
-       // phase 1
-       for _, obj := range objList {
-               // If we have a type alias, collect it for the 2nd phase.
-               if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].aliasPos.IsValid() {
-                       aliasList = append(aliasList, tname)
-                       continue
+       if check.conf._EnableAlias {
+               // With _Alias nodes we can process declarations in any order.
+               for _, obj := range objList {
+                       check.objDecl(obj, nil)
+               }
+       } else {
+               // Without _Alias nodes, we process non-alias type declarations first, followed by
+               // alias declarations, and then everything else. This appears to avoid most situations
+               // where the type of an alias is needed before it is available.
+               // There may still be cases where this is not good enough (see also go.dev/issue/25838).
+               // In those cases Checker.ident will report an error ("invalid use of type alias").
+               var aliasList []*TypeName
+               var othersList []Object // everything that's not a type
+               // phase 1: non-alias type declarations
+               for _, obj := range objList {
+                       if tname, _ := obj.(*TypeName); tname != nil {
+                               if check.objMap[tname].tdecl.Assign.IsValid() {
+                                       aliasList = append(aliasList, tname)
+                               } else {
+                                       check.objDecl(obj, nil)
+                               }
+                       } else {
+                               othersList = append(othersList, obj)
+                       }
+               }
+               // phase 2: alias type declarations
+               for _, obj := range aliasList {
+                       check.objDecl(obj, nil)
+               }
+               // phase 3: all other declarations
+               for _, obj := range othersList {
+                       check.objDecl(obj, nil)
                }
-
-               check.objDecl(obj, nil)
-       }
-       // phase 2
-       for _, obj := range aliasList {
-               check.objDecl(obj, nil)
        }
 
        // At this point we may have a non-empty check.methods map; this means that not all
@@ -562,7 +710,7 @@ func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
 
 // unusedImports checks for unused imports.
 func (check *Checker) unusedImports() {
-       // if function bodies are not checked, packages' uses are likely missing - don't check
+       // If function bodies are not checked, packages' uses are likely missing - don't check.
        if check.conf.IgnoreFuncBodies {
                return
        }
@@ -591,9 +739,9 @@ func (check *Checker) errorUnusedPkg(obj *PkgName) {
                elem = elem[i+1:]
        }
        if obj.name == "" || obj.name == "." || obj.name == elem {
-               check.softErrorf(obj, _UnusedImport, "%q imported but not used", path)
+               check.softErrorf(obj, UnusedImport, "%q imported and not used", path)
        } else {
-               check.softErrorf(obj, _UnusedImport, "%q imported but not used as %s", path, obj.name)
+               check.softErrorf(obj, UnusedImport, "%q imported as %s and not used", path, obj.name)
        }
 }