Skip to content

Add anti-fundamental attribute - #160391

Draft
ShaddyDC wants to merge 3 commits into
rust-lang:mainfrom
ShaddyDC:anti-fundamental
Draft

Add anti-fundamental attribute#160391
ShaddyDC wants to merge 3 commits into
rust-lang:mainfrom
ShaddyDC:anti-fundamental

Conversation

@ShaddyDC

@ShaddyDC ShaddyDC commented Aug 2, 2026

Copy link
Copy Markdown

This PR introduces the internal #[rustc_anti_fundamental] trait attribute to prevent downstream crates from implementing traits on non-local #[fundamental] types (such as Pin and Box).

We extend the orphan rule to reject implementations for anti-fundamental traits if the head of its Self type is a non-local fundamental type.
We mark Deref, DerefMut, Receiver, CoerceUnsized, and DispatchFromDyn with #[rustc_anti_fundamental], which cleanly resolves orphan rule conflicts and removes the internal PinDerefMutHelper workaround trait in core::pin.

See also #85099
Fixes #148727, #147998 and #148899
Removes hack needed by #145608

AI disclosure

This PR was created with AI assistance. I don't think I have permission to add the llm-assisted label.
It touches soundness, but I created it before the new LLM usage policy came into effect, and I only noticed once I fixed some issues I came across and rebased and wanted to update the PR.
Xiang has agreed to review this PR.

We sketched out the implementation logic by hand together and I then gave instructions to the LLM to implement it. I then iterated it and expanded testing through discussing edge cases and AI review until we were fairly confident in the current implementation.

r? @dingxiangfei2009

@rustbot

rustbot commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @dingxiangfei2009 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Aug 2, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

This adds a new compiler attribute that prevents non-local fundamental
types from receiving implementations of marked traits.
This allows addressing soundness problems with traits like DerefMut on
Pin and similar wrapper types.
The attribute is checked during the orphan check in coherence.
Mark Deref, DerefMut, DispatchFromDyn, CoerceUnsized, and Receiver
with #[rustc_anti_fundamental] to prevent downstream crates from
implementing these traits on #[fundamental] types like Box and Pin.
@dingxiangfei2009

Copy link
Copy Markdown
Contributor

@rustbot label +llm-assisted

@rustbot rustbot added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Sep 2, 2026
@dingxiangfei2009

Copy link
Copy Markdown
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 2, 2026
@rust-bors

rust-bors Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 14450d2 (14450d27a2d0d3b3cb66037c8111ee3da14ed3c2)
Base parent: c25253f (c25253fac451fff60fb5b6c0b2c64d60f628c7cd)

Comment thread library/core/src/pin.rs Outdated
Comment on lines 1887 to 1892

@Darksonn Darksonn Sep 2, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd update the wording here to talk about anti-fundamental instead of saying "special hack".

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Adjusted the wording and changed the link to point to this PR instead

@dingxiangfei2009

Copy link
Copy Markdown
Contributor

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-160391 created and queued.
🤖 Automatically detected try build 14450d2
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 2, 2026
We marked DerefMut anti-fundamental, so the PinHelper indirection is no
longer needed to prevent downstream users from implementing DerefMut on
Pin.
/// it wraps a local type as in (2)).
/// - e.g., `Box<LocalType>` or `&Pin<LocalType>` is rejected if the trait
/// is `#[rustc_anti_fundamental]`.
/// - This lets the standard library reserve control over traits like `Deref`

@dingxiangfei2009 dingxiangfei2009 Sep 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that we don't need bullet points to structure the motivation, it would be more readable if this is just a prose.

View changes since the review

loop {
ty = infcx.shallow_resolve(ty);
let norm_ty = match lazily_normalize_ty(ty)? {
norm if norm.is_ty_var() => ty,

@dingxiangfei2009 dingxiangfei2009 Sep 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, if norm is a type var, ty can't be normalized to the slightest bit I suppose. So it reads a bit off to me if we call it norm_ty... Wdyt?

View changes since the review

/// Checks the head of the Self type for a non-local fundamental type.
///
/// If the head is a reference (`&` / `&mut`), we unwrap it and inspect the pointee type.
/// This ensures that wrapping a fundamental type in a reference (such as `&Pin<LocalType>`)

@dingxiangfei2009 dingxiangfei2009 Sep 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// This ensures that wrapping a fundamental type in a reference (such as `&Pin<LocalType>`)
/// This ensures that wrapping a fundamental type in a reference, say a `&Pin<LocalType>`,

View changes since the review

@theemathas

Copy link
Copy Markdown
Contributor

@craterbot cancel

See #162233

@craterbot

Copy link
Copy Markdown
Collaborator

🗑️ Experiment pr-160391 deleted!

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsoundness and ICE due to DispatchFromDyn allowing bogus impls on references.

7 participants