Skip to content

fix(reborrow): recursive implementation - #159103

Open
aapoalas wants to merge 11 commits into
rust-lang:mainfrom
aapoalas:reborrow/fix-borrowed-place
Open

fix(reborrow): recursive implementation#159103
aapoalas wants to merge 11 commits into
rust-lang:mainfrom
aapoalas:reborrow/fix-borrowed-place

Conversation

@aapoalas

@aapoalas aapoalas commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

View all comments

If Reborrow finds '&'a mut T' fields where 'a is the Reborrowed type's first lifetime parameter (currently the only lifetime that is allowed to reborrow) then it inserts a Deref and borrow of the T, and likewise if it finds a 'T: Reborrow' field then the field type is recursed into.

This makes Reborrow always produce the correct borrow checking logic at the cost of most probably being inconsiderately expensive. The thinking is that performance will be a followup consideration.

r? @oli-obk

@rustbot

rustbot commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

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

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to constck

cc @fee1-dead

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

@rustbot rustbot added 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. labels Jul 10, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

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

cc @bjorn3

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

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Jul 10, 2026
@rust-log-analyzer

This comment has been minimized.

@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch from c74824e to 4ac1ece Compare July 19, 2026 14:32
@rustbot

This comment has been minimized.

@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch 3 times, most recently from aebdf5d to b845ed2 Compare July 20, 2026 09:37
@rust-bors

This comment has been minimized.

@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch from b845ed2 to 549d21b Compare July 23, 2026 11:59
@rustbot

This comment has been minimized.

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

Started a review a few days ago, but other things came in between, so nothing architectural yet, but docs are always useful

View changes since this review

Comment thread compiler/rustc_middle/src/mir/syntax.rs
Comment thread tests/ui/reborrow/custom_marker_mut_field_borrow.rs
Comment thread tests/ui/reborrow/custom_marker_place_conflict.rs Outdated
Comment thread tests/ui/reborrow/custom_marker_place_conflict_deref.rs Outdated
@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 Jul 23, 2026
@rustbot

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

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

@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch from 44ad71e to 2c3a3fa Compare July 27, 2026 18:10
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch from 2c3a3fa to d815250 Compare August 7, 2026 16:45
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

If Reborrow finds '&'a mut T' fields then it inserts
a Deref and borrow of the T, and likewise if it finds a
'T: Reborrow' field then the field type is recursed into.

This makes Reborrow always produce the correct borrow checking logic at
the cost of most probably being inconsiderately expensive. The thinking
is that performance will be a followup consideration.
@aapoalas
aapoalas force-pushed the reborrow/fix-borrowed-place branch from d815250 to c289b88 Compare August 13, 2026 16:00
@rustbot

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

@oli-obk oli-obk 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 14, 2026

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

I don't fully grok all this yet, but I have enough of an understanding that I'm confident with landing this. It does seem like a coherent thing to me and it will fully "click" in my mind at some point

View changes since this review

/// producing a borrow on an indirect place, producing a value that can be returned from the
/// function since it does not capture any local place. If no such field exists, then
/// reborrowing the type must dereference the type itself to find an indirect place, but
/// generally such types will not implements `Deref`. Therefore, in borrow checking we instead

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
/// generally such types will not implements `Deref`. Therefore, in borrow checking we instead
/// generally such types will not implement `Deref`. Therefore, in borrow checking we instead

@@ -0,0 +1,17 @@
//@ check-fail

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.

this is the default setting for ui tests and redundant (also more of those below)

}
}
if !did_reborrow {
// If source contained no reference, perform a phantom dereference.

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.

Ah this is the key point here: we need the rest of borrowck to act as if there is a deref here 🤔

Comment on lines +2543 to +2547
// The reason why we do this obligation here (again: it's already done in THIR) is
// because we'd want to catch impls like `CoerceShared<Target<'static>> for Source<'a>`
// and, in those cases, correctly generate a `'a: 'static` bound.
//
// I'm not sure what would be the right way to resolve this conundrum.

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.

this is exactly what you need to do in borrowck's "typeck". You redo the ops, this time with lifetimes, and ICE on any type conflicts.

I think what you should be doing (instead of manually running an ocx), is to use the usual fully_perform actions, but otherwise this is exactly The Right Thing ™️

@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 18, 2026
@oli-obk

oli-obk commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@bors delegate+

r=me with remaining things resolved

@rust-bors

rust-bors Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

✌️ @aapoalas, you can now approve this pull request!

If @oli-obk told you to "r=me" after making some further change, then please make that change and post @bors r=oli-obk.

View changes since this delegation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants