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/gc: cannot use a named unsafe.Pointer as receiver type #6116

Closed
dvyukov opened this issue Aug 12, 2013 · 8 comments
Closed

cmd/gc: cannot use a named unsafe.Pointer as receiver type #6116

dvyukov opened this issue Aug 12, 2013 · 8 comments
Milestone

Comments

@dvyukov
Copy link
Member

dvyukov commented Aug 12, 2013

parent: 17677:d7db8c804ffa tip, darwin/amd64

package main
import "unsafe"
type T unsafe.Pointer
func (t *T) foo() {}
func main() {}

/tmp/unsafe.go:4: invalid receiver type *T (*T / unsafe.Pointer)
@cznic
Copy link
Contributor

cznic commented Aug 12, 2013

Comment 1:

unsafe.Pointer[0] is a pointer type thus both T and *T are invalid receiver types. IMO
#workingAsIntended
> type Pointer *ArbitraryType
> type ArbitraryType int
  [0]: http://localhost:6060/pkg/unsafe/#Pointer

@ianlancetaylor
Copy link
Contributor

Comment 3:

I agree that this seems to me to be working as intended.  Dmitriy, did you have a
specific use for a method on a named variant of unsafe.Pointer?

@dvyukov
Copy link
Member Author

dvyukov commented Aug 19, 2013

Comment 4:

Here is where I want to use it:
https://code.google.com/p/go/source/browse/src/pkg/sync/cond.go#110
unsafe.Pointer would have less casts than uintptr.
A similar use case would be:
type ConfigPtr unsafe.Pointer
func (p *ConfigPtr) GetCurrent() *Config {
    return (*Config)(atomic.LoadPointer(p))
}
func (p *ConfigPtr) UpdateCurrent(c *Config) {
    atomic.StorePointer(p, unsafe.Pointer(c))
}
var currentConfig ConfigPtr

@cznic
Copy link
Contributor

cznic commented Aug 19, 2013

Comment 5:

What about:
----
type ConfigPtr struct { unsafe.Pointer }
func (p *ConfigPtr) GetCurrent() *Config {
    return (*Config)(atomic.LoadPointer(p.Pointer))
}
func (p *ConfigPtr) UpdateCurrent(c *Config) {
    atomic.StorePointer(p.Pointer, unsafe.Pointer(c))
}
var currentConfig ConfigPtr
----
?

@dvyukov
Copy link
Member Author

dvyukov commented Aug 19, 2013

Comment 6:

Well, this is another possible workaround. I've used uintptr to work around this.
I am not saying that this is particularly important.
But it's nice that you can do 'type Foo int' instead of 'type Foo struct {foo int}'.

@dvyukov
Copy link
Member Author

dvyukov commented Aug 19, 2013

Comment 7:

Rob, Robert, please confirm whether it's allowed by spec or not. If the compiler does
correct thing according to spec, then we just close this, this is not a feature request.

@robpike
Copy link
Contributor

robpike commented Aug 19, 2013

Comment 8:

The spec does not exactly say that unsafe.Pointer is a pointer. It just says a pointer
can be converted to/from an unsafe.Pointer. However, an example defines it as
*ArbitraryType, as does the definition in the library docs. Thus, I claim that
unsafe.Pointer is a pointer and thus this is WorkingAsIntended.
Reopen if you disagree, but I think it's reasonably clear.

Status changed to WorkingAsIntended.

@dvyukov
Copy link
Member Author

dvyukov commented Aug 19, 2013

Comment 9:

OK, thanks. I am not disagree.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

6 participants