Skip to content

Assert callable arg count matches buffer dims in for_each_element#9214

Open
abadams wants to merge 3 commits into
mainfrom
abadams/buffer_fill_validation_2
Open

Assert callable arg count matches buffer dims in for_each_element#9214
abadams wants to merge 3 commits into
mainfrom
abadams/buffer_fill_validation_2

Conversation

@abadams

@abadams abadams commented Jul 16, 2026

Copy link
Copy Markdown
Member

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:

  • 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})

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.

abadams and others added 2 commits July 16, 2026 12:59
…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

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@c43b345). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant