From a4491f08bf5b232d5e20ca71522c4da179bc7a08 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Mar 2014 14:00:35 -0400 Subject: [PATCH] go.tools/oracle: fix bug in reduceScope on ad-hoc main packages. LGTM=crawshaw R=crawshaw CC=golang-codereviews https://golang.org/cl/77450048 --- oracle/oracle.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/oracle/oracle.go b/oracle/oracle.go index 67aac5cb..f4bb9936 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -303,14 +303,13 @@ func reduceScope(pos string, conf *loader.Config) { if err != nil { return // no files for package } - _ = bp - // TODO(adonovan): fix: also check that the queried file appears in the package. - // for _, f := range bp.GoFiles, bp.TestGoFiles, bp.XTestGoFiles { - // if sameFile(f, fqpos.filename) { goto found } - // } - // return // not found - // found: + // Check that the queried file appears in the package: + // it might be a '// +build ignore' from an ad-hoc main + // package, e.g. $GOROOT/src/pkg/net/http/triv.go. + if !pkgContainsFile(bp, fqpos.fset.File(fqpos.start).Name()) { + return // not found + } conf.TypeCheckFuncBodies = func(p string) bool { return p == importPath } @@ -325,6 +324,17 @@ func reduceScope(pos string, conf *loader.Config) { _ = conf.ImportWithTests(importPath) // ignore error } +func pkgContainsFile(bp *build.Package, filename string) bool { + for _, files := range [][]string{bp.GoFiles, bp.TestGoFiles, bp.XTestGoFiles} { + for _, file := range files { + if sameFile(file, filename) { + return true + } + } + } + return false +} + // New constructs a new Oracle that can be used for a sequence of queries. // // iprog specifies the program to analyze.