From 850ea9953c5d4811d308acd6eb4012c787cc06ea Mon Sep 17 00:00:00 2001 From: Thomas Van Strydonck Date: Mon, 4 Aug 2025 15:46:12 +0200 Subject: [PATCH] fix: match on uninhabited types at compile time uninhabited types should be dealt with at compile time, not through the runtime panic that `unreachable!` throws since rust 1.82, Rust has better ergonomics for matching on uninhabited enum variants, which makes this easier for owned types (https://github.com/rust-lang/rust/pull/122792) --- openraft/src/error.rs | 1 - openraft/src/error/into_ok.rs | 6 +++++- openraft/src/error/streaming_error.rs | 4 ---- rt-monoio/src/lib.rs | 7 ++----- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/openraft/src/error.rs b/openraft/src/error.rs index 36788edca..d492e2baa 100644 --- a/openraft/src/error.rs +++ b/openraft/src/error.rs @@ -321,7 +321,6 @@ where C: RaftTypeConfig RPCError::Unreachable(e) => RPCError::Unreachable(e), RPCError::PayloadTooLarge(e) => RPCError::PayloadTooLarge(e), RPCError::Network(e) => RPCError::Network(e), - RPCError::RemoteError(_infallible) => unreachable!(), } } } diff --git a/openraft/src/error/into_ok.rs b/openraft/src/error/into_ok.rs index a023ef541..1b248683d 100644 --- a/openraft/src/error/into_ok.rs +++ b/openraft/src/error/into_ok.rs @@ -11,7 +11,11 @@ where E: Into fn into_ok(self) -> T { match self { Ok(t) => t, - Err(_) => unreachable!(), + Err(e) => { + // NOTE: `allow` required because of buggy reachability detection by rust compiler + #[allow(unreachable_code)] + match e.into() {} + } } } } diff --git a/openraft/src/error/streaming_error.rs b/openraft/src/error/streaming_error.rs index d3ab25f11..3c77e7ec1 100644 --- a/openraft/src/error/streaming_error.rs +++ b/openraft/src/error/streaming_error.rs @@ -48,7 +48,6 @@ impl From> for ReplicationError { impl From> for StreamingError { fn from(value: RPCError) -> Self { - #[allow(unreachable_patterns)] match value { RPCError::Timeout(e) => StreamingError::Timeout(e), RPCError::Unreachable(e) => StreamingError::Unreachable(e), @@ -56,9 +55,6 @@ impl From> for StreamingError { unreachable!("PayloadTooLarge should not be converted to StreamingError") } RPCError::Network(e) => StreamingError::Network(e), - RPCError::RemoteError(_e) => { - unreachable!("Infallible error should not be produced at all") - } } } } diff --git a/rt-monoio/src/lib.rs b/rt-monoio/src/lib.rs index d339ba348..68b398e7c 100644 --- a/rt-monoio/src/lib.rs +++ b/rt-monoio/src/lib.rs @@ -74,11 +74,8 @@ impl AsyncRuntime for MonoioRuntime { } #[inline] - fn is_panic(_join_error: &Self::JoinError) -> bool { - // Given that joining a task will never fail, i.e., `Self::JoinError` - // will never be constructed, and it is impossible to construct an - // enum like `Infallible`, this function could never be invoked. - unreachable!("unreachable since argument `join_error` could never be constructed") + fn is_panic(join_error: &Self::JoinError) -> bool { + match *join_error {} } #[inline]