func (c *funcContext) objectName(o types.Object) string { if o.Pkg() != c.p.pkg || o.Parent() == c.p.pkg.Scope() { c.p.dependencies[o] = true } if o.Pkg() != c.p.pkg { pkgVar, found := c.p.pkgVars[o.Pkg().Path()] if !found { pkgVar = fmt.Sprintf(`$packages["%s"]`, o.Pkg().Path()) } return pkgVar + "." + o.Name() } switch o.(type) { case *types.Var, *types.Const: if o.Exported() && o.Parent() == c.p.pkg.Scope() { return "$pkg." + o.Name() } } name, found := c.p.objectVars[o] if !found { name = c.newVariableWithLevel(o.Name(), o.Parent() == c.p.pkg.Scope(), "") c.p.objectVars[o] = name } if c.p.escapingVars[o] { return name + "[0]" } return name }
// checkInPackageBlock performs safety checks for renames of // func/var/const/type objects in the package block. func (r *renamer) checkInPackageBlock(from types.Object) { // Check that there are no references to the name from another // package if the renaming would make it unexported. if ast.IsExported(from.Name()) && !ast.IsExported(r.to) { for pkg, info := range r.packages { if pkg == from.Pkg() { continue } if id := someUse(info, from); id != nil && !r.checkExport(id, pkg, from) { break } } } info := r.packages[from.Pkg()] lexinfo := lexical.Structure(r.iprog.Fset, from.Pkg(), &info.Info, info.Files) // Check that in the package block, "init" is a function, and never referenced. if r.to == "init" { kind := objectKind(from) if kind == "func" { // Reject if intra-package references to it exist. if refs := lexinfo.Refs[from]; len(refs) > 0 { r.errorf(from.Pos(), "renaming this func %q to %q would make it a package initializer", from.Name(), r.to) r.errorf(refs[0].Id.Pos(), "\tbut references to it exist") } } else { r.errorf(from.Pos(), "you cannot have a %s at package level named %q", kind, r.to) } } // Check for conflicts between package block and all file blocks. for _, f := range info.Files { if prev, b := lexinfo.Blocks[f].Lookup(r.to); b == lexinfo.Blocks[f] { r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to) r.errorf(prev.Pos(), "\twith this %s", objectKind(prev)) return // since checkInPackageBlock would report redundant errors } } // Check for conflicts in lexical scope. if from.Exported() { for _, info := range r.packages { r.checkInLexicalScope(from, info) } } else { r.checkInLexicalScope(from, info) } }