Skip to content

Commit c50bc62

Browse files
Inline obligation_for_method
1 parent 9f57edf commit c50bc62

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

Diff for: compiler/rustc_hir_typeck/src/method/mod.rs

+18-31
Original file line numberDiff line numberDiff line change
@@ -324,35 +324,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
324324
Ok(pick)
325325
}
326326

327-
pub(super) fn obligation_for_method(
328-
&self,
329-
cause: ObligationCause<'tcx>,
330-
trait_def_id: DefId,
331-
self_ty: Ty<'tcx>,
332-
opt_input_types: Option<&[Ty<'tcx>]>,
333-
) -> (traits::PredicateObligation<'tcx>, ty::GenericArgsRef<'tcx>) {
334-
// Construct a trait-reference `self_ty : Trait<input_tys>`
335-
let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| {
336-
match param.kind {
337-
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => {}
338-
GenericParamDefKind::Type { .. } => {
339-
if param.index == 0 {
340-
return self_ty.into();
341-
} else if let Some(input_types) = opt_input_types {
342-
return input_types[param.index as usize - 1].into();
343-
}
344-
}
345-
}
346-
self.var_for_def(cause.span, param)
347-
});
348-
349-
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args);
350-
351-
// Construct an obligation
352-
let poly_trait_ref = ty::Binder::dummy(trait_ref);
353-
(traits::Obligation::new(self.tcx, cause, self.param_env, poly_trait_ref), args)
354-
}
355-
356327
/// `lookup_method_in_trait` is used for overloaded operators.
357328
/// It does a very narrow slice of what the normal probe/confirm path does.
358329
/// In particular, it doesn't really do any probing: it simply constructs
@@ -367,8 +338,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
367338
self_ty: Ty<'tcx>,
368339
opt_input_types: Option<&[Ty<'tcx>]>,
369340
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
370-
let (obligation, args) =
371-
self.obligation_for_method(cause, trait_def_id, self_ty, opt_input_types);
341+
// Construct a trait-reference `self_ty : Trait<input_tys>`
342+
let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind {
343+
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => {
344+
unreachable!("did not expect operator trait to have lifetime/const")
345+
}
346+
GenericParamDefKind::Type { .. } => {
347+
if param.index == 0 {
348+
self_ty.into()
349+
} else if let Some(input_types) = opt_input_types {
350+
input_types[param.index as usize - 1].into()
351+
} else {
352+
self.var_for_def(cause.span, param)
353+
}
354+
}
355+
});
356+
357+
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args);
358+
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref);
372359
self.construct_obligation_for_trait(m_name, trait_def_id, obligation, args)
373360
}
374361

Diff for: compiler/rustc_hir_typeck/src/op.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::Span;
1515
use rustc_span::source_map::Spanned;
1616
use rustc_span::symbol::{Ident, sym};
1717
use rustc_trait_selection::infer::InferCtxtExt;
18-
use rustc_trait_selection::traits::{FulfillmentError, ObligationCtxt};
18+
use rustc_trait_selection::traits::{FulfillmentError, Obligation, ObligationCtxt};
1919
use rustc_type_ir::TyKind::*;
2020
use tracing::debug;
2121
use {rustc_ast as ast, rustc_hir as hir};
@@ -931,9 +931,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
931931
self.check_expr_coercible_to_type(rhs_expr, rhs_ty, None);
932932
}
933933

934-
let (obligation, _) =
935-
self.obligation_for_method(cause, trait_did, lhs_ty, Some(input_types));
936-
// FIXME: This should potentially just add the obligation to the `FnCtxt`
934+
// Construct an obligation `self_ty : Trait<input_tys>`
935+
let args =
936+
ty::GenericArgs::for_item(self.tcx, trait_did, |param, _| match param.kind {
937+
ty::GenericParamDefKind::Lifetime
938+
| ty::GenericParamDefKind::Const { .. } => {
939+
unreachable!("did not expect operand trait to have lifetime/const args")
940+
}
941+
ty::GenericParamDefKind::Type { .. } => {
942+
if param.index == 0 {
943+
lhs_ty.into()
944+
} else {
945+
input_types[param.index as usize - 1].into()
946+
}
947+
}
948+
});
949+
let obligation = Obligation::new(
950+
self.tcx,
951+
cause,
952+
self.param_env,
953+
ty::TraitRef::new_from_args(self.tcx, trait_did, args),
954+
);
937955
let ocx = ObligationCtxt::new_with_diagnostics(&self.infcx);
938956
ocx.register_obligation(obligation);
939957
Err(ocx.select_all_or_error())

0 commit comments

Comments
 (0)