Conditional borrow with RPIT
@eopb is already working on this.
Since Feb 25, 2026.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
Code
fn foo<'a, 'b>(one: &'a (), two: &'b ()) -> impl Sized {
if true { one } else { two }
}
Output
Checking zk v0.1.0 (/private/var/folders/9d/tqvdlqrd6fz7t8746q0jdjzr0000gp/T/tmp.zk1OwsfTpg/zk)
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
╭▸ src/lib.rs:25:28
│
24 │ fn foo<'a, 'b>(one: &'a (), two: &'b ()) -> impl Sized {
│ ────────── opaque type defined here
25 │ if true { one } else { two }
│ ━━━
│
╰ note: hidden type `&()` captures lifetime `'_`
For more information about this error, try `rustc --explain E0700`.
error: could not compile `zk` (lib) due to 1 previous error
Desired output
I'm not sure what I want the compiler to tell me here because I'm still a little confused as to why this doesn't compile. Currently the output confusingly mentions '_ which appears nowhere in our function signature. If '_ refers to a lifetime that comes from the body of the function, that we cannot name, I'd expect it to be called something like '0 in the diagnostic.
These two snippets compile:
fn foo<'a, 'b>(one: &'a (), two: &'b ()) -> impl Sized {
one
}
and
fn foo<'a, 'b>(one: &'a (), two: &'b ()) -> impl Sized {
two
}
Under edition 2024 capture rules, I expect the return type to implicitly be impl Sized + use<'a, 'b>. Since we can see both one and two satisfy that type in the two snippets above, I'd expect variance to handle the original case where I return conditionally.
Rust version
rustc 1.95.0-nightly (3c9faa0d0 2026-02-16)
binary: rustc
commit-hash: 3c9faa0d037b9eecda4a440cc482ff7f960fb8a5
commit-date: 2026-02-16
host: aarch64-apple-darwin
release: 1.95.0-nightly
LLVM version: 22.1.0
@rustbot label +A-diagnostics +T-compiler +A-impl-trait
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.