1
0
mirror of https://github.com/golang/go synced 2024-10-01 20:28:33 -06:00
go/refactor/lexical/lexical_test.go
Alan Donovan 897f6677ae refactor/lexical: understand the structure of the lexical environment.
The Uses, Defs and Scope information provided by go/types is
inadequate for answering "what if?" queries about the
structure of the lexical environment.

In this code, for example,

        var x int

        func f() {
                print(x)
                x := ""
                print(x)
        }

the two referring Idents x appear at the same lexical depth,
inside the function f's Scope object, yet they resolve to
different objects.

This package associates a lexical.Environment instance with
every reference to capture these differences.  Each
environment is a linked list of enclosing Blocks, and for each
block, a number indicating what prefix of its bindings are
visible.  (Zero for the first 'x' reference above, 1 for the
second.)

+ Smoke test over stdlib.

This functionality could be integrated with the type checker
in lieu of the not-so-useful types.Info.Scopes data, at little
extra cost in code or in running time/space.  We should talk
about that.

LGTM=sameer
R=gri, sameer
CC=golang-codereviews
https://golang.org/cl/143790043
2014-09-19 13:11:01 -04:00

56 lines
1.3 KiB
Go

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lexical
import (
"go/build"
"testing"
"code.google.com/p/go.tools/go/buildutil"
"code.google.com/p/go.tools/go/loader"
)
func TestStdlib(t *testing.T) {
defer func(saved func(format string, args ...interface{})) {
logf = saved
}(logf)
logf = t.Errorf
ctxt := build.Default // copy
// Enumerate $GOROOT packages.
saved := ctxt.GOPATH
ctxt.GOPATH = "" // disable GOPATH during AllPackages
pkgs := buildutil.AllPackages(&ctxt)
ctxt.GOPATH = saved
// Throw in a number of go.tools packages too.
pkgs = append(pkgs,
"code.google.com/p/go.tools/cmd/godoc",
"code.google.com/p/go.tools/refactor/lexical")
// Load, parse and type-check the program.
conf := loader.Config{
Build: &ctxt,
SourceImports: true,
}
for _, path := range pkgs {
if err := conf.ImportWithTests(path); err != nil {
t.Error(err)
}
}
iprog, err := conf.Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
// This test ensures that Structure doesn't panic and that
// its internal sanity-checks against go/types don't fail.
for pkg, info := range iprog.AllPackages {
_ = Structure(iprog.Fset, pkg, &info.Info, info.Files)
}
}