-
-
Notifications
You must be signed in to change notification settings - Fork 379
Direct all std::vector assigns through size-aware version #3352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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/unit
make cpplint
Summary
Closes #3351
Intended Effect
More size mismatch errors are properly caught by Stan
How to Verify
New test added, which crashes on
develop
and 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: