Skip to content

cmd/cgo: Selector pointer shorthand does not operate with cgo structs named directly #9557

@jyc

Description

@jyc

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)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions