From e5803fceed33ce5bb2d0f29f8fd0db6a70496326 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Wed, 12 Nov 2025 21:59:11 -0800 Subject: [PATCH] Move `into_try_type` to a free function --- compiler/rustc_hir/src/lang_items.rs | 2 +- library/core/src/ops/try_trait.rs | 20 +++++++++++-------- ...on_traits.PreCodegen.after.panic-abort.mir | 2 +- ...n_traits.PreCodegen.after.panic-unwind.mir | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 0b236cebdcaa..391fba8395ce 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -372,7 +372,7 @@ language_item_table! { TryTraitFromOutput, sym::from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; TryTraitFromYeet, sym::from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None; - ResidualIntoTryType, sym::into_try_type, into_try_type_fn, Target::Method(MethodKind::Trait { body: true }), GenericRequirement::None; + ResidualIntoTryType, sym::into_try_type, into_try_type_fn, Target::Fn, GenericRequirement::None; CoercePointeeValidated, sym::coerce_pointee_validated, coerce_pointee_validated_trait, Target::Trait, GenericRequirement::Exact(0); diff --git a/library/core/src/ops/try_trait.rs b/library/core/src/ops/try_trait.rs index 374e59e5d75c..f68782c804cd 100644 --- a/library/core/src/ops/try_trait.rs +++ b/library/core/src/ops/try_trait.rs @@ -364,15 +364,19 @@ pub const trait Residual: Sized { /// The "return" type of this meta-function. #[unstable(feature = "try_trait_v2_residual", issue = "91285")] type TryType: [const] Try; +} - /// Here for convenience in the `?` desugaring. - /// Probably should not be stabilized, as it should never be overridden. - /// (without a `final fn` of some form, cc RFC#3678) - #[unstable(feature = "try_trait_v2_residual", issue = "91285")] - #[lang = "into_try_type"] - fn into_try_type(self) -> Self::TryType { - FromResidual::from_residual(self) - } +/// Used in `try {}` blocks so the type produced in the `?` desugaring +/// depends on the residual type `R` and the output type of the block `O`, +/// but importantly not on the contextual type the way it would be if +/// we called `<_ as FromResidual>::from_residual(r)` directly. +#[unstable(feature = "try_trait_v2_residual", issue = "91285")] +// needs to be `pub` to avoid `private type` errors +#[expect(unreachable_pub)] +#[inline] // FIXME: force would be nice, but fails -- see #148915 +#[lang = "into_try_type"] +pub fn residual_into_try_type, O>(r: R) -> >::TryType { + FromResidual::from_residual(r) } #[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")] diff --git a/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-abort.mir index 94c430ea2df3..55f44d954d80 100644 --- a/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-abort.mir @@ -34,7 +34,7 @@ fn option_traits(_1: Option) -> Option { } bb3: { - _0 = as Residual>::into_try_type(const Option::::None) -> [return: bb4, unwind unreachable]; + _0 = ops::try_trait::residual_into_try_type::, u32>(const Option::::None) -> [return: bb4, unwind unreachable]; } bb4: { diff --git a/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-unwind.mir index 43058051c32c..b42aea38d3e3 100644 --- a/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.PreCodegen.after.panic-unwind.mir @@ -34,7 +34,7 @@ fn option_traits(_1: Option) -> Option { } bb3: { - _0 = as Residual>::into_try_type(const Option::::None) -> [return: bb4, unwind continue]; + _0 = ops::try_trait::residual_into_try_type::, u32>(const Option::::None) -> [return: bb4, unwind continue]; } bb4: {