diff --git a/internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go b/internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go new file mode 100644 index 00000000..6af8eb87 --- /dev/null +++ b/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) } diff --git a/internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go b/internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go new file mode 100644 index 00000000..6e974e5a --- /dev/null +++ b/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 +} diff --git a/internal/wire/testdata/ReturnArgumentAsInterface/pkg b/internal/wire/testdata/ReturnArgumentAsInterface/pkg new file mode 100644 index 00000000..f7a5c8ce --- /dev/null +++ b/internal/wire/testdata/ReturnArgumentAsInterface/pkg @@ -0,0 +1 @@ +example.com/foo diff --git a/internal/wire/testdata/ReturnArgumentAsInterface/want/program_out.txt b/internal/wire/testdata/ReturnArgumentAsInterface/want/program_out.txt new file mode 100644 index 00000000..8ab686ea --- /dev/null +++ b/internal/wire/testdata/ReturnArgumentAsInterface/want/program_out.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go b/internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go new file mode 100644 index 00000000..211e06f8 --- /dev/null +++ b/internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go @@ -0,0 +1,16 @@ +// Code generated by Wire. DO NOT EDIT. + +//go:generate wire +//+build !wireinject + +package main + +import ( + "fmt" +) + +// Injectors from wire.go: + +func injectStringer(s MyString) fmt.Stringer { + return s +} diff --git a/internal/wire/wire.go b/internal/wire/wire.go index 10069a49..b55ff29f 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -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, @@ -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 { @@ -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]) } else { ig.p("\treturn %s", ig.localNames[len(calls)-1]) }