wire: fill in ProviderSet.VarName when the set is a package variable (google/go-cloud#279)

Rename ProviderSet.Name to ProviderSet.VarName.

Fixes google/go-cloud#277
This commit is contained in:
Robert van Gent
2018-08-02 14:00:17 -07:00
committed by Ross Light
parent 85deb53791
commit 3a3760180d
5 changed files with 17 additions and 16 deletions

View File

@@ -217,8 +217,8 @@ func gather(info *wire.Info, key wire.ProviderSetID) (_ []outGroup, imports map[
continue continue
} }
visited[curr] = struct{}{} visited[curr] = struct{}{}
if curr.Name != "" && !(curr.PkgPath == key.ImportPath && curr.Name == key.VarName) { if curr.VarName != "" && !(curr.PkgPath == key.ImportPath && curr.VarName == key.VarName) {
imports[formatProviderSetName(curr.PkgPath, curr.Name)] = struct{}{} imports[formatProviderSetName(curr.PkgPath, curr.VarName)] = struct{}{}
} }
for _, imp := range curr.Imports { for _, imp := range curr.Imports {
next = append(next, imp) next = append(next, imp)

View File

@@ -248,10 +248,10 @@ func verifyArgsUsed(set *ProviderSet, used []*providerSetSrc) []error {
} }
} }
if !found { if !found {
if imp.Name == "" { if imp.VarName == "" {
errs = append(errs, errors.New("unused provider set")) errs = append(errs, errors.New("unused provider set"))
} else { } else {
errs = append(errs, fmt.Errorf("unused provider set %q", imp.Name)) errs = append(errs, fmt.Errorf("unused provider set %q", imp.VarName))
} }
} }
} }
@@ -429,12 +429,12 @@ func verifyAcyclic(providerMap *typeutil.Map, hasher typeutil.Hasher) []error {
func bindingConflictError(fset *token.FileSet, pos token.Pos, typ types.Type, prevSet *ProviderSet) error { func bindingConflictError(fset *token.FileSet, pos token.Pos, typ types.Type, prevSet *ProviderSet) error {
typString := types.TypeString(typ, nil) typString := types.TypeString(typ, nil)
var err error var err error
if prevSet.Name == "" { if prevSet.VarName == "" {
err = fmt.Errorf("multiple bindings for %s (previous binding at %v)", err = fmt.Errorf("multiple bindings for %s (previous binding at %v)",
typString, fset.Position(prevSet.Pos)) typString, fset.Position(prevSet.Pos))
} else { } else {
err = fmt.Errorf("multiple bindings for %s (previous binding in %q.%s)", err = fmt.Errorf("multiple bindings for %s (previous binding in %q.%s)",
typString, prevSet.PkgPath, prevSet.Name) typString, prevSet.PkgPath, prevSet.VarName)
} }
return notePosition(fset.Position(pos), err) return notePosition(fset.Position(pos), err)
} }

View File

@@ -46,9 +46,9 @@ type ProviderSet struct {
Pos token.Pos Pos token.Pos
// PkgPath is the import path of the package that declared this set. // PkgPath is the import path of the package that declared this set.
PkgPath string PkgPath string
// Name is the variable name of the set, if it came from a package // VarName is the variable name of the set, if it came from a package
// variable. // variable.
Name string VarName string
Providers []*Provider Providers []*Provider
Bindings []*IfaceBinding Bindings []*IfaceBinding
@@ -202,7 +202,7 @@ func Load(bctx *build.Context, wd string, pkgs []string) (*Info, []error) {
if buildCall == nil { if buildCall == nil {
continue continue
} }
set, errs := oc.processNewSet(pkgInfo, buildCall) set, errs := oc.processNewSet(pkgInfo, buildCall, "")
if len(errs) > 0 { if len(errs) > 0 {
ec.add(notePositionAll(prog.Fset.Position(fn.Pos()), errs)...) ec.add(notePositionAll(prog.Fset.Position(fn.Pos()), errs)...)
continue continue
@@ -393,7 +393,7 @@ func (oc *objectCache) get(obj types.Object) (val interface{}, errs []error) {
break break
} }
} }
return oc.processExpr(oc.prog.Package(obj.Pkg().Path()), spec.Values[i]) return oc.processExpr(oc.prog.Package(obj.Pkg().Path()), spec.Values[i], obj.Name())
case *types.Func: case *types.Func:
return processFuncProvider(oc.prog.Fset, obj) return processFuncProvider(oc.prog.Fset, obj)
default: default:
@@ -424,7 +424,7 @@ func (oc *objectCache) varDecl(obj *types.Var) *ast.ValueSpec {
// processExpr converts an expression into a Wire structure. It may // processExpr converts an expression into a Wire structure. It may
// return a *Provider, a structProviderPair, an *IfaceBinding, a // return a *Provider, a structProviderPair, an *IfaceBinding, a
// *ProviderSet, or a *Value. // *ProviderSet, or a *Value.
func (oc *objectCache) processExpr(pkg *loader.PackageInfo, expr ast.Expr) (interface{}, []error) { func (oc *objectCache) processExpr(pkg *loader.PackageInfo, expr ast.Expr, varName string) (interface{}, []error) {
exprPos := oc.prog.Fset.Position(expr.Pos()) exprPos := oc.prog.Fset.Position(expr.Pos())
expr = astutil.Unparen(expr) expr = astutil.Unparen(expr)
if obj := qualifiedIdentObject(&pkg.Info, expr); obj != nil { if obj := qualifiedIdentObject(&pkg.Info, expr); obj != nil {
@@ -440,7 +440,7 @@ func (oc *objectCache) processExpr(pkg *loader.PackageInfo, expr ast.Expr) (inte
} }
switch fnObj.Name() { switch fnObj.Name() {
case "NewSet": case "NewSet":
pset, errs := oc.processNewSet(pkg, call) pset, errs := oc.processNewSet(pkg, call, varName)
return pset, notePositionAll(exprPos, errs) return pset, notePositionAll(exprPos, errs)
case "Bind": case "Bind":
b, err := processBind(oc.prog.Fset, &pkg.Info, call) b, err := processBind(oc.prog.Fset, &pkg.Info, call)
@@ -476,16 +476,17 @@ type structProviderPair struct {
ptrProvider *Provider ptrProvider *Provider
} }
func (oc *objectCache) processNewSet(pkg *loader.PackageInfo, call *ast.CallExpr) (*ProviderSet, []error) { func (oc *objectCache) processNewSet(pkg *loader.PackageInfo, call *ast.CallExpr, varName string) (*ProviderSet, []error) {
// Assumes that call.Fun is wire.NewSet or wire.Build. // Assumes that call.Fun is wire.NewSet or wire.Build.
pset := &ProviderSet{ pset := &ProviderSet{
Pos: call.Pos(), Pos: call.Pos(),
PkgPath: pkg.Pkg.Path(), PkgPath: pkg.Pkg.Path(),
VarName: varName,
} }
ec := new(errorCollector) ec := new(errorCollector)
for _, arg := range call.Args { for _, arg := range call.Args {
item, errs := oc.processExpr(pkg, arg) item, errs := oc.processExpr(pkg, arg, "")
if len(errs) > 0 { if len(errs) > 0 {
ec.add(errs...) ec.add(errs...)
continue continue

View File

@@ -1,5 +1,5 @@
ERROR ERROR
unused provider set unused provider set "unusedSet"
unused provider "provideUnused" unused provider "provideUnused"
unused value of type string unused value of type string
unused interface binding to type foo.Fooer unused interface binding to type foo.Fooer

View File

@@ -86,7 +86,7 @@ func generateInjectors(g *gen, pkgInfo *loader.PackageInfo) (injectorFiles []*as
g.p("// Injectors from %s:\n\n", name) g.p("// Injectors from %s:\n\n", name)
injectorFiles = append(injectorFiles, f) injectorFiles = append(injectorFiles, f)
} }
set, errs := oc.processNewSet(pkgInfo, buildCall) set, errs := oc.processNewSet(pkgInfo, buildCall, "")
if len(errs) > 0 { if len(errs) > 0 {
ec.add(notePositionAll(g.prog.Fset.Position(fn.Pos()), errs)...) ec.add(notePositionAll(g.prog.Fset.Position(fn.Pos()), errs)...)
continue continue