Direct all std::vector assigns through size-aware version #3352
Direct all std::vector assigns through size-aware version #3352
Conversation
| // If we've made it this far, we need x to have elements to assign to, | ||
| // and we know this is either a no-op or x has size 0 | ||
| x.resize(y.size()); | ||
| if constexpr (std::is_rvalue_reference_v<U&&>) { |
There was a problem hiding this comment.
If we are trying to fix the case of the below does this do what we intend?
parameters {
array[1] vector[1] theta;
}
transformed parameters {
array[1] vector[2] wrong_size;
wrong_size = theta;
}
idt we want to resize wrong_size here. Shouldn't this throw an error?
There was a problem hiding this comment.
The first part of this function checks the sizes match if x is not size 0, and will throw an exception.
So, when this line executes it is guaranteed that x.size == y.size OR x.size == 0. If the sizes match, this resize does nothing.
But if x was size 0, and we can’t just directly assign to it, we need to resize it or else x[i] will blow up in the following loop
There was a problem hiding this comment.
In particular, this is the test I added, and the test still asserts that this throws an error, it just no longer crashes.
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
Submission Checklist
./runTests.py src/test/unitmake cpplintSummary
Closes #3351
Intended Effect
More size mismatch errors are properly caught by Stan
How to Verify
New test added, which crashes on
developand passes on this branch.Side Effects
Documentation
Copyright and Licensing
Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):
Simons Foundation
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses: