Skip to content

Implement derives for Reborrow and CoerceShared - #156338

Open
P8L1 wants to merge 2 commits into
rust-lang:mainfrom
P8L1:derive-reborrow-coerce-shared
Open

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

Conversation

@P8L1

@P8L1 P8L1 commented May 8, 2026

Copy link
Copy Markdown
Contributor

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

rustbot commented May 8, 2026

Copy link
Copy Markdown
Collaborator

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

rustbot commented May 8, 2026

Copy link
Copy Markdown
Collaborator

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

@ChayimFriedman2 ChayimFriedman2 left a comment

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.

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

rustbot commented May 9, 2026

Copy link
Copy Markdown
Collaborator

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

P8L1 commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

@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)]

@aapoalas aapoalas May 10, 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.

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

View changes since the review

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.

those are hard to support in the current derive infra.

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.

Should def be an open question on the tracking issue, as #[coerce_shared(Thing<'a, T>)] looks a bit unclear to me, maybe #[coerce_shared_target = Thing<'a, T>] if our attribute syntax permits that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I checked the available attribute forms. The current grammar does not accept '#[coerce_shared_target = Thing<'a, T>], since the right-hand side of an = attribute is parsed as an expression and stored as AttrArgs::Eq { expr: Box<Expr>, .. }, while Thing<'a, T> is type syntax and fails expression parsing.

Also, since the current derive pipeline diagnoses arguments on derive paths and resolves only the trait path, we currently reject derive(CoerceShared(Target))

A structured helper such as #[coerce_shared(target = Thing<'a, T>)] is technically feasible because delimited attributes retain their token stream, allowing the builtin derive to parse target = followed by parse_ty().

@aapoalas aapoalas left a comment

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.

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

tiif commented May 11, 2026

Copy link
Copy Markdown
Member

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

P8L1 commented May 14, 2026

Copy link
Copy Markdown
Contributor Author

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

P8L1 commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

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
@P8L1

P8L1 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

@dingxiangfei2009 It's been a while, just a reminder to review. No pressure thought!

@oli-obk oli-obk left a comment

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.

Good thing I didn't unsubscribe, even if it was reassigned from me. I was a bit busy catching up in May as I was not doing much Rust stuff before it.

Generally lgtm, slightly worried about spans without expansion info causing funny diagnostics, but we'll add that when necessary (or testable)

View changes since this review

}

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

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.

those are hard to support in the current derive infra.

}

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

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.

Should def be an open question on the tracking issue, as #[coerce_shared(Thing<'a, T>)] looks a bit unclear to me, maybe #[coerce_shared_target = Thing<'a, T>] if our attribute syntax permits that?

Comment thread compiler/rustc_builtin_macros/src/deriving/reborrow.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 6, 2026
@oli-obk oli-obk assigned oli-obk and unassigned dingxiangfei2009 Aug 6, 2026
@rustbot

This comment has been minimized.

@P8L1
P8L1 force-pushed the derive-reborrow-coerce-shared branch from c6af245 to 8d9db78 Compare August 6, 2026 23:31
@rustbot

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

@P8L1

P8L1 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@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 Aug 6, 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