Skip to content

Allocator: Document memory ownership, remove disjointness - #165921

Open
maxdexh wants to merge 1 commit into
rust-lang:mainfrom
maxdexh:this-mailbox-is-mine
Open

maxdexh wants to merge 1 commit into
rust-lang:mainfrom
maxdexh:this-mailbox-is-mine

Conversation

@maxdexh

@maxdexh maxdexh commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

View all comments

Discussion on zulip

The disjointness requirement is incorrect (for any reasonable interpretation), and makes it unsound to compose allocators. We really want our types to be composable; allocators handing out memory acquired from other allocators makes a lot of sense.

The problem is that such "wrapper allocators" can be composed into a cycle, in which case they end up handing out memory from themselves, in a manner that other wrappers can observe is non-disjoint (see the example from @theemathas in the zulip thread).

This PR removes the disjointness part of the docs and replaces it with an overall contract about ownership: An allocator generally cannot return the same memory twice, but not because it overlaps; instead, the argument is that it lacks ownership of the memory it has already returned, similar to how we already documented that it cannot modify the memory it has returned; that memory is owned by the caller.

The ownership system also implies memory disjointness in cases where you can observe two memory blocks being exclusively owned; see #165921 (comment). This is necessary for &mut and copy_nonoverlapping to be sound to use on them, e.g. through Box.

As a result of this rephrasing, it is okay for cyclically composed allocators to return the same memory reentrantly, because they have transferred ownership of it to themselves (the caller), and are thus free to pass it on, as if it was from any other allocator.

cc @theemathas

r? @nia-e

@rustbot rustbot added A-allocators Area: Custom and system allocators 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 Nov 13, 2026
@rustbot

rustbot commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

nia-e is currently at their maximum review capacity.
They may take a while to respond.

@nia-e

nia-e commented Nov 13, 2026

Copy link
Copy Markdown
Member

this is in line with the discussion on Zulip. cc @rust-lang/libs-ping - should we FCP this? I think this is mostly formalising the semantics we already wanted but may be worth doing regardless

@nia-e nia-e added the I-libs-nominated Nominated for discussion during a libs team meeting. label Nov 13, 2026
@maxdexh

maxdexh commented Nov 13, 2026 •

Copy link
Copy Markdown
Member Author

idk about fcp, but i think this should be backported to beta

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch 2 times, most recently from 21a7dfe to 993247b Compare November 13, 2026 15:07
@clarfonthey

Copy link
Copy Markdown
Contributor

Well, 1.100 is out now, and we're definitely not doing a point release for a doc backport.

@maxdexh

maxdexh commented Nov 13, 2026 •

Copy link
Copy Markdown
Member Author

Well, 1.100 is out now, and we're definitely not doing a point release for a doc backport.

bold of you to assume that the version won't need a point release. but anyway yeah ofc, hence why i only suggested beta ^^

If only we could make doc.rust-lang.org default to nightly 🙃

Comment thread library/core/src/alloc/mod.rs Resolved
Comment thread library/core/src/alloc/mod.rs Resolved
Comment thread library/core/src/alloc/mod.rs Resolved
@hanna-kruppe

Copy link
Copy Markdown
Contributor

I think this should definitely be FCP'd, it's making a material change to the contract of a now-stable trait. That's not to say it's likely to be a breaking change for implementers in practice, but it is changing a stable and very subtle contract, so more scrutiny is good. If this is obviously what everyone meant all along when stabilizing the trait, then it should be an easy FCP. But also, everything to do with allocator contract has been extremely subtle, so let's not be too hasty IMO.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from 993247b to eea9a39 Compare November 13, 2026 15:49
@N1ark

N1ark commented Nov 13, 2026

Copy link
Copy Markdown
Member

ty :D

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from eea9a39 to 8320498 Compare November 13, 2026 15:50
@theemathas theemathas added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Nov 13, 2026
@maxdexh

maxdexh commented Nov 13, 2026 •

Copy link
Copy Markdown
Member Author

I think this should definitely be FCP'd, it's making a material change to the contract of a now-stable trait. That's not to say it's likely to be a breaking change for implementers in practice, but it is changing a stable and very subtle contract, so more scrutiny is good. If this is obviously what everyone meant all along when stabilizing the trait, then it should be an easy FCP. But also, everything to do with allocator contract has been extremely subtle, so let's not be too hasty IMO.

We have another 4/3 of a month until the next release cycle. As long as this doesn't get stuck in proposed-fcp for 3 months despite no concerns (like one of my other PRs, which is still waiting), i don't see any downside to doing one here.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch 2 times, most recently from 342ced6 to 50562bb Compare November 13, 2026 16:04
@nia-e

nia-e commented Nov 13, 2026

Copy link
Copy Markdown
Member

@rfcbot fcp merge libs

@rust-rfcbot

rust-rfcbot commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

@nia-e has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Nov 13, 2026
@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from 50562bb to 61febb6 Compare November 13, 2026 16:28
@rust-log-analyzer

This comment has been minimized.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch 2 times, most recently from 249f34b to cbf59ab Compare November 17, 2026 09:10
Comment thread library/core/src/alloc/mod.rs Resolved
Comment thread library/core/src/alloc/mod.rs Resolved
theemathas

This comment was marked as resolved.

@rust-bors

This comment has been minimized.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from ac167ca to 80d9e3a Compare November 19, 2026 11:50
@rustbot

rustbot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from 80d9e3a to 2c4b6f2 Compare November 19, 2026 11:58
@rustbot rustbot added A-box Area: Our favorite opsem complication A-slice-vec Area: `[T]` and `Vec<T>` labels Nov 19, 2026
@maxdexh

maxdexh commented Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Alright, as per this discussion, I've removed the concepts of "currently allocated" and "invalidating" from the allocator contract and replaced them with talking about ownership directly, as they do not make sense in situations where ownership is passed through a cyclic chain of allocators (unless "memory block" != "block of memory").

The word "invalidate" is still used the way we usually use it: something becomes invalid at a language level.

cc @theemathas @RalfJung

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch 5 times, most recently from ee43e64 to b2724ee Compare November 19, 2026 12:41
theemathas

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch 6 times, most recently from 6887fdd to 6b947ae Compare November 19, 2026 13:36
theemathas

This comment was marked as resolved.

@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from 6b947ae to 734ef2e Compare November 19, 2026 17:30
@maxdexh
maxdexh force-pushed the this-mailbox-is-mine branch from 734ef2e to e6fadb3 Compare November 19, 2026 17:42
@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Nov 23, 2026

This branch has not been deployed

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

Labels

A-allocators Area: Custom and system allocators A-box Area: Our favorite opsem complication A-slice-vec Area: `[T]` and `Vec<T>` disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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.