Skip to content

Fix UnionWeight constructed or populated with non-member elements. - #343

Merged
copybara-service[bot] merged 1 commit into
mainfrom
copybara/994352243
Oct 7, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
copybara/994352243

Conversation

@copybara-service

Copy link
Copy Markdown

Fix UnionWeight constructed or populated with non-member elements.

UnionWeight<W, O> represents the empty set (Zero()) with !first_.Member() and rest_.empty(), and represents NoWeight() (UnionWeight(W::Zero(), W::NoWeight())) with first_ = W::Zero() and rest_ = {W::NoWeight()}. However, constructing UnionWeight(W weight) from a non-member weight or calling PushBack(weight, srt) with a non-member weight on an empty UnionWeight left !first_.Member() while pushing NoWeight() to rest_. Because Size() returns 0 and UnionWeightIterator::Done() is immediately true when !first_.Member(), such weights reported Member() == true, compared equal to Zero(), printed as "EmptySet" instead of "BadSet", and failed to propagate NoWeight() in Plus, Times, Divide, Quantize, Reverse, and Write.

  • In UnionWeight(W weight) and UnionWeight::PushBack, set first_ = W::Zero() when !weight.Member() and !first_.Member(), matching UnionWeight::NoWeight().
  • In UnionWeight::Member(), check if (!first_.Member()) return rest_.empty(); and verify w.Member() across rest_.

`UnionWeight<W, O>` represents the empty set (`Zero()`) with `!first_.Member()` and `rest_.empty()`, and represents `NoWeight()` (`UnionWeight(W::Zero(), W::NoWeight())`) with `first_ = W::Zero()` and `rest_ = {W::NoWeight()}`. However, constructing `UnionWeight(W weight)` from a non-member `weight` or calling `PushBack(weight, srt)` with a non-member `weight` on an empty `UnionWeight` left `!first_.Member()` while pushing `NoWeight()` to `rest_`. Because `Size()` returns `0` and `UnionWeightIterator::Done()` is immediately `true` when `!first_.Member()`, such weights reported `Member() == true`, compared equal to `Zero()`, printed as `"EmptySet"` instead of `"BadSet"`, and failed to propagate `NoWeight()` in `Plus`, `Times`, `Divide`, `Quantize`, `Reverse`, and `Write`.

- In `UnionWeight(W weight)` and `UnionWeight::PushBack`, set `first_ = W::Zero()` when `!weight.Member()` and `!first_.Member()`, matching `UnionWeight::NoWeight()`.
- In `UnionWeight::Member()`, check `if (!first_.Member()) return rest_.empty();` and verify `w.Member()` across `rest_`.

PiperOrigin-RevId: 995132105
@copybara-service
copybara-service Bot merged commit cbd6624 into main Oct 7, 2026
1 check passed
@copybara-service
copybara-service Bot deleted the copybara/994352243 branch October 7, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant