close
Skip to content

Implement derives for Reborrow and CoerceShared#156338

Open
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:derive-reborrow-coerce-shared
Open

Implement derives for Reborrow and CoerceShared#156338
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:derive-reborrow-coerce-shared

Conversation

@P8L1
Copy link
Copy Markdown
Contributor

@P8L1 P8L1 commented May 8, 2026

This adds built-in derive support for the experimental Reborrow and CoerceShared marker traits behind #![feature(reborrow)].

The derives generate the same marker impls users would write manually, while preserving the existing coherence and structural validation path for the experimental reborrow traits.

Please note this impl:

  • Preserves generics, lifetimes, const params, and where clauses in generated impls.
  • Preserves multiple lifetime parameters in generated impls instead of duplicating the current experimental one-lifetime limitation in the derive layer.
  • Requires an explicit CoerceShared target type through the derive helper attribute.
  • Adds UI coverage for valid derives, feature gating, malformed helper attributes, unsupported item kinds, validation failures, and multiple-lifetime derive behavior.
  • Makes the derive layer future-proof for multiple lifetimes: it preserves all lifetime parameters and generates the same impl shape as a manual impl.

@rustbot label F-reborrow
Tracking: #145612

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 8, 2026

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 8, 2026
@rustbot rustbot added 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. labels May 8, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 8, 2026

r? @tiif

rustbot has assigned @tiif.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@P8L1 P8L1 force-pushed the derive-reborrow-coerce-shared branch 2 times, most recently from c515771 to 1a83d37 Compare May 8, 2026 19:01
@rust-log-analyzer

This comment has been minimized.

@P8L1 P8L1 force-pushed the derive-reborrow-coerce-shared branch from 1a83d37 to 8e2ead2 Compare May 8, 2026 20:06
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Please separate the rust-analyzer changes to a new PR in the rust-analyzer repo, there is no reason for them to be here.

View changes since this review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 9, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 9, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 9, 2026
@P8L1 P8L1 force-pushed the derive-reborrow-coerce-shared branch from 8e2ead2 to 0d49f18 Compare May 9, 2026 20:18
@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented May 9, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 9, 2026
}

#[derive(Diagnostic)]
#[diag("`derive(CoerceShared)` requires exactly one `#[coerce_shared(Target)]` attribute", code = E0802)]
Copy link
Copy Markdown
Contributor

@aapoalas aapoalas May 10, 2026

Choose a reason for hiding this comment

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

suggestion: I would've gone with derive(CoerceShared(Target)).

View changes since the review

Copy link
Copy Markdown
Contributor

@aapoalas aapoalas left a comment

Choose a reason for hiding this comment

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

Thank you so much for this work. I'm really thankful for the help; I expect this would've taken me quite a bit of time to figure out! <3

View changes since this review

@tiif
Copy link
Copy Markdown
Member

tiif commented May 11, 2026

I don't have enough context to review this, so I will leave this to the compiler champion.

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned tiif May 11, 2026
@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented May 14, 2026

Hi @oli-obk, no rush, but I wanted to check that this PR is still on your radar.

I know about the usual two-week review window; I’m just making sure it did not get missed after the latest updates. Thanks!

@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented May 22, 2026

Oli seems to be very busy ATM so thus:

r? @dingxiangfei2009

Cc. @aapoalas

@rustbot rustbot assigned dingxiangfei2009 and unassigned oli-obk May 22, 2026
@rustbot rustbot added the F-reborrow `#![feature(reborrow)]`; see #145612 label May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-reborrow `#![feature(reborrow)]`; see #145612 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants