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 11:11:01 -06:00
|
|
|
// 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"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/buildutil"
|
|
|
|
"golang.org/x/tools/go/loader"
|
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 11:11:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/cmd/godoc",
|
|
|
|
"golang.org/x/tools/refactor/lexical")
|
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 11:11:01 -06:00
|
|
|
|
|
|
|
// Load, parse and type-check the program.
|
2015-02-23 15:03:22 -07:00
|
|
|
conf := loader.Config{Build: &ctxt}
|
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 11:11:01 -06:00
|
|
|
for _, path := range pkgs {
|
2015-01-30 11:30:23 -07:00
|
|
|
conf.ImportWithTests(path)
|
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 11:11:01 -06:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|