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

Issues with extern C functions receiving/returning structs by value with floating point members on x86_64 #7415

Closed
SiegeLord opened this issue Jun 26, 2013 · 1 comment
Labels
A-codegen Area: Code generation

Comments

@SiegeLord
Copy link
Contributor

Note that I only tested this with a quad of f32's, I imagine it fails for several other numbers/sizes of floats too. Tested with rust at dc4560d.

You need two files, test.c and test.rs:

test.c:

#include <stdio.h>

typedef struct
{
    float a, b, c, d;
} A;

A get()
{
    A a = {1, 2, 3, 4};
    return a;
}

void test(A a)
{
    printf("%f %f %f %f\n", a.a, a.b, a.c, a.d);
}

test.rs:

struct A
{
    a : f32,
    b : f32,
    c : f32,
    d : f32,
}

#[link_args = "-ltest"]
extern "C"
{
    fn get() -> A;
    fn test(a : A);
}

fn main()
{
    unsafe
    {
        let a = get();
        println(fmt!("%?", a));
        let b = A{a: 0.1, b: 0.2, c: 0.3, d: 0.4};
        test(b);
    }
}

Compile using this code:

gcc -c test.c
ar -cvq libtest.a test.o
rustc test.rs -L.

The output when running test on my system is:

./test
{a: 0, b: 1, c: 0, d: 0}
0.100000 0.200000 0.200000 0.000000

The expected output is:

./test
{a: 1, b: 2, c: 3, d: 4}
0.100000 0.200000 0.300000 0.400000
@brson
Copy link
Contributor

brson commented Jun 27, 2013

Related to #5744 #5745

@bors bors closed this as completed in a2227f9 Jun 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

2 participants