this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

lsp/eval: Correct usages for import specs

package x
import "y.com/z"
out: fun(z)

If we ask for the usages of either `z` on line 3, or `y.com/z" on line
2, the same thing happens: we look for usages of the import. This does
correctly find the `z` on line 3, but the way it traverses up the
evaluation tree also means it reaches the top-level pkg navigable, which
means it's marked as escaping from the pkg and so other pkgs in the same
module then get searched and evaluated.

This behaviour is correct except for imports. Imports do not get
automatically exported by the current pkg. So we need to detect this
situation and make sure we cannot consider it escaped from the current
pkg.

The old behaviour wouldn't result in the wrong results, it would just do
unnecessary work. The new code is already exercised by the existing
tests (checked with code coverage). Adding a dedicated test to check
that in this scenario we do not regress and perform extra work would be
extremely difficult, as there's no sensible way to observe the amount of
work done.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I68c84ec3251ca568370a9710fe3722d9ce0f0fa4
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1228807
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>

+13
+13
internal/lsp/eval/eval.go
··· 947 947 for parent := nav; parent != nil; child, parent = parent, parent.parent { 948 948 if parent == pkgNav { 949 949 isExported = true 950 + if child != nil { 951 + for _, fr := range nav.frames { 952 + if _, ok := fr.node.(*ast.ImportSpec); ok { 953 + // We're looking for usages of an imported pkg 954 + // within the current pkg. Imports do not 955 + // automatically get re-exported by a package, 956 + // so although we've reached the pkgNav, this 957 + // import spec is not exported. 958 + isExported = false 959 + break 960 + } 961 + } 962 + } 950 963 } 951 964 evalWorklist = []*navigable{parent} 952 965 if child == nil {