Skip to content

Invalid Go code when initializing array of typedef type #934

Description

@rikki322

c2go incorrectly translates an initializer for an array whose element type comes from a C typedef.
In C, typedef int Integer; makes Integer an alias for int, so initializing an Integer array with integer constants is valid. However, the generated Go code declares the array as []Integer but initializes it with int32(...) elements. If Integer is generated as a distinct Go type, values of type int32 cannot be used directly as Integer values in a slice literal.
To Reproduce

#include <stdio.h>

typedef int Integer;

int main(void) {
    Integer values[3] = {1, 0, 3};

    printf("%d\n", values[0]);
    return 0;
}

Generated Go Code

func main() {
        var values []Integer = []Integer{int32(1), int32(0), int32(3)}
        noarch.Printf((&[]byte("%d\n\x00")[0]), Integer(*&values[0]))
        return
}

Observed Behavior
The generated Go code fails to compile:

# command-line-arguments
./test.go:166:35: cannot use int32(1) (constant 1 of type int32) as Integer value in array or slice literal
./test.go:166:45: cannot use int32(0) (constant 0 of type int32) as Integer value in array or slice literal
./test.go:166:55: cannot use int32(3) (constant 3 of type int32) as Integer value in array or slice literal

The initializer elements should be emitted as Integer(...), or the typedef should be represented as a Go alias if compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions