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/cgo: Selector pointer shorthand does not operate with cgo structs named directly #9557

Closed
jyc opened this issue Jan 10, 2015 · 1 comment
Milestone

Comments

@jyc
Copy link

jyc commented Jan 10, 2015

What version of Go are you using (go version)?
go version go1.4 linux/amd64

Issue
The specification states that "if the type of x is a named pointer type and (*x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (*x).f."

When a C variable containing a struct pointer is assigned to a Go variable, the shorthand applies to selector expressions using that variable. However, it seems that the shorthand does not apply in the case that the C variable is named directly, without the use of a temporary variable. I try to illustrate this in the example below:

package main

import (
    "fmt"
)

// struct blah {
//   int a;
// };
//
// struct blah *foo;
//
// void
// blah_alloc() {
//   foo = malloc(sizeof(struct blah));
//   foo->a = 7;
// }
import "C"

type bar struct {
    a int
}

var buzz *bar

func init() {
    buzz = &bar{}
}

func main() {
    // works
    // implicit foo.a -> (*foo).a
    foo := C.foo
    fmt.Println(foo.a)

    // works
    fmt.Println((*C.foo).a)

    // doesn't work, compiler displays an error
    // implicit C.foo -> (*C.foo).a
    fmt.Println(C.foo.a)

    // works
    fmt.Println(buzz.a)
}
@mikioh mikioh changed the title Selector pointer shorthand does not operate with cgo structs named directly spec: Selector pointer shorthand does not operate with cgo structs named directly Jan 10, 2015
@mikioh mikioh changed the title spec: Selector pointer shorthand does not operate with cgo structs named directly cmd/cgo: Selector pointer shorthand does not operate with cgo structs named directly Jan 10, 2015
@minux minux self-assigned this Jan 12, 2015
@minux minux added this to the Go1.5 milestone Jan 12, 2015
@minux
Copy link
Member

minux commented Jan 12, 2015

when rewriting the Go AST, cmd/cgo forgot to handle selectors.

@minux minux closed this as completed in 16993f2 Jan 12, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
@rsc rsc unassigned minux Jun 23, 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