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

cmd/compile: unsafe.Pointer not treated as a pointer for embedding #6357

Closed
griesemer opened this issue Sep 10, 2013 · 6 comments
Closed

cmd/compile: unsafe.Pointer not treated as a pointer for embedding #6357

griesemer opened this issue Sep 10, 2013 · 6 comments

Comments

@griesemer
Copy link
Contributor

http://play.golang.org/p/YhdG1ZoVzp

Per the spec: "An embedded type must be specified as a type name T or as a pointer
to a non-interface type name *T, and T itself may not be a pointer type."  (
http://tip.golang.org/ref/spec#Struct_types )

gc issues an error for line 9 (as expected), but not for line 10. Arguably, an
unsafe.Pointer is a pointer and thus line 10 should not be legal.

The spec is not very clear about whether an unsafe.Pointer should act like a pointer in
these situations, but:

1. The documentation (by pseudo code) defines unsafe.Pointer as *ArbitraryType (
http://tip.golang.org/ref/spec#Package_unsafe )

2. For the purposes of method receivers, they are treated as pointers (see issue #6116)

3. It's ok to assign nil to it (see line 15 of the above code).

4. It's called a "Pointer".
@griesemer
Copy link
Contributor Author

Comment 1:

PS: gccgo reports an error for line 9 and 10:
x.go:9:6: error: embedded type may not be a pointer
      P1
      ^
x.go:10:6: error: embedded type may not be a pointer
      P2
      ^

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 2:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 4:

Labels changed: added repo-main.

@rsc rsc removed the compiler-bug label Apr 10, 2015
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: unsafe.Pointer not treated as a pointer for embedding cmd/compile: unsafe.Pointer not treated as a pointer for embedding Jun 8, 2015
tycho added a commit to tycho/go-sdl2 that referenced this issue Dec 26, 2015
With gccgo I was getting this:

    $ go build
    # github.com/veandco/go-sdl2/sdl_mixer
    ./sdl_mixer.go:83:2: error: embedded type may not be a pointer
      unsafe.Pointer
      ^

Apparently upstream Go hasn't yet added the above error[1].

Fixes veandco#167.

[1] golang/go#6357

Signed-off-by: Steven Noonan <steven@uplinklabs.net>
@odeke-em
Copy link
Member

odeke-em commented Jun 6, 2020

Good news @griesemer, this issue has been fixed since at least Go1.7 (I couldn't get Go1.6 and earlier to build on my Macbook Pro) and it rightly matches gccgo's behavior producing

Go1.7

$ go version && go run main.go 
go version go1.7.6 darwin/amd64
# command-line-arguments
./main.go:9: embedded type cannot be a pointer
./main.go:10: embedded type cannot be a pointer

Go1.8

 go version && go run main.go 
go version go1.8.7 darwin/amd64
# command-line-arguments
./main.go:9: embedded type cannot be a pointer
./main.go:10: embedded type cannot be a pointer

Go1.9

$ go version && go run main.go 
go version go1.9.7 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

Go1.10

$ go version && go run main.go 
go version go1.10.8 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

Go1.11

$ go version && go run main.go 
go version go1.11.13 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

Go1.12

$ go version && go run main.go 
go version go1.12.16 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

Go1.13

$ go version && go run main.go 
go version go1.13.11 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

Go1.14

$ go version && go run main.go 
go version go1.14.3 darwin/amd64
# command-line-arguments
./main.go:9:2: embedded type cannot be a pointer
./main.go:10:2: embedded type cannot be a pointer

I shall let you close this issue as you please. Thank you.

@griesemer
Copy link
Contributor Author

Appears fixed per the above comment. Verified in latest playground. Closing.

@golang golang locked and limited conversation to collaborators Aug 12, 2021
@rsc rsc removed their assignment Jun 22, 2022
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

4 participants