What's the right syntax for an infallible reference?
⚓ Rust 📅 2026-03-26 👤 surdeus 👁️ 4The compiler does not recognise Ok(&'r !) as an infallible variant. I'm starting with the assumption that the error is in my own understanding and/or syntax and am turning up a blank on solving this.
#![feature(never_type)]
#![feature(try_trait_v2)]
use std::ops::FromResidual;
enum MyResult<'r, T, E> {
Ok(&'r T),
Err(&'r E),
}
// This should compile - instead it gives error[E0004]: non-exhaustive patterns: `lifetimes::MyResult::Ok(_)` not covered
impl<'r, T, E> FromResidual<MyResult<'r, !, E>> for MyResult<'r, T, E> {
fn from_residual(residual: MyResult<'r, !, E>) -> Self {
match residual {
MyResult::Err(_) => todo!(),
}
}
}
// Longhand alternative ...
enum MyResidual<'r, E> {
Ok(&'r !),
Err(&'r E),
}
// ... also refuses to compile with error[E0004]: non-exhaustive patterns: `MyResidual::Ok(_)` not covered
impl<'r, T, E> FromResidual<MyResidual<'r, E>> for MyResult<'r, T, E> {
fn from_residual(residual: MyResidual<'r, E>) -> Self {
match residual {
MyResidual::Err(_) => todo!(),
}
}
}
// For comparison this works fine ...
enum MyOwnedResidual<'r, E> {
Ok(!),
Err(&'r E),
}
// ... and compiles without requiring an Ok(_) pattern
impl<'r, T, E> FromResidual<MyOwnedResidual<'r, E>> for MyResult<'r, T, E> {
fn from_residual(residual: MyOwnedResidual<'r, E>) -> Self {
match residual {
MyOwnedResidual::Err(_) => todo!(),
}
}
}
here's this example on playground
The same thing happens if using std::convert::Infallible rather than !
5 posts - 4 participants
🏷️ Rust_feed