diff --git a/internal/wire/testdata/ValueFromFunctionScope/foo/foo.go b/internal/wire/testdata/ValueFromFunctionScope/foo/foo.go new file mode 100644 index 0000000..e3fe914 --- /dev/null +++ b/internal/wire/testdata/ValueFromFunctionScope/foo/foo.go @@ -0,0 +1,37 @@ +// Copyright 2018 The Go Cloud 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" +) + +type foo struct { + V int +} + +type bar struct { + V int +} + +func newBar(v int) *bar { + return &bar{V: v} +} + +func main() { + f := &foo{V: 42} + b := injectBar(f) + fmt.Printf("%d\n", b.V) +} diff --git a/internal/wire/testdata/ValueFromFunctionScope/foo/wire.go b/internal/wire/testdata/ValueFromFunctionScope/foo/wire.go new file mode 100644 index 0000000..42bf705 --- /dev/null +++ b/internal/wire/testdata/ValueFromFunctionScope/foo/wire.go @@ -0,0 +1,29 @@ +// Copyright 2018 The Go Cloud 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 ( + "github.com/google/go-cloud/wire" +) + +func injectBar(f *foo) *bar { + wire.Build( + newBar, + wire.Value(f.V), // fails because f.V is not from package scope + ) + return nil +} diff --git a/internal/wire/testdata/ValueFromFunctionScope/pkg b/internal/wire/testdata/ValueFromFunctionScope/pkg new file mode 100644 index 0000000..f7a5c8c --- /dev/null +++ b/internal/wire/testdata/ValueFromFunctionScope/pkg @@ -0,0 +1 @@ +example.com/foo diff --git a/internal/wire/testdata/ValueFromFunctionScope/want/wire_errs.txt b/internal/wire/testdata/ValueFromFunctionScope/want/wire_errs.txt new file mode 100644 index 0000000..a3b3705 --- /dev/null +++ b/internal/wire/testdata/ValueFromFunctionScope/want/wire_errs.txt @@ -0,0 +1 @@ +inject injectBar: value int can't be used: f is not declared in package scope diff --git a/internal/wire/wire.go b/internal/wire/wire.go index 68aa13d..666cae1 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -765,9 +765,15 @@ func accessibleFrom(info *types.Info, node ast.Node, wantPkg string) error { // Local package names are fine, since we can just reimport them. return true } - if pkg := obj.Pkg(); pkg != nil && !ast.IsExported(ident.Name) && pkg.Path() != wantPkg { - unexportError = fmt.Errorf("uses unexported identifier %s", obj.Name()) - return false + if pkg := obj.Pkg(); pkg != nil { + if !ast.IsExported(ident.Name) && pkg.Path() != wantPkg { + unexportError = fmt.Errorf("uses unexported identifier %s", obj.Name()) + return false + } + if obj.Parent() != pkg.Scope() { + unexportError = fmt.Errorf("%s is not declared in package scope", obj.Name()) + return false + } } return true })