Skip to content

Mutate abstractly typed arrays in place in with_mutation setters - #200

Merged
sunxd3 merged 2 commits into
mainfrom
fix1519
Oct 21, 2026
Merged

sunxd3 merged 2 commits into
mainfrom
fix1519

Conversation

@yebai

@yebai yebai commented Oct 20, 2026

Copy link
Copy Markdown
Member

set(x, with_mutation(optic), v) returns a copy of x when v is an array assigned at a scalar index and x's element type is not an array type, as with Vector{Any}. BangBang.setindex!! makes the copy because its in-place check rejects that combination. DynamicPPL rebinds the container to the returned value, so under threaded evaluation each task installs its own copy and writes are lost (TuringLang/DynamicPPL.jl#1519).

The setter now calls setindex! and returns x when x isa Array, every index is an integer or CartesianIndex, and v isa eltype(x). Other inputs, including views, static arrays, OffsetArrays and values that need conversion or widening, keep the BangBang path. As a result, x is mutated where it used to be copied, as already happens for concretely typed arrays, and a zero-dimensional x[] now succeeds where it threw.

This works around a false negative in BangBang's possible, and can be removed once that is fixed upstream.

Closes TuringLang/DynamicPPL.jl#1519

`BangBang.setindex!!` copies an `Array` whose element type is not an
array type, such as `Vector{Any}`, when an array value is assigned at a
scalar index, so threaded DynamicPPL tildes lose writes. Call
`setindex!` for an `Array` when the value already has its element type;
other containers keep the BangBang path.

Assisted-by: Codex <codex@openai.com>
@yebai
yebai requested a review from sunxd3 October 20, 2026 21:29
@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.69%. Comparing base (c8e7600) to head (55d198f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #200      +/-   ##
==========================================
+ Coverage   92.63%   92.69%   +0.06%     
==========================================
  Files          17       17              
  Lines        2145     2149       +4     
==========================================
+ Hits         1987     1992       +5     
+ Misses        158      157       -1     

☔ 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.

@github-actions

Copy link
Copy Markdown
Contributor

AbstractPPL.jl documentation for PR #200 is available at:
https://TuringLang.github.io/AbstractPPL.jl/previews/PR200/

Comment thread src/varname/optic.jl Outdated
Comment thread test/varname/optic.jl Outdated
Comment thread test/varname/optic.jl Outdated
Assisted-by: Codex <codex@openai.com>

@sunxd3 sunxd3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sunxd3
sunxd3 merged commit 8ae0e22 into main Oct 21, 2026
17 checks passed
@sunxd3
sunxd3 deleted the fix1519 branch October 21, 2026 10:31
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.

Threaded tildes into a Vector{Any} can drop a write

2 participants