Indefinite Array in Struct Fails on Access
cod3monk opened this issue · 10 comments
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.
Hi. For declaring a variable with an array type I used "&[i32]" rather than "[i32]". Hope this helps.
@FanyuYe Where? In the struct definition? That leads to lvalue required for unary '&' operand
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 = ...;
- 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]
.
The latter does not work here (also not with making cache_size mutable), the first I already had as a work-around.
It is not a big issue, just an annoyance.
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.
Sounds good, would have saved me some time ;)
Can I close this bug with the last commit?
Works for me. Thanks