Skip to content

go/types: invalid identifiers are no longer type checked #71777

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

Closed
aykevl opened this issue Feb 16, 2025 · 5 comments
Closed

go/types: invalid identifiers are no longer type checked #71777

aykevl opened this issue Feb 16, 2025 · 5 comments
Assignees
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@aykevl
Copy link

aykevl commented Feb 16, 2025

Go version

go version go1.24.0 linux/arm64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/ayke/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/ayke/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1340575841=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/ayke/src/tinygo/tinygo/go.mod'
GOMODCACHE='/home/ayke/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ayke'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go1.24.0'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/ayke/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go1.24.0/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I ran the code here: https://go.dev/play/p/SzYsA_F4wwR

What did you see happen?

In Go 1.23, it reports the following error:

got type checking error: -: undefined: C.FOO
got type checking error: test.go:5:6: undefined: BAR

In Go 1.24, it reports only the following error:

got type checking error: test.go:5:6: undefined: BAR

What did you expect to see?

I expected the C.FOO undefined error to still be reported.

This is a regression from 5bd442a (see #68183). We relied on the previous behavior for CGo support in TinyGo: we replaced all C.foo strings with a literal "C.foo" identifier and defined functions and constants and such with a similar name. This worked very well and led to nice error messages (such as the "undefined: C.FOO" message above, instead of having to use name mangling).

I'm not sure how to best fix this. I see that 5bd442a fixes a real bug, but in our case it's a regression. We can use name mangling to work around this, making the CGo error messages less nice. We can do some search-and-replace on the error messages to work around the name mangling, but I'm afraid that's going to be fragile. Name mangling is probably the easiest short-term fix (since we need Go 1.24 support) but if there's any way this can be fixed upstream that would be great.

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 16, 2025
aykevl added a commit to tinygo-org/tinygo that referenced this issue Feb 16, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
aykevl added a commit to tinygo-org/tinygo that referenced this issue Feb 16, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
aykevl added a commit to tinygo-org/tinygo that referenced this issue Feb 16, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@ianlancetaylor ianlancetaylor added this to the Go1.25 milestone Feb 17, 2025
@ianlancetaylor
Copy link
Contributor

Setting milestone to 1.25 since this affects TinyGo. If we decide to fix this we should backport to fix to 1.24.

aykevl added a commit to tinygo-org/tinygo that referenced this issue Feb 18, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
@aykevl
Copy link
Author

aykevl commented Feb 18, 2025

I have a workaround for now, using name mangling.

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 18, 2025
@griesemer griesemer self-assigned this Feb 19, 2025
aykevl added a commit to tinygo-org/tinygo that referenced this issue Feb 21, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
deadprogram pushed a commit to tinygo-org/tinygo that referenced this issue Feb 25, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
deadprogram pushed a commit to tinygo-org/tinygo that referenced this issue Feb 25, 2025
This mangles CGo identifier names to something like "_Cgo_foo" instead
of using literal identifiers like "C.foo". This works around
golang/go#71777.

I don't like this solution, but I hope we'll find a better solution in
the future. In that case we can revert this commit.
@griesemer
Copy link
Contributor

Hi @aykevl ; I looked at this some more and I don't see an obvious "right" solution on the parser/type-checker side: the general problem is that we don't want to repeat errors reported by the parser side of things (incl. scanner, etc) in the type checker; i.e., the type checker "knows" (within reason) what the parser has checked already. Your code explicitly manipulates the AST and makes it incorrect.

Currently (with the fix for #68183) this knowledge is implicit, in the code: the type checker knows to not complain about invalid identifiers.

A more general approach might be to have a bit (a bool) in the Ident signaling to the type checker that an error was already reported by the parser (and in general this might apply to any node). Perhaps no extra bit is needed, but a singleton "invalid identifier" can be used to communicate this situation. We already have a BadExpr node, but we cannot easily use that for a bad identifier w/o changing significant parts of the go/ast API.

Alternatively, the parser could ignore the error and let the type checker do all the work: that would solve your problem but it would also make the parser lenient in a way that seems wrong - after all its job is to check the grammar.

A 4th option might be to have a types.Config flag to enable this (and possibly other) "strict" checking in the type checker, say for situations where there is no parser, and the AST is generated somehow (or updated).

All of these are doable but all of these seem not commensurate with the problem at hand or might require API changes that we'd rather not do.

Since name mangling seems to work for you, I am inclined to do nothing here.
Feel free to re-open if you have a better and more commensurate suggestion than what I sketched out above; thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants