Skip to content

[std][BTree] Fix behavior of ::append to match documentation, ::insert, and ::extend #145628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tinnamchoi
Copy link

@tinnamchoi tinnamchoi commented Aug 19, 2025

Resolves #145614

@rustbot
Copy link
Collaborator

rustbot commented Aug 19, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 19, 2025
@Mark-Simulacrum Mark-Simulacrum added I-libs-api-nominated Nominated for discussion during a libs-api team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Aug 21, 2025
@Mark-Simulacrum
Copy link
Member

BTreeMap::append is documented as:

Moves all elements from other into self, leaving other empty.

If a key from other is already present in self, the respective value from self will be overwritten with the respective value from other.

This (IMO) leaves ambiguous whether equal (by Eq/Ord) keys are updated or not in the implementation. Today, append will update the key as well. This PR is proposing that we align the behavior with insert's, which is documented as not updating the key:

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

This is a breaking change, so nominating for libs-api. We can try running crater, but I'm not sure we'll find much signal given the specific conditions needed to trigger it (BTreeMap, using append, having keys with extra metadata, and writing a test reachable in Crater that exercises this case).

HashMap's extend impl uses insert internally, so it matches insert behavior. BTreeMap's extend also calls BTreeMap::insert in a loop and so does not match append today.

@tinnamchoi, can you also update the docs for append to reflect this change?

@tinnamchoi tinnamchoi changed the title [std][BTree] Fix behavior of ::append to match documentation and ::insert [std][BTree] Fix behavior of ::append to match documentation, ::insert, and ::extend Aug 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-libs-api-nominated Nominated for discussion during a libs-api team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

{BTreeMap,BTreeSet}::append behaves differently to ::insert and ::extend
4 participants