1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:54:40 -07:00

go.tools/go/types: don't crash dealing with unknown constants

Also: Better tracing for top-level declarations.

Fixes golang/go#8518.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/132310043
This commit is contained in:
Robert Griesemer 2014-08-28 12:17:30 -07:00
parent 61ce470a5e
commit 87b4cd993d
3 changed files with 45 additions and 4 deletions

View File

@ -647,7 +647,7 @@ func TestSelection(t *testing.T) {
conf.Packages[path] = pkg
}
libSrc := `
const libSrc = `
package lib
type T float64
const C T = 3
@ -655,7 +655,7 @@ var V T
func F() {}
func (T) M() {}
`
mainSrc := `
const mainSrc = `
package main
import "lib"
@ -791,3 +791,39 @@ func main() {
}
}
}
func TestIssue8518(t *testing.T) {
fset := token.NewFileSet()
conf := Config{
Packages: make(map[string]*Package),
Error: func(err error) { t.Log(err) }, // don't exit after first error
Import: func(imports map[string]*Package, path string) (*Package, error) {
return imports[path], nil
},
}
makePkg := func(path, src string) {
f, err := parser.ParseFile(fset, path, src, 0)
if err != nil {
t.Fatal(err)
}
pkg, _ := conf.Check(path, fset, []*ast.File{f}, nil) // errors logged via conf.Error
conf.Packages[path] = pkg
}
const libSrc = `
package a
import "missing"
const C1 = foo
const C2 = missing.C
`
const mainSrc = `
package main
import "a"
var _ = a.C1
var _ = a.C2
`
makePkg("a", libSrc)
makePkg("main", mainSrc) // don't crash when type-checking this package
}

View File

@ -253,7 +253,7 @@ func (check *Checker) recordTypeAndValue(x ast.Expr, mode operandMode, typ Type,
assert(typ != nil)
if mode == constant {
assert(val != nil)
assert(isConstType(typ))
assert(typ == Typ[Invalid] || isConstType(typ))
}
if m := check.Types; m != nil {
m[x] = TypeAndValue{mode, typ, val}

View File

@ -45,7 +45,12 @@ func (check *Checker) objDecl(obj Object, def *Named, path []*TypeName) {
}
if trace {
check.trace(obj.Pos(), "-- resolving %s", obj.Name())
check.trace(obj.Pos(), "-- declaring %s", obj.Name())
check.indent++
defer func() {
check.indent--
check.trace(obj.Pos(), "=> %s", obj)
}()
}
d := check.objMap[obj]