godef
godef copied to clipboard
Unable to parse implicitely typed map struct keys, when imported from other package
Category
Bug report
Environment and versions used
- Golang version: 1.9
- Operating System: Mac OSX Sierra
- godef version: current master @ b692db1
NB:
-
$GOPATH
and$GOROOT
are correctly setup - The bug should not be platform dependent
Issue
When a map that maps a struct to another struct is defined, if the map keys are typed implicitely, and the package is imported by another package, godef throws an error
Steps to reproduce
(A github repo is linked below for the test)
- Create a new directory, for example
$GOPATH/src/test
- Create a
main.go
file :
package main
import "fmt"
import "test/someotherpackage"
func main() {
fmt.Println(someotherpackage.Map)
}
- Create another package, for example
$GOPATH/src/test/someotherpackage
- Create a
test.go
file :
package someotherpackage
type Test struct {
X int
Y int
}
var Map = map[Test]struct{}{
{1,2} : struct{}{},
{1,2} : struct{}{},
{1,2} : struct{}{},
}
- Run :
godef -debug -f main.go someotherpackage.Map
This produces the following stacktrace :
Stacktrace
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.SelectorExpr someotherpackage.Map [
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.Ident someotherpackage [
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.ImportSpec "test/someotherpackage" [
2017/10/20 19:50:04 ] -> 0x0, Type{package "" *ast.ImportSpec "test/someotherpackage"}
2017/10/20 19:50:04 ] -> 0xc420011a40, Type{package "" *ast.ImportSpec "test/someotherpackage"}
2017/10/20 19:50:04 member Type{package "" *ast.ImportSpec "test/someotherpackage"} 'Map' {
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:9:11: expected '}', found ':'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:10:5: expected declaration, found '{'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:11:5: expected declaration, found '{'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:12:1: expected declaration, found '}'
2017/10/20 19:50:04 } -> <nil>
2017/10/20 19:50:04 ] -> 0x0, Type{bad "" <nil> }
parseLocalPackage error: no more package files found
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.SelectorExpr someotherpackage.Map [
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.Ident someotherpackage [
2017/10/20 19:50:04 exprType tuple:false pkg: *ast.ImportSpec "test/someotherpackage" [
2017/10/20 19:50:04 ] -> 0x0, Type{package "" *ast.ImportSpec "test/someotherpackage"}
2017/10/20 19:50:04 ] -> 0xc420011a40, Type{package "" *ast.ImportSpec "test/someotherpackage"}
2017/10/20 19:50:04 member Type{package "" *ast.ImportSpec "test/someotherpackage"} 'Map' {
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:9:11: expected '}', found ':'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:10:5: expected declaration, found '{'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:11:5: expected declaration, found '{'
2017/10/20 19:50:04 $GOPATH/src/test/someotherpackage/test.go:12:1: expected declaration, found '}'
2017/10/20 19:50:04 } -> <nil>
2017/10/20 19:50:04 ] -> 0x0, Type{bad "" <nil> }
godef: no declaration found for someotherpackage.Map
Expected behaviour
It should find the declaration for someotherpackage.Map
, as this is valid go syntax. The code compiles and runs fine !
Reproducible demo
The test can be found here : https://github.com/Gandem/test-issue-godef
Cloning the repo in $GOPATH/test
should work to reproduce the issue.
Note : godef -debug -f test.go Map
works fine when the command is run from $GOPATH/test/someotherpackage
I believe https://github.com/rogpeppe/godef/pull/56 fixes this issue, if anyone has the time to look at it ! (cc @rogpeppe ?)