Skip to content

cmd/go: reports errors for a correct preamble when it contains a builtin C type and a C.* name can't be resolved #50710

Closed
@qmuntal

Description

@qmuntal

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

go version go1.17.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\qmuntaldiaz\AppData\Local\go-build
set GOENV=C:\Users\qmuntaldiaz\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\qmuntaldiaz\go\pkg\mod
set GONOPROXY=github.com/microsoft/*
set GONOSUMDB=github.com/microsoft/*
set GOOS=windows
set GOPATH=C:\Users\qmuntaldiaz\go
set GOPRIVATE=github.com/microsoft/*
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\qmuntaldiaz\code\gotest\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\QMUNTA~1\AppData\Local\Temp\go-build2404042358=/tmp/go-build -gno-record-gcc-switches

What did you do?

==foo.go==

package foo

// int StrLen(_GoString_ s) {
//	return _GoStringLen(s);
// }
import "C"
import "fmt"

func Test() {
	fmt.Println(C.StrLen1("goo"))
}

go tool cgo foo.go

What did you expect to see?

Only this error message:

foo.go:10:14: could not determine kind of name for C.StrLen1

What did you see instead?

I saw the previous error and a preamble error, when the preamble is perfectly fine.

foo.go:10:14: could not determine kind of name for C.StrLen1
cgo: 
gcc errors for preamble:
foo.go:3:13: error: unknown type name '_GoString_'
    3 | // int StrLen(_GoString_ s) {
      |             ^~~~~~~~~~

_GoString_ is defined in the buildin prolog but we are not adding the prolog to the preamble when checking if the preamble has any error:

go/src/cmd/cgo/gcc.go

Lines 492 to 495 in e7d5857

preambleErrors := p.gccErrors([]byte(f.Preamble))
if len(preambleErrors) > 0 {
error_(token.NoPos, "\n%s errors for preamble:\n%s", gccBaseCmd[0], preambleErrors)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions