Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/wire: use set to determine which argument to use in zero-call injectors #223

Merged
merged 1 commit into from Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go
@@ -0,0 +1,25 @@
// Copyright 2019 The Wire Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "fmt"

func main() {
fmt.Println(injectStringer("Hello, World!"))
}

type MyString string

func (s MyString) String() string { return string(s) }
28 changes: 28 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go
@@ -0,0 +1,28 @@
// Copyright 2019 The Wire Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//+build wireinject

package main

import (
"fmt"

"github.com/google/wire"
)

func injectStringer(s MyString) fmt.Stringer {
wire.Build(wire.Bind(new(fmt.Stringer), new(MyString)))
return nil
}
1 change: 1 addition & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/pkg
@@ -0,0 +1 @@
example.com/foo
@@ -0,0 +1 @@
Hello, World!
16 changes: 16 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions internal/wire/wire.go
Expand Up @@ -367,12 +367,12 @@ func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *Prov
}

// Perform one pass to collect all imports, followed by the real pass.
injectPass(name, sig, calls, &injectorGen{
injectPass(name, sig, calls, set, &injectorGen{
g: g,
errVar: disambiguate("err", g.nameInFileScope),
discard: true,
})
injectPass(name, sig, calls, &injectorGen{
injectPass(name, sig, calls, set, &injectorGen{
g: g,
errVar: disambiguate("err", g.nameInFileScope),
discard: false,
Expand Down Expand Up @@ -586,7 +586,7 @@ type injectorGen struct {

// injectPass generates an injector given the output from analysis.
// The sig passed in should be verified.
func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen) {
func injectPass(name string, sig *types.Signature, calls []call, set *ProviderSet, ig *injectorGen) {
params := sig.Params()
injectSig, err := funcOutput(sig)
if err != nil {
Expand Down Expand Up @@ -643,12 +643,7 @@ func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen
}
}
if len(calls) == 0 {
for i := 0; i < params.Len(); i++ {
if types.Identical(injectSig.out, params.At(i).Type()) {
ig.p("\treturn %s", ig.paramNames[i])
break
}
}
ig.p("\treturn %s", ig.paramNames[set.For(injectSig.out).Arg().Index])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could Arg panic here due to some user error? If so, it might be worth to add a better error message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any error here should be due to a bug: the only reason that the zero-call case should be able to occur is when the injector function result should be coming directly from an argument.

} else {
ig.p("\treturn %s", ig.localNames[len(calls)-1])
}
Expand Down