wire: add a test that fails due to wire.Value on a value from function scope (google/go-cloud#405)
Fixes google/go-cloud#378
This commit is contained in:
committed by
Ross Light
parent
46248162d7
commit
fab79bd5bd
37
internal/wire/testdata/ValueFromFunctionScope/foo/foo.go
vendored
Normal file
37
internal/wire/testdata/ValueFromFunctionScope/foo/foo.go
vendored
Normal file
@@ -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)
|
||||
}
|
||||
29
internal/wire/testdata/ValueFromFunctionScope/foo/wire.go
vendored
Normal file
29
internal/wire/testdata/ValueFromFunctionScope/foo/wire.go
vendored
Normal file
@@ -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
|
||||
}
|
||||
1
internal/wire/testdata/ValueFromFunctionScope/pkg
vendored
Normal file
1
internal/wire/testdata/ValueFromFunctionScope/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
||||
example.com/foo
|
||||
1
internal/wire/testdata/ValueFromFunctionScope/want/wire_errs.txt
vendored
Normal file
1
internal/wire/testdata/ValueFromFunctionScope/want/wire_errs.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
inject injectBar: value int can't be used: f is not declared in package scope
|
||||
@@ -765,10 +765,16 @@ 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 {
|
||||
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
|
||||
})
|
||||
return unexportError
|
||||
|
||||
Reference in New Issue
Block a user