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

Indefinite Array in Struct Fails on Access #53

Closed
cod3monk opened this issue Feb 24, 2017 · 10 comments
Closed

Indefinite Array in Struct Fails on Access #53

cod3monk opened this issue Feb 24, 2017 · 10 comments

Comments

@cod3monk
Copy link

When compiling the following code with impala (2ab3ab4)

struct CS {
    sizes : [i32],
    count : i32,
}

fn main() -> i32 {
 let cache_sizes = CS {sizes: [32*1024, 256*1024, 20*1024*1024], count: 3}; // in bytes
 
 for i in range(0, cache_sizes.count) {
     print_int(cache_sizes.sizes(i));
 }
 
 0
}

Impala fails with the following message:

Assertion failed: ((!r || dynamic_cast<L*>(r)) && "cast not possible"), function scast, file dev/anydsl/thorin/src/thorin/util/cast.h, line 26.
Abort trap: 6

I don't see the cast here and the same code works if sizes and count are not in a struct but two local variables.

@FanyuYe
Copy link

FanyuYe commented Feb 24, 2017

Hi. For declaring a variable with an array type I used "&[i32]" rather than "[i32]". Hope this helps.

@cod3monk
Copy link
Author

cod3monk commented Feb 24, 2017

@FanyuYe Where? In the struct definition? That leads to lvalue required for unary '&' operand

@FanyuYe
Copy link

FanyuYe commented Feb 24, 2017

This is because you try to assign value to it. So you have to declare it use 'mut' key word.
E.g.
let mut cache_sizes = ...;

@richardmembarth
Copy link
Member

  • You can either use an definite array (of size 3):
struct CS {
    sizes : [i32 * 3],
    count : i32,
}

let cache_sizes = CS {sizes: [32*1024, 256*1024, 20*1024*1024], count: 3}; // in bytes

for i in range(0, cache_sizes.count) {
    print_int(cache_sizes.sizes(i));
} 

Writing to cache_sizes.sizes requires cache_sizes to be mutable: let mut cache_sizes.

  • You can use a reference to an indefinite array:
struct CS {
    sizes : &[i32],
    count : i32,
}   

let cache_sizes = CS {sizes: [32*1024, 256*1024, 20*1024*1024], count: 3}; // in bytes

for i in range(0, cache_sizes.count) {
    print_int(cache_sizes.sizes(i));
}

Writing to cache_sizes.sizes requires then sizes of the CS struct to be mutable: sizes: &mut[i32].

@cod3monk cod3monk changed the title Undefinite Array in Struct Fails on Access Indefinite Array in Struct Fails on Access Feb 24, 2017
@cod3monk
Copy link
Author

The latter does not work here (also not with making cache_size mutable), the first I already had as a work-around.

@cod3monk
Copy link
Author

It is not a big issue, just an annoyance.

@leissa
Copy link
Member

leissa commented Feb 24, 2017

Originally, the idea was to support variable length arrays and flexible array members as in C99. But this has never been thoroughly implemented. Also, we'd like to change a couple of things how anydsl handles arrays in future versions anyway.
However, in the meantime I can add a check in the semantic analysis which rejects such usage of indefinite arrays.

@cod3monk
Copy link
Author

Sounds good, would have saved me some time ;)

leissa added a commit that referenced this issue Feb 24, 2017
@leissa
Copy link
Member

leissa commented Feb 24, 2017

Can I close this bug with the last commit?

@leissa leissa closed this as completed Feb 24, 2017
@cod3monk
Copy link
Author

Works for me. Thanks

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

4 participants