stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

[BUG] Array literals in `return` statements may generate wrong type in C++ #1334

Closed WardBrian closed 11 months ago

WardBrian commented 11 months ago

Current Behavior:

A literal like { 0.0 } is always marked as DataOnly and generated as std::vector<double>, even when it is in a return position that requires the type to be autodiff.

This can lead to the C++ compiler to fail as a function which is meant to return std::vector<var> tries to return std::vector<double>

Model Code

functions {
  array[] real foo(real a) {
    return {0.1};
  }
}
parameters {
  real a;
}
model {
  array[1] real z = foo(a);
}

Environment:

stan 2.32.2

Anything else:

First noticed on forums: https://discourse.mc-stan.org/t/compilation-error-passes-syntax-check/32059