Dimension mismatch in assignment not caught within partial_sum function for reduce_sum

I’m running a model using reduce_sum. Just realised that I have a dimension mismatch error in my code that isn’t being picked up at compilation. For simplicity, within the partial_sum function for a reduce_sum operation I have the following statement:

int test[3] = j[1:10];

so I’m assigning an array of length 10 to an int array that is declared to be of length 3.

This compiles, and behaves as if test is of length 10.

Shouldn’t it error?

1 Like

Interesting. Yeah I would think you should get an error. Tagging @bbbales2 and @wds15 who can probably give you a more definitive answer about what’s going on. It might help them if you could share a reproducible example (even a toy one).

I think it should error. This is a known bug in Stan that we haven’t removed for a long time. I think it’s the one we haven’t removed because some people used it for ragged arrays, and so informally we’ve been waiting for actual ragged arrays or something like that.

Again, unsupported so it’s all up in the air and it might disappear at any second :D.

This should throw a runtime error:

int test[3];
test[1:3] = j[1:10];

What is happening with your current statement is more like:

int test[3];
test = j[1:10];

In the first thing, you are trying to assign 10 things from inside j to 3 things inside test. In the second thing, you are reassigning what the named variable test is. Does that make sense?

1 Like

This is the relevant issue for this case: https://github.com/stan-dev/stanc3/issues/523

2 Likes