diff options
| author | Michael Goulet <michael@errs.io> | 2024-10-28 16:37:43 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-10-28 16:37:43 +0000 |
| commit | c50bc625867b13813a2c5d63f45b63ea6479a1be (patch) | |
| tree | c30a5f5427fcf1d8c2f81b6170536e952a60079c | |
| parent | 9f57edf2e261ade3c926d69459162057f15164d6 (diff) | |
| download | rust-c50bc625867b13813a2c5d63f45b63ea6479a1be.tar.gz rust-c50bc625867b13813a2c5d63f45b63ea6479a1be.zip | |
Inline obligation_for_method
| -rw-r--r-- | compiler/rustc_hir_typeck/src/method/mod.rs | 49 | ||||
| -rw-r--r-- | compiler/rustc_hir_typeck/src/op.rs | 26 |
2 files changed, 40 insertions, 35 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index e20a0cb67c3..0b481a94563 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -324,35 +324,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Ok(pick) } - pub(super) fn obligation_for_method( - &self, - cause: ObligationCause<'tcx>, - trait_def_id: DefId, - self_ty: Ty<'tcx>, - opt_input_types: Option<&[Ty<'tcx>]>, - ) -> (traits::PredicateObligation<'tcx>, ty::GenericArgsRef<'tcx>) { - // Construct a trait-reference `self_ty : Trait<input_tys>` - let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| { - match param.kind { - GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => {} - GenericParamDefKind::Type { .. } => { - if param.index == 0 { - return self_ty.into(); - } else if let Some(input_types) = opt_input_types { - return input_types[param.index as usize - 1].into(); - } - } - } - self.var_for_def(cause.span, param) - }); - - let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args); - - // Construct an obligation - let poly_trait_ref = ty::Binder::dummy(trait_ref); - (traits::Obligation::new(self.tcx, cause, self.param_env, poly_trait_ref), args) - } - /// `lookup_method_in_trait` is used for overloaded operators. /// It does a very narrow slice of what the normal probe/confirm path does. /// In particular, it doesn't really do any probing: it simply constructs @@ -367,8 +338,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self_ty: Ty<'tcx>, opt_input_types: Option<&[Ty<'tcx>]>, ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> { - let (obligation, args) = - self.obligation_for_method(cause, trait_def_id, self_ty, opt_input_types); + // Construct a trait-reference `self_ty : Trait<input_tys>` + let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind { + GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => { + unreachable!("did not expect operator trait to have lifetime/const") + } + GenericParamDefKind::Type { .. } => { + if param.index == 0 { + self_ty.into() + } else if let Some(input_types) = opt_input_types { + input_types[param.index as usize - 1].into() + } else { + self.var_for_def(cause.span, param) + } + } + }); + + let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args); + let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref); self.construct_obligation_for_trait(m_name, trait_def_id, obligation, args) } diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs index 1574e9e98d4..fe860f3f40d 100644 --- a/compiler/rustc_hir_typeck/src/op.rs +++ b/compiler/rustc_hir_typeck/src/op.rs @@ -15,7 +15,7 @@ use rustc_span::Span; use rustc_span::source_map::Spanned; use rustc_span::symbol::{Ident, sym}; use rustc_trait_selection::infer::InferCtxtExt; -use rustc_trait_selection::traits::{FulfillmentError, ObligationCtxt}; +use rustc_trait_selection::traits::{FulfillmentError, Obligation, ObligationCtxt}; use rustc_type_ir::TyKind::*; use tracing::debug; use {rustc_ast as ast, rustc_hir as hir}; @@ -931,9 +931,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.check_expr_coercible_to_type(rhs_expr, rhs_ty, None); } - let (obligation, _) = - self.obligation_for_method(cause, trait_did, lhs_ty, Some(input_types)); - // FIXME: This should potentially just add the obligation to the `FnCtxt` + // Construct an obligation `self_ty : Trait<input_tys>` + let args = + ty::GenericArgs::for_item(self.tcx, trait_did, |param, _| match param.kind { + ty::GenericParamDefKind::Lifetime + | ty::GenericParamDefKind::Const { .. } => { + unreachable!("did not expect operand trait to have lifetime/const args") + } + ty::GenericParamDefKind::Type { .. } => { + if param.index == 0 { + lhs_ty.into() + } else { + input_types[param.index as usize - 1].into() + } + } + }); + let obligation = Obligation::new( + self.tcx, + cause, + self.param_env, + ty::TraitRef::new_from_args(self.tcx, trait_did, args), + ); let ocx = ObligationCtxt::new_with_diagnostics(&self.infcx); ocx.register_obligation(obligation); Err(ocx.select_all_or_error()) |
