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) {
|
func freevars(o *oracle) (queryResult, error) {
|
||||||
file := o.queryPath[len(o.queryPath)-1] // the enclosing file
|
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
|
// The id and sel functions return non-nil if they denote an
|
||||||
// object o or selection o.x.y that is referenced by the
|
// 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()) {
|
if !(file.Pos() <= obj.Pos() && obj.Pos() <= file.End()) {
|
||||||
return nil // not defined in this file
|
return nil // not defined in this file
|
||||||
}
|
}
|
||||||
if obj.Parent() == nil {
|
scope := obj.Parent()
|
||||||
return nil // e.g. interface method TODO(adonovan): what else?
|
if scope == nil {
|
||||||
|
return nil // e.g. interface method, struct field
|
||||||
}
|
}
|
||||||
if obj.Parent() == o.queryPkgInfo.Scopes[file] {
|
if scope == fileScope || scope == pkgScope {
|
||||||
return nil // defined at file scope
|
return nil // defined at file or package scope
|
||||||
}
|
}
|
||||||
if o.startPos <= obj.Pos() && obj.Pos() <= o.endPos {
|
if o.startPos <= obj.Pos() && obj.Pos() <= o.endPos {
|
||||||
return nil // defined within selection => not free
|
return nil // defined within selection => not free
|
||||||
|
|
|
@ -15,6 +15,8 @@ type S struct {
|
||||||
t T
|
t T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func f(int) {}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type C int
|
type C int
|
||||||
x := 1
|
x := 1
|
||||||
|
@ -29,6 +31,8 @@ func main() {
|
||||||
println(s.x + s.t.a + s.t.b + x + int(y)) // @freevars fv2 "print.*y."
|
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.
|
// TODO(adonovan): enable when go/types supports labels.
|
||||||
loop: // #@freevars fv-def-label "loop:"
|
loop: // #@freevars fv-def-label "loop:"
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -12,3 +12,7 @@ var s.x int
|
||||||
var x int
|
var x int
|
||||||
var y int32
|
var y int32
|
||||||
|
|
||||||
|
-------- @freevars fv3 --------
|
||||||
|
Free identifers:
|
||||||
|
var x int
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue