Assert callable arg count matches buffer dims in for_each_element#9214
Open
abadams wants to merge 3 commits into
Open
Assert callable arg count matches buffer dims in for_each_element#9214abadams wants to merge 3 commits into
abadams wants to merge 3 commits into
Conversation
…lement
Buffer::for_each_element previously accepted a callable taking fewer int
arguments than the buffer's dimensionality, silently iterating only over a
reduced-dimensionality slice. A nullary callable (e.g. buffer.fill(rng))
therefore wrote only the origin element. This is an easy mistake to make and
produces no error.
Require the callable to take either a const int *, or exactly as many int
arguments as the buffer has dimensions. This uncovered three real bugs where
buffers were only ever having their first element randomized:
- test/fuzz/lossless_cast.cpp: buf.fill(fuzz)
- test/correctness/interleave_rgb.cpp: fill([]() { return rand(); })
- test/correctness/multiple_scatter.cpp: fill(std::mt19937{0})
All three are fixed to use for_each_value, which passes an element reference
and does not depend on coordinates. The reduced-slice usage in
test/correctness/for_each_element.cpp is rewritten to slice off a channel
first so the callable's arg count matches the iteration domain.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A zero-argument callable passed to fill (for example a random number generator) has no coordinate to consume, so there is no distinction between filling via for_each_element and for_each_value. Dispatch such callables to for_each_value, which evaluates the callable once per element. This makes the natural buffer.fill(rng) idiom fill the whole buffer instead of tripping the for_each_element arg-count assertion. With this, the previously-broken buffer initializations in interleave_rgb, multiple_scatter, and lossless_cast (which only ever randomized the origin element) are fixed by reverting them to their original fill(rng) form. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The for_each_element arg-count assertion rejects a callable taking fewer int arguments than the buffer's dimensionality. These calls iterated a 2D domain over a 3D buffer and handled all channels inside the callable, so slice off the channel dimension first to get a matching 2D domain. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9214 +/- ##
=======================================
Coverage ? 69.83%
=======================================
Files ? 255
Lines ? 78574
Branches ? 18789
=======================================
Hits ? 54874
Misses ? 18162
Partials ? 5538 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Buffer::for_each_element previously accepted a callable taking fewer int arguments than the buffer's dimensionality, silently iterating only over a reduced-dimensionality slice. Buffer::fill uses Buffer::for_each_element. A nullary callable (e.g. buffer.fill(rng)) therefore wrote only the origin element. This is an easy mistake to make and produces no error.
This PR requires a callable passed to for_each_element to take either a const int *, or exactly as many int arguments as the buffer has dimensions. This uncovered three real bugs where buffers were only ever having their first element randomized:
Buffer::fill was changed to defer to for_each_value when passed a zero-arg callable. This is unambiguous, because it behaves in exactly the same way as if it deferred to for_each_element for scalar buffers. The reduced-slice usage in test/correctness/for_each_element.cpp is rewritten to slice off a channel first so the callable's arg count matches the iteration domain.
This is a breaking change. It will cause user code that uses this iterate-over-a-slice feature to assert-fail. I give 50/50 odds whether such user code is working as intended, so I think this is worth it. It may also break user code that is relying on fill(rng) to only initialize the first element, but surely such code is buggy.