From aa854485cea4da95606b9b61fe5a6a0961e97402 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Fri, 9 Aug 2024 23:28:40 -0500 Subject: [PATCH] Explicitly specify type parameter on FromResidual impls in stdlib. To work around coherence issue. Also adds regression test. --- core/src/ops/control_flow.rs | 4 +++- core/src/option.rs | 4 +++- core/tests/ops.rs | 1 + core/tests/ops/from_residual.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 core/tests/ops/from_residual.rs diff --git a/core/src/ops/control_flow.rs b/core/src/ops/control_flow.rs index a2709c66b06ad..ab73dc19fcc73 100644 --- a/core/src/ops/control_flow.rs +++ b/core/src/ops/control_flow.rs @@ -116,7 +116,9 @@ impl ops::Try for ControlFlow { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::FromResidual for ControlFlow { +// Note: manually specifying the residual type instead of using the default to work around +// https://github.com/rust-lang/rust/issues/99940 +impl ops::FromResidual> for ControlFlow { #[inline] fn from_residual(residual: ControlFlow) -> Self { match residual { diff --git a/core/src/option.rs b/core/src/option.rs index 6c89c81018038..9cec79c17ca73 100644 --- a/core/src/option.rs +++ b/core/src/option.rs @@ -2495,7 +2495,9 @@ impl ops::Try for Option { } #[unstable(feature = "try_trait_v2", issue = "84277")] -impl ops::FromResidual for Option { +// Note: manually specifying the residual type instead of using the default to work around +// https://github.com/rust-lang/rust/issues/99940 +impl ops::FromResidual> for Option { #[inline] fn from_residual(residual: Option) -> Self { match residual { diff --git a/core/tests/ops.rs b/core/tests/ops.rs index 2ee0abd399bb6..501e0f33fe4cc 100644 --- a/core/tests/ops.rs +++ b/core/tests/ops.rs @@ -1,4 +1,5 @@ mod control_flow; +mod from_residual; use core::ops::{ Bound, Deref, DerefMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, diff --git a/core/tests/ops/from_residual.rs b/core/tests/ops/from_residual.rs new file mode 100644 index 0000000000000..d5c86ccbcd317 --- /dev/null +++ b/core/tests/ops/from_residual.rs @@ -0,0 +1,26 @@ +//! Regression test that Option and ControlFlow can have downstream FromResidual impls. +//! cc https://github.com/rust-lang/rust/issues/99940, +//! This does NOT test that issue in general; Option and ControlFlow's FromResidual +//! impls in core were changed to not be affected by that issue. + +use core::ops::{ControlFlow, FromResidual}; + +struct Local; + +impl FromResidual for Option { + fn from_residual(_: Local) -> Option { + unimplemented!() + } +} + +impl FromResidual for ControlFlow { + fn from_residual(_: Local) -> ControlFlow { + unimplemented!() + } +} + +impl FromResidual for Result { + fn from_residual(_: Local) -> Result { + unimplemented!() + } +}