Skip to content

Handle generic reborrow in expression-use adjustment walking#156795

Open
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:fix-generic-reborrow-expr-use-visitor
Open

Handle generic reborrow in expression-use adjustment walking#156795
P8L1 wants to merge 1 commit into
rust-lang:mainfrom
P8L1:fix-generic-reborrow-expr-use-visitor

Conversation

@P8L1
Copy link
Copy Markdown
Contributor

@P8L1 P8L1 commented May 21, 2026

Fixes an ICE in expression-use adjustment walking where Adjust::GenericReborrow could reach a match arm that assumed generic reborrow was unreachable.

GenericReborrow is already emitted by typeck and classified as rvalue-producing elsewhere in expr_use_visitor.rs, so the adjustment walker must handle it explicitly instead of panicking.

This PR models GenericReborrow as a borrow-like use of the source expression:

  • Mutability::Mut is treated like an exclusive/mutable reborrow use.
  • Mutability::Not is treated like a shared/coerce-shared borrow-like use.
  • The source is not moved or treated as a mere copy.

cc @aapoalas

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

rustbot commented May 21, 2026

r? @nikomatsakis

rustbot has assigned @nikomatsakis.
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 19 candidates

@P8L1 P8L1 force-pushed the fix-generic-reborrow-expr-use-visitor branch from 53084cc to 90709a5 Compare May 21, 2026 12:01
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.

// assignment. This is a contradiction.
unreachable!("Reborrow trait usage during adjustment walk");
adjustment::Adjust::GenericReborrow(mutability) => {
let bk = ty::BorrowKind::from_mutbl(mutability);
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.

thought: I don't have in-depth understanding about what this exactly doing and if that matches my understanding but... probably it does :) Definitely it's better than an ICE.

The interesting corner-case would probably be something like this:

fn id_with_closure<'a>(x: CustomMut<'a>) -> CustomMut<'a> {
  let cb = || x;
  cb()
}

Right now due to a mistake/complication in the implementation this wouldn't even work with just returning x, but once that's fixed then the closure version should also work.

@theemathas
Copy link
Copy Markdown
Contributor

Does this fix #156339 ?

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

Labels

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

Development

Successfully merging this pull request may close these issues.

5 participants