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

Fixed size arrays broken with -O3 #38

Open
stlemme opened this issue Nov 4, 2016 · 1 comment
Open

Fixed size arrays broken with -O3 #38

stlemme opened this issue Nov 4, 2016 · 1 comment

Comments

@stlemme
Copy link
Member

stlemme commented Nov 4, 2016

The following code leads to correct results with -O2 but wrong with -O3

fn main() -> () {
    let N = 4;
    let mut A = [N:i32];
    let mut B = ~[N:i32];
    let mut C = [0, 0, 0, 0];
    let mut D = [N:i32];
    
    for i in $range(0, N) {
        A(i) = i*i;
        B(i) = i*i;
        C(i) = i*i;
    }
    
    D(0) = 0;
    D(1) = 1;
    D(2) = 4;
    D(3) = 9;
    // D(4) = 16; // should break, but doesn't
    
    print_string("A: "); print_array(bitcast[&[i32]](&A), N); print_string("\n");
    print_string("B: "); print_array(bitcast[&[i32]]( B), N); print_string("\n");
    print_string("C: "); print_array(bitcast[&[i32]](&C), N); print_string("\n");
    print_string("D: "); print_array(bitcast[&[i32]](&D), N); print_string("\n");
}

fn print_array(arr: &[i32], num: i32) -> () {
    print_string("["); print_int(arr(0));
    for i in $range(1, num) {
        print_string(", "); print_int(arr(i));
    }
    print_string("]");
}

Output with -O3:

A: [0, 0, 0, 0]
B: [0, 1, 4, 9]
C: [0, 1, 4, 9]
D: [0, 0, 0, 0]
@leissa
Copy link
Member

leissa commented Nov 4, 2016

let mut A = [N:i32];

creates an indefinite array on the stack. Code generation is currently broken for this case. This is a known issue. You either have to use a heap allocated indefinite array or a stack-allocated definite array.

I'll keep this bug report open, until this issue is fixed. But it will take a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants