Skip to content

cmd/cgo: cannot define new methods on non-local type in 1.24 #71917

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
3v1n0 opened this issue Feb 23, 2025 · 4 comments
Closed

cmd/cgo: cannot define new methods on non-local type in 1.24 #71917

3v1n0 opened this issue Feb 23, 2025 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@3v1n0
Copy link

3v1n0 commented Feb 23, 2025

Go version

go version go1.24.0 linux/amd64

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=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/marco/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/marco/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3394646905=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/marco/Dev/authd/go.mod'
GOMODCACHE='/tmp/go-path/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/tmp/go-path'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/snap/go/10853'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/marco/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/snap/go/10853/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

We had some code doing:

package main

/*
#include <stdlib.h>

typedef struct {
        int value;
} Struct;
*/
import "C"
import "fmt"

type CStruct = C.Struct

func (s *CStruct) init(value int) {
	s.value = C.int(value)
}

func main() {
	s := &CStruct{}
	s.init(1234)
	fmt.Println(s.value)
}

What did you see happen?

./main.go:16:10: cannot define new methods on non-local type CStruct

What did you expect to see?

This used to work until go 1.23, now it's not allowed anymore.

@seankhliao seankhliao changed the title go 1.24 breaks some CGO usage: cannot define new methods on non-local type CStruct cmd/cgo: cannot define new methods on non-local type in 1.24 Feb 23, 2025
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 23, 2025
@seankhliao
Copy link
Member

see #60725 it's considered a bug as it's not allowed on go type aliases.

@3v1n0
Copy link
Author

3v1n0 commented Feb 24, 2025

So using instead type CStruct C.Struct fixes it, is that considered fine instead?

3v1n0 added a commit to 3v1n0/authd that referenced this issue Feb 24, 2025
By using type alias instead we were getting compile errors in Go 1.24

See: golang/go#71917
3v1n0 added a commit to ubuntu/authd that referenced this issue Feb 24, 2025
Fix the definition as it wouldn't work in newer go:

```
# github.com/ubuntu/authd/pam/internal/gdm [github.com/ubuntu/authd/pam/internal/gdm.test]
./extension.go:91:12: cannot define new methods on non-local type jsonProtoMessage
./extension.go:106:12: cannot define new methods on non-local type jsonProtoMessage
./extension.go:115:12: cannot define new methods on non-local type jsonProtoMessage
./extension.go:119:12: cannot define new methods on non-local type jsonProtoMessage
./extension.go:123:12: cannot define new methods on non-local type jsonProtoMessage
./extension.go:137:12: cannot define new methods on non-local type jsonProtoMessage
FAIL	github.com/ubuntu/authd/pam/internal/gdm [build failed]
```

See: golang/go#71917

UDENG-6160
@ianlancetaylor
Copy link
Member

@3v1n0 Sure, that is fine. What is forbidden is defining methods on a C type. With type CStruct C.Struct you will be defining methods on the Go type CStruct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

5 participants