mirror of
https://github.com/golang/go
synced 2024-11-18 18:54:42 -07:00
go.oracle: freevars: don't report free identifiers defined in package scope.
The existing check rejected only free identifiers defined in file scope, i.e. just imports. + regression test. R=crawshaw, gri CC=golang-dev https://golang.org/cl/13256050
This commit is contained in:
parent
7a5597c226
commit
daa44ab970
@ -28,6 +28,8 @@ import (
|
||||
//
|
||||
func freevars(o *oracle) (queryResult, error) {
|
||||
file := o.queryPath[len(o.queryPath)-1] // the enclosing file
|
||||
fileScope := o.queryPkgInfo.Scopes[file]
|
||||
pkgScope := fileScope.Parent()
|
||||
|
||||
// The id and sel functions return non-nil if they denote an
|
||||
// object o or selection o.x.y that is referenced by the
|
||||
@ -61,11 +63,12 @@ func freevars(o *oracle) (queryResult, error) {
|
||||
if !(file.Pos() <= obj.Pos() && obj.Pos() <= file.End()) {
|
||||
return nil // not defined in this file
|
||||
}
|
||||
if obj.Parent() == nil {
|
||||
return nil // e.g. interface method TODO(adonovan): what else?
|
||||
scope := obj.Parent()
|
||||
if scope == nil {
|
||||
return nil // e.g. interface method, struct field
|
||||
}
|
||||
if obj.Parent() == o.queryPkgInfo.Scopes[file] {
|
||||
return nil // defined at file scope
|
||||
if scope == fileScope || scope == pkgScope {
|
||||
return nil // defined at file or package scope
|
||||
}
|
||||
if o.startPos <= obj.Pos() && obj.Pos() <= o.endPos {
|
||||
return nil // defined within selection => not free
|
||||
|
4
oracle/testdata/src/main/freevars.go
vendored
4
oracle/testdata/src/main/freevars.go
vendored
@ -15,6 +15,8 @@ type S struct {
|
||||
t T
|
||||
}
|
||||
|
||||
func f(int) {}
|
||||
|
||||
func main() {
|
||||
type C int
|
||||
x := 1
|
||||
@ -29,6 +31,8 @@ func main() {
|
||||
println(s.x + s.t.a + s.t.b + x + int(y)) // @freevars fv2 "print.*y."
|
||||
}
|
||||
|
||||
f(x) // @freevars fv3 "f.x."
|
||||
|
||||
// TODO(adonovan): enable when go/types supports labels.
|
||||
loop: // #@freevars fv-def-label "loop:"
|
||||
for {
|
||||
|
4
oracle/testdata/src/main/freevars.golden
vendored
4
oracle/testdata/src/main/freevars.golden
vendored
@ -12,3 +12,7 @@ var s.x int
|
||||
var x int
|
||||
var y int32
|
||||
|
||||
-------- @freevars fv3 --------
|
||||
Free identifers:
|
||||
var x int
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user