Skip to content

Support field-wise CoerceShared reborrows#157101

Open
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:fix-reborrow-coerceshared-field-relations
Open

Support field-wise CoerceShared reborrows#157101
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:fix-reborrow-coerceshared-field-relations

Conversation

@P8L1
Copy link
Copy Markdown
Contributor

@P8L1 P8L1 commented May 29, 2026

View all comments

This PR extends generic shared reborrows so that CoerceShared validates and lowers source-to-target structs field by field, rather than relying on a same-layout, transmute-like copy.

The core change is a shared rustc_middle::ty::reborrow helper that computes the field correspondence used by CoerceShared validation, borrow checking, const-eval, and codegen.

The correspondence rules are:

  • Named structs match non-PhantomData data fields by name.
  • Tuple structs match non-PhantomData data fields by position.
  • PhantomData fields are ignored.
  • Every target data field must correspond to a source data field.

CoerceShared impl validation now checks each corresponding field relation. A field relation is accepted when the field is:

  • Copy-compatible,
  • recursively CoerceShared-compatible, or
  • a mutable-reference-to-shared-reference reborrow.

The validation also rejects mismatched field styles, missing source fields, mismatched reborrow lifetimes, and inaccessible fields with targeted diagnostics.

Borrowck now adds shared generic reborrow constraints recursively through the validated field relations, while still issuing the loan for the original source place as a whole. This ensures source-only fields remain protected for the inferred target lifetime when the target omits fields from the source.

Const-eval and codegen now lower shared generic reborrows recursively into the target fields instead of treating the operation as a transmute-like copy. This covers nested CoerceShared fields and layout-changing source/target pairs.

Fixes

Fixes #156566.
Fixes #156309.
Fixes #156315.

PR scope

I did not split these into smaller PRs because the issues mostly share the same root cause: CoerceShared needed a field-by-field validation and lowering model. So when making this, I just decided to write it in a way that will fix the issues anyway.

Tracking

#145612
https://rust-lang.github.io/rust-project-goals/2025h2/autoreborrow-traits.html

CC. @aapoalas, @dingxiangfei2009
r? @RalfJung

@rustbot rustbot added 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. labels May 29, 2026
@P8L1

This comment was marked as outdated.

@rustbot rustbot added the F-reborrow `#![feature(reborrow)]`; see #145612 label May 29, 2026
@P8L1

This comment was marked as outdated.

@P8L1

This comment was marked as outdated.

@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 29, 2026

🔨 5 commits were squashed into b8e47e8.

@rust-bors rust-bors Bot force-pushed the fix-reborrow-coerceshared-field-relations branch from 9713677 to b8e47e8 Compare May 29, 2026 09:36
@rust-log-analyzer

This comment has been minimized.

@P8L1 P8L1 force-pushed the fix-reborrow-coerceshared-field-relations branch from b8e47e8 to 83d9f22 Compare May 31, 2026 17:45
@rust-log-analyzer

This comment has been minimized.

@P8L1

This comment was marked as outdated.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 1, 2026

Unclosed quote in argument. Run @bors help to see available commands.

@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented Jun 1, 2026

@bors squash msg="Support field-wise CoerceShared reborrows"

@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 1, 2026

🔨 7 commits were squashed into 8987e4a.

@rust-bors rust-bors Bot force-pushed the fix-reborrow-coerceshared-field-relations branch from 62ec49c to 8987e4a Compare June 1, 2026 06:26
@P8L1 P8L1 marked this pull request as ready for review June 1, 2026 06:28
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 1, 2026

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

The Cranelift subtree was changed

cc @bjorn3

@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 Jun 1, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 1, 2026

r? @folkertdev

rustbot has assigned @folkertdev.
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 17 candidates

@rustbot rustbot assigned RalfJung and unassigned folkertdev Jun 1, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 1, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Jun 1, 2026

Why are you asking for my review here? I have nothing to do with this feature and don't have capacity to take on a new feature, especially not a massive PR like this.

@rustbot reroll

@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented Jun 1, 2026

Why are you asking for my review here? I have nothing to do with this feature and don't have capacity to take on a new feature, especially not a massive PR like this.

@rustbot reroll

Oh I'm Sorry I did not think much.

I inferred from #156955 that you might be the right person to look specifically at the CTFE/Miri part, since that PR also touched reborrow const-eval/interpreter code and you reviewed it. But I understand now that this might be a bit to much to review on top of the work you already have..

Once again sorry, have a nice day

Comment thread compiler/rustc_const_eval/src/interpret/step.rs
@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Jun 1, 2026

Looks like this is associated with https://rust-lang.github.io/rust-project-goals/2026/reborrow-traits.html ?
Cc @aapoalas -- is there a long-term plan for this proposal? If such non-trivial MIR semantics are needed, a "small" compiler team ask may not quite reflect the amount of work this may take. Also see this thread.

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Jun 1, 2026

r? @oli-obk
who is the compiler liaison for that goal.

Also @P8L1 when you do work for a project goal, that's a good thing to mention in the PR description. If you're not aware of the project goal here, please coordinate with @aapoalas and @oli-obk.

@rustbot rustbot assigned oli-obk and unassigned nnethercote Jun 1, 2026
@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented Jun 1, 2026

r? @oli-obk who is the compiler liaison for that goal.

Also @P8L1 when you do work for a project goal, that's a good thing to mention in the PR description. If you're not aware of the project goal here, please coordinate with @aapoalas and @oli-obk.

Hi Ralf, sorry, I always thought the tracking issue was enough. Thanks for the correction, I won't make the mistake again!

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Jun 1, 2026

Fair, the tracking issue should in principle suffice. But not everyone always clicks through all the links. ;)

@oli-obk oli-obk assigned aapoalas and unassigned oli-obk Jun 1, 2026
@dingxiangfei2009
Copy link
Copy Markdown
Contributor

Hello @P8L1 and thank you for taking interest in the reborrow work.

With per-field reborrowing the proposed design will break away from a few fundamental but important assumptions and intended use cases of this language feature. A design document would certainly bring @aapoalas and me onto the same page and help us understand why the extension would be desirable. Let us chat on Zulip as well if you need any help.

@P8L1
Copy link
Copy Markdown
Contributor Author

P8L1 commented Jun 1, 2026

Hello @P8L1 and thank you for taking interest in the reborrow work.

With per-field reborrowing the proposed design will break away from a few fundamental but important assumptions and intended use cases of this language feature. A design document would certainly bring @aapoalas and me onto the same page and help us understand why the extension would be desirable. Let us chat on Zulip as well if you need any help.

Thanks, that makes sense.

I wrote up a PR-specific design note here please review it and share your thoughts so this PR can proceed!

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.

Projects

None yet

9 participants