Skip to content

alloc: stabilise Allocator - #156882

Open
nia-e wants to merge 1 commit into
rust-lang:mainfrom
nia-e:stable-allocator
Open

alloc: stabilise Allocator#156882
nia-e wants to merge 1 commit into
rust-lang:mainfrom
nia-e:stable-allocator

Conversation

@nia-e

@nia-e nia-e commented May 24, 2026

Copy link
Copy Markdown
Member

View all comments

See the current proposed stabilisation report for up-to-date information on the proposed stable API.

outdated former status

Stabilise a bare-minimum (dyn-incompatible, but could be in the future) Allocator trait, alongside Box::new_in(), Vec::new_in(), the System & Global Allocators, and a blanket impl of Allocator for T: GlobalAlloc. For now, we should take care not to make it possible to instantiate anything other than a Vec or Box with custom allocators; it's Probably Fine, but worth a proper look before we rush in.

The soundness requirements for implementors were tightened to the most restrictive ones we could reasonably want per a conversation with @RalfJung.

This was discussed extensively at the all-hands with an apparent tentative consensus from libs and participating ecosystem stakeholders that the current design can be extended backwards-compatibly to address almost all usecases.

cc @rust-lang/libs @rust-lang/libs-api @rust-lang/opsem

r? libs

@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 May 24, 2026
@nia-e nia-e added A-allocators Area: Custom and system allocators relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels May 24, 2026
@nia-e nia-e added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. and removed S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels May 24, 2026
@nia-e

nia-e commented May 24, 2026

Copy link
Copy Markdown
Member Author

r? @Amanieu

@rustbot rustbot assigned Amanieu and unassigned Mark-Simulacrum May 24, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/alloc/mod.rs Outdated
@nia-e
nia-e force-pushed the stable-allocator branch from ecf76bf to ed24b36 Compare May 24, 2026 17:42
Comment thread library/alloc/src/collections/binary_heap/mod.rs Outdated
Comment thread library/core/src/alloc/mod.rs Outdated
Comment thread library/core/src/alloc/global.rs Outdated
Comment thread tests/ui/allocator/not-an-allocator.u.stderr Outdated
Comment thread library/core/src/alloc/global.rs Outdated
Comment thread library/core/src/alloc/mod.rs Outdated
@theemathas

Copy link
Copy Markdown
Contributor

I believe the safety requirements are not yet correct. See #156544

@jmillikin

This comment has been minimized.

@bushrat011899

This comment has been minimized.

@jmillikin

This comment has been minimized.

@bushrat011899

This comment has been minimized.

@jmillikin

This comment has been minimized.

@bushrat011899

This comment has been minimized.

@joshtriplett

This comment was marked as outdated.

@rust-rfcbot

This comment was marked as outdated.

@nia-e nia-e added S-waiting-on-t-libs Status: Awaiting decision from T-libs S-waiting-on-t-lang Status: Awaiting decision from T-lang I-lang-nominated Nominated for discussion during a lang team meeting. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 19, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread library/alloc/src/vec/mod.rs Outdated
@Amanieu

Amanieu commented Aug 20, 2026

Copy link
Copy Markdown
Member

I believe FCP is ready to be opened; the proposed-for-stabilisation bits are listed in the report. In brief:

  • Allocator trait;

  • Box<T, A> & Vec<T, A> + a couple methods on these;

  • System & Global allocators.

The libs+libs-api ask is "are these indeed the APIs we want?" and the vibe seems to be positive. The lang ask is "can we commit to Box never being fundamental in A, given that changing this would be breaking later?" (context, context) and again the vibe appeared positive last time this was brought up ^^

Thanks @nia-e for pushing this through!

@rfcbot merge lang,libs-api

@rust-rfcbot

rust-rfcbot commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

@Amanieu 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/lang-advisors: FCP proposed for lang, 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. labels Aug 20, 2026
@nia-e

nia-e commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

cc also @rust-lang/libs-api + @rust-lang/lang

@traviscross

Copy link
Copy Markdown
Contributor

Do we intend (now or later) to make any ABI guarantees about Box<T, A> where A ≠ Global?

@RalfJung

RalfJung commented Aug 20, 2026

Copy link
Copy Markdown
Member

I'd be fine with making guarantees for the case where A has trivial ABI. Beyond that -- probably not.

@traviscross

Copy link
Copy Markdown
Contributor

Should we be firing improper_ctypes_definitions?

@rust-bors

This comment has been minimized.

@nia-e

nia-e commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

I'm happy having them under that lint

@rustbot

rustbot commented Aug 20, 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.

Comment thread library/alloc/src/sync.rs
@rust-bors

rust-bors Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@traviscross

Copy link
Copy Markdown
Contributor

Thanks @nia-e.

As I understand it, on lang, we're signing off on two things:

One, we're agreeing that Box<T, A> will not be fundamental in A. Why does this make sense? We can ask, why is it fundamental in T? The answer is that we think of Box<…> as an ownership mode over a value, and the trait behavior of that value is (fundamentally) determined by T. This doesn't apply to A. Further, we want Box<…> to cover A so that authors can write certain impls that should be universally quantified over A, such as:

impl<A: Allocator> PartialEq<LocalTy> for Box<u8, A> {
  fn eq(&self, _: &LocalTy) -> bool { true }
}

Two, we're agreeing that Box<T, A> will not be noalias and will not carry the corresponding language-level uniqueness requirements when A ≠ Global (except, possibly, when A is some NativeAllocator<…> wrapper we might add in the future). This makes sense because, unlike the global allocator, which is magic, custom allocators are normal Rust code, and certain allocators want to offset from the data pointer to the allocator state during deallocation.

See:

@rfcbot reviewed

@theemathas

Copy link
Copy Markdown
Contributor

Based on conversations elsewhere, I don't think this will be an issue, I don't think this will be a problem. But it's worth mentioning for awareness.

Based on the documentation of Box::into_raw_with_allocator, we are committing to a guarantee that the raw pointer from a Box can be deallocated with the layout of T. This implies that the allocator is stored in the Box struct, not in the heap allocation.

This in turns means that if we ever wanted to implement DispatchFromDyn on boxes with custom allocators, we'd need to modify the compiler so that dynamic dispatch works on receivers whose ABI aren't pointer-like. My understanding is that this is a matter of implementation work, and can be done.

@theemathas

theemathas commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

And now for a concern that I think is actually blocking: Some traits in std are currently implemented on Box<T>, and not a generic Box<T, A>. I think that changing this later is a breaking change, so we'd need to generalize such impls on stable traits now.

(This is also a semver hazard for third-party crates that have blanket impls on Boxes. Generalizing such impls to include boxes with custom allocators is a breaking change.)

@bushrat011899

Copy link
Copy Markdown
Contributor

And now for a concern that I think is actually blocking: Some traits in std are currently implemented on Box<T>, and not a generic Box<T, A>. I think that changing this later is a breaking change, so we'd need to generalize such impls on stable traits now.

Is it a breaking change? Such implementations are effectively Box<T, Global>, and only the T parameter is fundamental. Or is this in reference to type inference (which is why Default is explicitly only implemented for Global)

@theemathas

Copy link
Copy Markdown
Contributor

@bushrat011899 The following code currently compiles on nightly, and would break if we were to change the Read for Box impl to work with custom allocators.

#![feature(allocator_api)]

use std::alloc::Global;
use std::io::{self, Read};

struct Thing;
impl Read for Thing {
    fn read(&mut self, _: &mut [u8]) -> Result<usize, io::Error> {
        Ok(0)
    }
}

impl Read for Box<Thing, &'static Global> {
    fn read(&mut self, _: &mut [u8]) -> Result<usize, io::Error> {
        Ok(0)
    }
}

@bushrat011899

Copy link
Copy Markdown
Contributor

Interesting, that's only an issue because the first type parameter is fundamental? For example, an implementation of Box<Foo, Global> (for a particular concrete Foo) could be expanded to all allocators without being a breaking change if I'm understanding the issue correctly?

@theemathas

Copy link
Copy Markdown
Contributor

Interesting, that's only an issue because the first type parameter is fundamental?

Yes.

For example, an implementation of Box<Foo, Global> (for a particular concrete Foo) could be expanded to all allocators without being a breaking change if I'm understanding the issue correctly?

I think so, yes.

@nia-e

nia-e commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

I'm happy to expand all appropriate trait impls before landing this. I believe some of them can't † be expanded directly (e.g. Default) since they'd be breaking on stable today, but I'll do a pass over all other downstream-implementable traits and shoot a PR generalising them if possible.

† Doing so would require effectively splitting Box into two types, one with 2 generic params (pointee + allocator), and a type alias to the former with A = Global. This would mean changes to the prelude as well and this was discussed & I will mention it for completeness but I personally wouldn't want to go that direction...

@maxdexh

maxdexh commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

† Doing so would require effectively splitting Box into two types, one with 2 generic params (pointee + allocator), and a type alias to the former with A = Global. This would mean changes to the prelude as well and this was discussed & I will mention it for completeness but I personally wouldn't want to go that direction...

Here is some more discussion about that splitting. I think we should do this ^^

@jkarneges

Copy link
Copy Markdown
Contributor

Not sure if discussed, but moving the allocator into the heap allocation can make sense for Rc/Arc and if that were to happen it might be strange if Box were fat.

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-run-make Area: port run-make Makefiles to rmake.rs disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-reference-pr This language change needs an approved Reference PR to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-t-lang Status: Awaiting decision from T-lang S-waiting-on-t-libs Status: Awaiting decision from T-libs T-clippy Relevant to the Clippy team. T-lang Relevant to the language team 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.