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:
committed by
Ross Light
parent
85deb53791
commit
3a3760180d
@@ -248,10 +248,10 @@ func verifyArgsUsed(set *ProviderSet, used []*providerSetSrc) []error {
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if imp.Name == "" {
|
||||
if imp.VarName == "" {
|
||||
errs = append(errs, errors.New("unused provider set"))
|
||||
} 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 {
|
||||
typString := types.TypeString(typ, nil)
|
||||
var err error
|
||||
if prevSet.Name == "" {
|
||||
if prevSet.VarName == "" {
|
||||
err = fmt.Errorf("multiple bindings for %s (previous binding at %v)",
|
||||
typString, fset.Position(prevSet.Pos))
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -46,9 +46,9 @@ type ProviderSet struct {
|
||||
Pos token.Pos
|
||||
// PkgPath is the import path of the package that declared this set.
|
||||
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.
|
||||
Name string
|
||||
VarName string
|
||||
|
||||
Providers []*Provider
|
||||
Bindings []*IfaceBinding
|
||||
@@ -202,7 +202,7 @@ func Load(bctx *build.Context, wd string, pkgs []string) (*Info, []error) {
|
||||
if buildCall == nil {
|
||||
continue
|
||||
}
|
||||
set, errs := oc.processNewSet(pkgInfo, buildCall)
|
||||
set, errs := oc.processNewSet(pkgInfo, buildCall, "")
|
||||
if len(errs) > 0 {
|
||||
ec.add(notePositionAll(prog.Fset.Position(fn.Pos()), errs)...)
|
||||
continue
|
||||
@@ -393,7 +393,7 @@ func (oc *objectCache) get(obj types.Object) (val interface{}, errs []error) {
|
||||
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:
|
||||
return processFuncProvider(oc.prog.Fset, obj)
|
||||
default:
|
||||
@@ -424,7 +424,7 @@ func (oc *objectCache) varDecl(obj *types.Var) *ast.ValueSpec {
|
||||
// processExpr converts an expression into a Wire structure. It may
|
||||
// return a *Provider, a structProviderPair, an *IfaceBinding, a
|
||||
// *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())
|
||||
expr = astutil.Unparen(expr)
|
||||
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() {
|
||||
case "NewSet":
|
||||
pset, errs := oc.processNewSet(pkg, call)
|
||||
pset, errs := oc.processNewSet(pkg, call, varName)
|
||||
return pset, notePositionAll(exprPos, errs)
|
||||
case "Bind":
|
||||
b, err := processBind(oc.prog.Fset, &pkg.Info, call)
|
||||
@@ -476,16 +476,17 @@ type structProviderPair struct {
|
||||
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.
|
||||
|
||||
pset := &ProviderSet{
|
||||
Pos: call.Pos(),
|
||||
PkgPath: pkg.Pkg.Path(),
|
||||
VarName: varName,
|
||||
}
|
||||
ec := new(errorCollector)
|
||||
for _, arg := range call.Args {
|
||||
item, errs := oc.processExpr(pkg, arg)
|
||||
item, errs := oc.processExpr(pkg, arg, "")
|
||||
if len(errs) > 0 {
|
||||
ec.add(errs...)
|
||||
continue
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
ERROR
|
||||
unused provider set
|
||||
unused provider set "unusedSet"
|
||||
unused provider "provideUnused"
|
||||
unused value of type string
|
||||
unused interface binding to type foo.Fooer
|
||||
|
||||
@@ -86,7 +86,7 @@ func generateInjectors(g *gen, pkgInfo *loader.PackageInfo) (injectorFiles []*as
|
||||
g.p("// Injectors from %s:\n\n", name)
|
||||
injectorFiles = append(injectorFiles, f)
|
||||
}
|
||||
set, errs := oc.processNewSet(pkgInfo, buildCall)
|
||||
set, errs := oc.processNewSet(pkgInfo, buildCall, "")
|
||||
if len(errs) > 0 {
|
||||
ec.add(notePositionAll(g.prog.Fset.Position(fn.Pos()), errs)...)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user