Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: Const objects from other packages have nil spec (and panic on .GetPos()) #5143

Closed
gopherbot opened this issue Mar 28, 2013 · 3 comments

Comments

@gopherbot
Copy link

by qslack:

Using go/types' Check on the following program:

package p
import "unicode/utf8"
var foo = utf8.UTFMax

with a types.Context.Ident func results in the func being called for the UTFMax ident.
However, the types.Const object associated with the ident has a nil spec member and
therefore panics when .GetPos() is called on it.

The following test (place in go.exp/go/types/objects_test.go) reproduces this bug:

package types

import (
    "go/ast"
    "go/parser"
    "go/token"
    "testing"
)

func TestConstGetPos(t *testing.T) {
    fset := token.NewFileSet()
    file, _ := parser.ParseFile(fset, "test.go", `package p; import "unicode/utf8"; var foo = utf8.UTFMax`, 0)
    objs := make([]Object, 0)
    c := Context{
        Ident: func(id *ast.Ident, obj Object) {
            objs = append(objs, obj)
        },
    }
    c.Check(fset, []*ast.File{file})
    for _, obj := range objs {
        obj.GetPos() // should not panic
    }
}
 
The panic output is:

$ go test go.exp/go/types
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x8 pc=0x454fc0]

goroutine 7 [running]:
testing.func��004()
    /home/sqs/src/go/src/pkg/testing/testing.go:341 +0xbc
go.exp/go/types.(*Const).GetPos(0xc2003efa50, 0x13)
    /home/sqs/src/go.exp/go/types/objects.go:101 +0x20
go.exp/go/types.TestConstGetPos(0xc200431900)
    /home/sqs/src/go.exp/go/types/objects_test.go:21 +0x247
testing.tRunner(0xc200431900, 0x71fee8)
    /home/sqs/src/go/src/pkg/testing/testing.go:346 +0x8a
created by testing.RunTests
    /home/sqs/src/go/src/pkg/testing/testing.go:426 +0x86b

goroutine 1 [chan receive]:
testing.RunTests(0x644028, 0x71fea0, 0x8, 0x8, 0x5f5001, ...)
    /home/sqs/src/go/src/pkg/testing/testing.go:427 +0x88e
testing.Main(0x644028, 0x71fea0, 0x8, 0x8, 0x7250c0, ...)
    /home/sqs/src/go/src/pkg/testing/testing.go:358 +0x8a
main.main()
    go.exp/go/types/_test/_testmain.go:57 +0x9a
FAIL    go.exp/go/types 0.244s



What is the expected output? Calling obj.GetPos() should return the Pos of the UTFMax
declaration.


What do you see instead? Calling obj.GetPos() causes a panic.


Which compiler are you using (5g, 6g, 8g, gccgo)? 6g


Which operating system are you using? Linux


Which version are you using? go version devel +0ab15b04070e Wed Mar 13 07:45:31 2013
-0700 linux/amd64
@griesemer
Copy link
Contributor

Comment 1:

Owner changed to @griesemer.

Status changed to Accepted.

@griesemer
Copy link
Contributor

Comment 2:

Thanks for the testcase; much appreciated!
See: https://golang.org/cl/8178046

@griesemer
Copy link
Contributor

Comment 3:

This issue was closed by revision golang/exp@384dfda.

Status changed to Fixed.

zend pushed a commit to go-src/inotify that referenced this issue Apr 3, 2015
Missed one case, apparently. Changes all cases
to look the same.

Fixes golang/go#5143.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/8178046
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants