Skip to content

Remove ^= as mutation for |= and &= operators#556

Merged
sourcefrog merged 2 commits intomainfrom
copilot/fix-e3f78709-2b91-4632-8c44-4621cea6bfda
Oct 6, 2025
Merged

Remove ^= as mutation for |= and &= operators#556
sourcefrog merged 2 commits intomainfrom
copilot/fix-e3f78709-2b91-4632-8c44-4621cea6bfda

Conversation

Copy link
Contributor

Copilot AI commented Oct 6, 2025

Problem

When mutating bitwise assignment operators, cargo-mutants was suggesting replacing |= with ^= (and &= with ^=). However, this mutation is uninformative for the common pattern of accumulating bits into a bitmap that starts from zero:

let base_ix = self.text.len();
self.chars |= slice.chars << base_ix;
self.chars_utf16 |= slice.chars_utf16 << base_ix;

In this code (from Zed's crates/rope/src/chunk.rs), when accumulating new set bits into a bitmap that's initially zeros, |= and ^= produce identical results. The mutation doesn't actually test anything different.

Solution

Changed the mutation patterns for bitwise assignment operators:

  • &= now only mutates to |= (removed ^=)
  • |= now only mutates to &= (removed ^=)
  • ^= still mutates to both |= and &= (unchanged)

This makes the mutations more meaningful by ensuring they actually change the behavior in typical usage patterns.

Changes

  • Updated mutation logic in src/visit.rs
  • Updated documentation in book/src/mutants.md with explicit entries for bitwise assignment operators
  • Added changelog entry to NEWS.md
  • Updated test expectations in mutate_assign_ops test
Original prompt

This section details on the original issue you should resolve

<issue_title>Reconsider replace |= with ^=</issue_title>
<issue_description>```rust
#[inline(always)]
pub fn append(&mut self, slice: ChunkSlice) {
if slice.is_empty() {
return;
};

    let base_ix = self.text.len();
    self.chars |= slice.chars << base_ix;
    self.chars_utf16 |= slice.chars_utf16 << base_ix;
    self.newlines |= slice.newlines << base_ix;
    self.tabs |= slice.tabs << base_ix;
    self.text.push_str(slice.text);
}

In this code from Zed `crates/rope/src/chunk.rs` we're accumulating new set bits into a bitmap that's initially zeros. It's probably a common pattern. Warning that the or might be xor may not be helpful enough?</issue_description>

<agent_instructions>Change the code in visit.rs so that it no longer mutates `|=` to `^=`. Update the table of patterns in book/src/. Mention it in the news. Update the tests.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #550

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Reconsider replace |= with ^= Remove ^= as mutation for |= and &= operators Oct 6, 2025
Copilot AI requested a review from sourcefrog October 6, 2025 13:55
Copilot AI and others added 2 commits October 6, 2025 07:08
Co-authored-by: sourcefrog <346355+sourcefrog@users.noreply.github.com>
@sourcefrog sourcefrog force-pushed the copilot/fix-e3f78709-2b91-4632-8c44-4621cea6bfda branch from 3b8c14a to ffec8db Compare October 6, 2025 14:10
@sourcefrog sourcefrog marked this pull request as ready for review October 6, 2025 19:19
@sourcefrog sourcefrog merged commit d9cc669 into main Oct 6, 2025
55 checks passed
@sourcefrog sourcefrog deleted the copilot/fix-e3f78709-2b91-4632-8c44-4621cea6bfda branch October 6, 2025 19:19
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.

Reconsider replace |= with ^=

2 participants