feat(Reborrow): Allow multiple lifetimes #163284
This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #<issueNr>' to the PR description to autoclose the issue upon merge.
:umbrella: The latest upstream changes (presumably #163419) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.
I couldn't find any in-tree docs about the lifetime handling. Should probably add some now that you aren't requiring equality anymore. I had to grab a variance table and poke at the code and tests here until I was sure it was all in the right direction 😆
probably needs updating of the diagnostic (and at minimum of the diagnostic struct name)
probably want to use more precise span for all the MalformedTarget errors, usually you have a specific item available which has its own span
Requested changes.
Reminder, once the PR becomes ready for a review, use @rustbot ready.
What would happen if the lifetime generic already has a _ suffix?
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.
Where should they be located in? Just here in the code?
Added a names_set to ensure no conflicts happen
Added more specific errors.
Updated diagnostics name and message
trait_params.push(cx.lifetime_param(lt.ident, Default::default()));
Fixed and also added a helper function for the WherePredicate creation.
@rustbot ready
The job test-x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)
<details><summary><i>Click to see the possible cause of the failure (guessed by this bot)</i></summary>
[RUSTC-TIMING] rustc_metadata test:false 17.248
Compiling rustc_codegen_ssa v0.0.0 (/checkout/compiler/rustc_codegen_ssa)
[RUSTC-TIMING] rustc_borrowck test:false 79.030
Compiling rustc_resolve v0.0.0 (/checkout/compiler/rustc_resolve)
warning: unused imports: `GenericBound`, `GenericParamKind`, `GenericParam`, `WherePredicateKind`, `WherePredicate`, and `WhereRegionPredicate`
--> compiler/rustc_builtin_macros/src/deriving/reborrow.rs:2:74
|
2 | self as ast, AngleBracketedArg, AttrArgs, DUMMY_NODE_ID, GenericArg, GenericBound,
| ^^^^^^^^^^^^
3 | GenericParam, GenericParamKind, Generics, ItemKind, WherePredicate, WherePredicateKind,
| ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
4 | WhereRegionPredicate, token,
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
warning: unused import: `DUMMY_SP`
--> compiler/rustc_builtin_macros/src/deriving/reborrow.rs:10:18
|
10 | use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
| ^^^^^^^^
warning: unused import: `thin_vec`
--> compiler/rustc_builtin_macros/src/deriving/reborrow.rs:11:25
|
</details>
yea, there are existing docs in the code, but they don't mention lifetimes yet at all beyond the trivial usage
Added inline comments to this. You might want to also read https://hackmd.io/xl3EvNkZSHqqNi_K2XwdRg regarding this - if we do go back to an associated type on CoerceShared (which I think we should - otherwise we cannot represent &mut T and &T using Reborrow and CoerceShared alone) then the lifetime stuff here will revert to an invariant again :D
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.
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.
:umbrella: The latest upstream changes (presumably #164699) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.
r=me with rebase and squashing
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.
@rustbot ready
The job test-x86_64-gnu-gcc failed! Check out the build log: (web) (plain enhanced) (plain)
<details><summary><i>Click to see the possible cause of the failure (guessed by this bot)</i></summary>
- --> $DIR/coerce-shared-marker-no-target-lifetime.rs:13:10
+ error: implementing `CoerceShared` requires source and target structs to use the same field style
+ --> $DIR/coerce-shared-marker-no-target-lifetime.rs:13:1
3 |
4 LL | impl<'a> CoerceShared<CustomMarkerRef> for CustomMarker<'a> {}
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6
7 error: aborting due to 1 previous error
8
---
To only update this specific test, also pass `--test-args reborrow/coerce-shared-marker-no-target-lifetime.rs`
error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/reborrow/coerce-shared-marker-no-target-lifetime.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Zcodegen-backend=gcc" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reborrow/coerce-shared-marker-no-target-lifetime" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2024"
stdout: none
--- stderr -------------------------------
error: implementing `CoerceShared` requires source and target structs to use the same field style
##[error] --> /checkout/tests/ui/reborrow/coerce-shared-marker-no-target-lifetime.rs:13:1
|
LL | impl<'a> CoerceShared<CustomMarkerRef> for CustomMarker<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
------------------------------------------
---
- --> $DIR/invalid-coerce-shared-static-mut-field.rs:20:5
+ error: implementing `CoerceShared` currently requires source and target to have at most one non-ZST reborrow data field
+ --> $DIR/invalid-coerce-shared-static-mut-field.rs:24:1
3 |
- LL | a: &'static mut u8,
- | ------------------ source field `a` has type `&'static mut u8`
- ...
- LL | a: &'a u8,
- | ^^^^^^^^^ target field `a` has type `&'a u8`
- ...
10 LL | impl<'a> CoerceShared<ReorderRef<'a>> for ReorderMut<'a> {}
- | ----------------------------------------------------------- required by this `CoerceShared` implementation
+ | ^^^^^^^^^^^^^^^^^^^^^^--------------^^^^^^--------------^^^
+ | | | |
+ | | | source type has 2 non-ZST reborrow data fields
+ | | target type has 2 non-ZST reborrow data fields
---
To only update this specific test, also pass `--test-args reborrow/invalid-coerce-shared-static-mut-field.rs`
error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/reborrow/invalid-coerce-shared-static-mut-field.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Zcodegen-backend=gcc" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reborrow/invalid-coerce-shared-static-mut-field" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error: implementing `CoerceShared` currently requires source and target to have at most one non-ZST reborrow data field
##[error] --> /checkout/tests/ui/reborrow/invalid-coerce-shared-static-mut-field.rs:24:1
|
LL | impl<'a> CoerceShared<ReorderRef<'a>> for ReorderMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^--------------^^^^^^--------------^^^
| | | |
| | | source type has 2 non-ZST reborrow data fields
| | target type has 2 non-ZST reborrow data fields
| in this `CoerceShared` implementation
---
To only update this specific test, also pass `--test-args reborrow/invalid-reborrow-derive-const-param.rs`
error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/reborrow/invalid-reborrow-derive-const-param.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Zcodegen-backend=gcc" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reborrow/invalid-reborrow-derive-const-param" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error: implementing `Reborrow` requires that at least one lifetime parameter
##[error] --> /checkout/tests/ui/reborrow/invalid-reborrow-derive-const-param.rs:11:10
|
---
---- [ui] tests/ui/reborrow/invalid-reborrow-impl-no-lifetime.rs stdout ----
error: Error: expected failure status (Some(1)) but received status Some(101).
status: exit status: 101
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/reborrow/invalid-reborrow-impl-no-lifetime.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Zcodegen-backend=gcc" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reborrow/invalid-reborrow-impl-no-lifetime" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
##[error]error: internal compiler error: compiler/rustc_borrowck/src/borrow_set.rs:403:17: ADT containing no '&mut T' or 'T: Reborrow' fields must only have one lifetime to implement Reborrow
thread 'rustc' (139118) panicked at compiler/rustc_borrowck/src/borrow_set.rs:403:17:
Box<dyn Any>
stack backtrace:
0: std::panicking::begin_panic::<rustc_span::macros::ExplicitBug>
1: rustc_span::macros::bug_impl
2: <rustc_borrowck::borrow_set::GatherBorrows>::gather_reborrows
3: <rustc_borrowck::borrow_set::GatherBorrows as rustc_middle::mir::visit::Visitor>::visit_assign
4: <rustc_borrowck::borrow_set::BorrowSet>::build
5: rustc_borrowck::borrowck_collect_region_constraints
6: <rustc_borrowck::root_cx::BorrowCheckRootCtxt>::do_mir_borrowck
7: rustc_borrowck::mir_borrowck
[... omitted 1 frame ...]
8: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface::passes::run_required_analyses::{closure#2}::{closure#0}>::{closure#0}
9: rustc_data_structures::sync::parallel::par_for_each_in::<rustc_span::def_id::LocalDefId, [rustc_span::def_id::LocalDefId], <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners<rustc_interface::passes::run_required_analyses::{closure#2}::{closure#0}>::{closure#0}>
10: <rustc_session::session::Session>::time::<(), rustc_interface::passes::run_required_analyses::{closure#2}>
11: rustc_interface::passes::analysis
[... omitted 1 frame ...]
12: <std::thread::local::LocalKey<core::cell::Cell<*const ()>>>::with::<rustc_middle::ty::context::tls::enter_context<<rustc_middle::ty::context::GlobalCtxt>::enter<rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::compiler_entrypoint::{closure#0}::{closure#1}>::{closure#2}, core::option::Option<rustc_interface::queries::Linker>>::{closure#1}, core::option::Option<rustc_interface::queries::Linker>>::{closure#0}, core::option::Option<rustc_interface::queries::Linker>>
13: <rustc_middle::ty::context::TyCtxt>::create_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::compiler_entrypoint::{closure#0}::{closure#1}>::{closure#2}>
14: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::compiler_entrypoint::{closure#0}::{closure#1}>
15: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}::{closure#0}, ()>
16: rustc_span::create_session_globals_then::<(), rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: please make sure that you have updated to the latest nightly
note: rustc 1.101.0-nightly (c1168185a 2026-11-01) running on x86_64-unknown-linux-gnu
note: compiler flags: -Z simulate-remapped-rust-src-base=/rustc/FAKE_PREFIX -Z translate-remapped-path-to-local-path=no -Z ignore-directory-in-diagnostics-source-blocks=/cargo -Z ignore-directory-in-diagnostics-source-blocks=/checkout/vendor -Z codegen-backend=gcc -C codegen-units=1 -Z ui-testing -Z deduplicate-diagnostics=no -Z write-long-types-to-disk=no -C strip=debuginfo -C prefer-dynamic -Z next-solver=coherence -C rpath -C debuginfo=0
query stack during panic:
#0 [mir_borrowck] borrow-checking `main`
#1 [analysis] running analysis passes on crate `invalid_reborrow_impl_no_lifetime`
end of query stack
error: aborting due to 1 previous error
------------------------------------------
---
To only update this specific test, also pass `--test-args reborrow/invalid-reborrow-impl-phantom-data-wrong-generics.rs`
error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/reborrow/invalid-reborrow-impl-phantom-data-wrong-generics.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Zcodegen-backend=gcc" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reborrow/invalid-reborrow-impl-phantom-data-wrong-generics" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0107]: struct takes 0 lifetime arguments but 1 lifetime argument was supplied
##[error] --> /checkout/tests/ui/reborrow/invalid-reborrow-impl-phantom-data-wrong-generics.rs:9:23
|
</details>
[!IMPORTANT]
For more information how to resolve CI failures of this job, visit this link.
@bors r+ rollup
:pushpin: Commit 500e0c68d175573b0bf1ec878d595f99d3853e95 has been approved by oli-obk
It is now in the queue for this repository.
oli-obk
aapoalas
<!-- homu-ignore:start -->
View all comments
<!-- homu-ignore:end -->
<!-- homu-ignore:start -->
<!--
Please read our [LLM policy] before opening a PR,
If you used an LLM to generate any part of this PR, including the PR description, please disclose that according to our [guidelines][disclosure guidelines].
LLM contributions are not banned, but are held to a higher standard of review and correctness.
[LLM policy]: https://forge.rust-lang.org/policies/llm-usage.html
[disclosure guidelines]: https://rustc-dev-guide.rust-lang.org/llm-guidance/writing.html#disclosure-guidelines
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.
This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using
r? <reviewer name>
When merged, your PR's description becomes part of the commit message of a merge commit.
If you do not want certain parts of it (such as your LLM disclosure) to show up in the permanent git history,
surround them with a pair of HTML comments containing
homu-ignore:startandhomu-ignore:end.-->
<!-- homu-ignore:end -->
The
Reborrow/CoerceSharedborrow checking code is now robust enough to properly handle multiple lifetimes, and for that we can allow multiple lifetimes in the traits. This requires making theCoerceSharedderive macro produce multiple lifetimes and in particular produce'a_ where'a: 'a_` additions to the generics, and removing some now-unnecessary checks from the wf-checks.The derive trait code is some of the uglies stuff I've ever written.
r? @oli-obk