Inline obligation_for_method

This commit is contained in:
Michael Goulet 2024-10-28 16:37:43 +00:00
parent 9f57edf2e2
commit c50bc62586
2 changed files with 40 additions and 35 deletions

View File

@ -324,35 +324,6 @@ pub(crate) fn lookup_probe_for_diagnostic(
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 @@ pub(super) fn lookup_method_in_trait(
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)
}

View File

@ -15,7 +15,7 @@
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 @@ fn lookup_op_method(
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())