stan-dev / stanc3

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

Turn off unenforce-initialize for assignment of doubles to ad types #1144

Closed SteveBronder closed 2 years ago

SteveBronder commented 2 years ago

Summary

Fixes #1141. The issue is that in c++ std::vector<var> and std::vector<double> are not assignable to one another, so we need to call the assignment that loops over each element of the assigned from vector and does assignment directly to the sub element. If unenforce-initialize is turned on then that vector will not have been filled yet and so we'll be accessing bad memory and getting a seg. This fixes that by checking in find_assignment_idx whether the rhs is array of data types and if so then failing the optimization since the decleration does need to be initialized with NA.

The mre from #1141 was added in the optimization test suite (Much appreciated @joewing!!)

Submission Checklist

Release notes

Bug fix for --O1 when arrays declared in transformed parameters or model were assigned data

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

SteveBronder commented 2 years ago

Ty! How long until our next release? If it's more than two months than a .2 might be good but otherwise I think it's fine to just put this in the next release.