Skip to content

borrowck: Fix Reborrow ICE on types with several lifetimes - #166171

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Dnreikronos:reborrow/multi_lifetime_no_reborrowed_field
Nov 26, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Dnreikronos:reborrow/multi_lifetime_no_reborrowed_field

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no &mut and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with &'a &'b str from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the &'static mut field ICE (#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.

@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 Nov 19, 2026
@Dnreikronos
Dnreikronos marked this pull request as ready for review November 19, 2026 12:37
@rustbot

rustbot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

@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 Nov 19, 2026
@rustbot

rustbot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

r? @mejrs

rustbot has assigned @mejrs.
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 79 candidates
  • Random selection from 19 candidates

@Dnreikronos
Dnreikronos force-pushed the reborrow/multi_lifetime_no_reborrowed_field branch from cd2ccaa to a40d99f Compare November 19, 2026 14:05
@Dnreikronos Dnreikronos changed the title reborrow: Reject multi-lifetime Reborrow impls with no reborrowed field borrowck: Fix Reborrow ICE on types with several lifetimes Nov 19, 2026
@mejrs

mejrs commented Nov 19, 2026

Copy link
Copy Markdown
Member

r? aapoalas

@rustbot rustbot assigned aapoalas and unassigned mejrs Nov 19, 2026
@rustbot

rustbot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

aapoalas is currently at their maximum review capacity.
They may take a while to respond.

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

LGTM - yeah, when there is no field that would explain what to reborrow using a deeper Reborrow then this is definitely the right choice: we shouldn't start looking into a PhantomData's generic because that way lays misery and pain (at least for now).

One thing you could do is remove the bug! from a zero-lifetime case: from the borrowck point of view that doesn't really matter, and in the future we might even explicitly allow that for Copy types. Having no lifetimes just makes a Reborrow equal Copy after all.

View changes since this review

@rust-lang rust-lang deleted a comment from rust-bors Bot Nov 24, 2026
@aapoalas

Copy link
Copy Markdown
Contributor

@bors delegate+

Please squash, consider removing the bug!, and merge.

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

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

View changes since this delegation.

When no field of a `Reborrow` type is reborrowed, borrowck borrows the
whole source value instead. That fallback assumed the target type has
exactly one lifetime and hit a `bug!` otherwise, even though `Reborrow`
impls on types with several lifetimes are accepted.

Borrow the source under each of the target's lifetimes instead. The one
lifetime case is unchanged, and the source stays borrowed for as long as
any of the target's lifetimes is live.
@Dnreikronos
Dnreikronos force-pushed the reborrow/multi_lifetime_no_reborrowed_field branch from a40d99f to 9f1b6ca Compare November 25, 2026 22:33
@Dnreikronos

Copy link
Copy Markdown
Contributor Author

@bors r+

@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 9f1b6ca has been tentatively approved by Dnreikronos

It will be put into the queue for this repository once PR CI succeeds.

⚠️ The following reviewer(s) could not be found: Dnreikronos

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 25, 2026
@Dnreikronos

Dnreikronos commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

LGTM - yeah, when there is no field that would explain what to reborrow using a deeper Reborrow then this is definitely the right choice: we shouldn't start looking into a PhantomData's generic because that way lays misery and pain (at least for now).

One thing you could do is remove the bug! from a zero-lifetime case: from the borrowck point of view that doesn't really matter, and in the future we might even explicitly allow that for Copy types. Having no lifetimes just makes a Reborrow equal Copy after all.

View changes since this review

hey o/
I squashed the commits and kept the bug! for now. I tried a CoerceShared coercion to a Copy type with no lifetime args and hit that guard. MIR type checking still expects a first lifetime arg, so dropping the guard would just move the ICE there.

I think the zero-lifetime case deserves its own fix. That keeps this PR focused on the crash with multiple lifetimes.

clarfonthey added a commit to clarfonthey/rust that referenced this pull request Nov 26, 2026
…e_no_reborrowed_field, r=Dnreikronos

borrowck: Fix Reborrow ICE on types with several lifetimes

Fixes rust-lang#165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no `&mut` and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with `&'a &'b str` from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the `&'static mut` field ICE (rust-lang#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.
clarfonthey added a commit to clarfonthey/rust that referenced this pull request Nov 26, 2026
…e_no_reborrowed_field, r=Dnreikronos

borrowck: Fix Reborrow ICE on types with several lifetimes

Fixes rust-lang#165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no `&mut` and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with `&'a &'b str` from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the `&'static mut` field ICE (rust-lang#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Rollup of 4 pull requests

Successful merges:

 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…owed_field, r=Dnreikronos

borrowck: Fix Reborrow ICE on types with several lifetimes

Fixes #165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no `&mut` and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with `&'a &'b str` from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the `&'static mut` field ICE (#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job dist-riscv64-linux-musl failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] rustc_incremental test:false 10.809
   Compiling ar_archive_writer v0.5.5
##[error]The runner has received a shutdown signal. This can happen when the runner service is stopped, or a manually started runner is canceled.

Session terminated, killing shell...::group::Clock drift check
  local time: Thu Nov 26 05:03:47 UTC 2026
  network time: Thu, 26 Nov 2026 05:03:47 GMT
##[endgroup]
[RUSTC-TIMING] ar_archive_writer test:false 3.155
   Compiling tracing-serde v0.2.0

@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 26, 2026
@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 52d604d failed: CI. Failed job:

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors retry

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
…e_no_reborrowed_field, r=Dnreikronos

borrowck: Fix Reborrow ICE on types with several lifetimes

Fixes rust-lang#165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no `&mut` and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with `&'a &'b str` from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the `&'static mut` field ICE (rust-lang#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
@rust-bors
rust-bors Bot merged commit c37dd4b into rust-lang:main Nov 26, 2026
14 of 15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Rollup merge of #166171 - Dnreikronos:reborrow/multi_lifetime_no_reborrowed_field, r=Dnreikronos

borrowck: Fix Reborrow ICE on types with several lifetimes

Fixes #165653

The ICE comes from borrowck. When a Reborrow type has no field that actually gets reborrowed (no `&mut` and no other Reborrow type inside), borrowck borrows the whole value instead, and that code just assumed the type has one lifetime. Reborrow types can have more than one lifetime now, so something like the struct with `&'a &'b str` from the issue blew up right there.

The fix is in that fallback. When nothing gets reborrowed, borrowck now borrows the whole value once for each lifetime of the target type instead of panicking. With one lifetime it does exactly what it did before. With more it keeps the value borrowed while any of them is still alive, so two live reborrows of a two lifetime marker still get the usual "borrowed as mutable more than once" error, and using them one after the other is fine, same as with one lifetime. The program from the issue now gets the same error its one lifetime version already gets, since it's reborrowing through a shared ref.

I ran a bunch of programs against a recent nightly. Nothing that compiles there stops compiling, the crashes are gone (generic fields, associated type fields, fields that are only Copy in some functions, CoerceShared targets with more lifetimes than the source all hit the same spot), and the ones that error now get the same error as their one lifetime version. Two things I didn't touch are the `&'static mut` field ICE (#164933) and a CoerceShared panic when the source and target PhantomData fields don't match, that one also happens with one lifetime so it's a different bug.

Honestly I think borrowing under every lifetime is the safe choice but maybe not the smartest one, a smarter version could work out which lifetime really matters for each field. If someone who knows the Reborrow design better has an opinion on that I'm happy to change it.
mehmettoplu-rs pushed a commit to mehmettoplu-rs/miri that referenced this pull request Nov 27, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#164252 (Share retain implementation between Vec and VecDeque)
 - rust-lang/rust#166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - rust-lang/rust#166156 (tests: cover proc-macro metadata path remapping)
 - rust-lang/rust#166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - rust-lang/rust#166506 (traits: Fix dead match arm for String type suggestions.)
 - rust-lang/rust#164542 (Add regression test for rust-lang/rust#151312)
 - rust-lang/rust#165651 (compiletest: Further clean up directive parsing)
 - rust-lang/rust#166196 (autodiff: don't ICE on invalid macro arguments)
 - rust-lang/rust#166556 (remove `Box::into_boxed_slice`)
 - rust-lang/rust#166562 (docs: add backticks around `!` in primitive docs )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

[ICE]: ADT containing no '&mut T' or 'T: Reborrow' fields must only have one lifetime to implement Reborrow

6 participants