golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.11k stars 17.68k forks source link

x/cmd/gotype: undeclared name: _Ctype_struct_bpf_program #25439

Open gustafj opened 6 years ago

gustafj commented 6 years ago

Cgo struct types of the form _Ctype_struct_ seems to be unknown to gotype.

What version of Go are you using (go version)?

go version go1.10.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64" GOOS="linux" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1"

What did you do?

Lint using gotype a dummy package which imports github.com/google/gopacket/pcap.

$ go get -u github.com/google/gopacket/pcap

$ cat $GOPATH/src/github.com/google/dummy/dummy.go
package dummy
import _ "github.com/google/gopacket/pcap"

$ gotype dummy.go

What did you expect to see?

No output (as in everything is OK).

What did you see instead?

dummy.go:2:10: could not import github.com/google/gopacket/pcap (type-checking package "github.com/google/gopacket/pcap" failed ($GOPATH/src/github.com/google/gopacket/pcap/pcap.go:533:49: undeclared name: _Ctype_struct_bpf_program))

Running gotype on github.com/google/gopacket/pcap directly gives:

$ gotype pcap.go
pcap.go:533:49: undeclared name: _Ctype_struct_bpf_program
pcap.go:203:7: undeclared name: _Ctype_struct_bpf_program
pcap.go:661:66: undeclared name: _Ctype_struct_bpf_program
pcap.go:801:34: undeclared name: _Ctype_struct_pcap_addr
pcap.go:401:17: invalid operation: p.pkthdr (variable of type *invalid type) has no field or method ts
pcap.go:403:19: invalid operation: p.pkthdr (variable of type *invalid type) has no field or method ts
pcap.go:406:27: invalid operation: p.pkthdr (variable of type *invalid type) has no field or method caplen
pcap.go:407:20: invalid operation: p.pkthdr (variable of type *invalid type) has no field or method len
pcap.go:425:4: invalid operation: p (variable of type *Handle) has no field or method waitForPacket
pcap.go:488:13: undeclared name: _Ctype_struct_pcap_stat
bcmills commented 6 years ago

(CC: @alandonovan @griesemer )

griesemer commented 6 years ago

go/types doesn't know anything about C; it only knows about the .go files (or package) it is presented with. Since go/types is not run via the go command, cgo is not run and it doesn't see the cgo generated files (which contain the relevant definitions).

When go/types sees an import "C", it "fakes" that import and silently suppresses all errors that would arise from qualified identifiers of the form C.foo . Thus it "works" ok with C.foo style identifiers.

There's no easy fix.