Remove some goofy slice logic from the operator path

This commit is contained in:
Michael Goulet 2024-10-28 21:22:23 +00:00
parent f202abd4d6
commit 3240fe2773
5 changed files with 26 additions and 25 deletions

View File

@ -23,7 +23,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span, span: Span,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> { ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
self.try_overloaded_place_op(span, base_ty, &[], PlaceOp::Deref) self.try_overloaded_place_op(span, base_ty, None, PlaceOp::Deref)
} }
/// Returns the adjustment steps. /// Returns the adjustment steps.

View File

@ -1,4 +1,4 @@
use std::{iter, slice}; use std::iter;
use rustc_ast::util::parser::PREC_UNAMBIGUOUS; use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey}; use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
@ -300,7 +300,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ident::with_dummy_span(method_name), Ident::with_dummy_span(method_name),
trait_def_id, trait_def_id,
adjusted_ty, adjusted_ty,
opt_input_type.as_ref().map(slice::from_ref), opt_input_type,
) { ) {
let method = self.register_infer_ok_obligations(ok); let method = self.register_infer_ok_obligations(ok);
let mut autoref = None; let mut autoref = None;

View File

@ -336,7 +336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
m_name: Ident, m_name: Ident,
trait_def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
opt_input_types: Option<&[Ty<'tcx>]>, opt_rhs_ty: Option<Ty<'tcx>>,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> { ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
// Construct a trait-reference `self_ty : Trait<input_tys>` // Construct a trait-reference `self_ty : Trait<input_tys>`
let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind { let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind {
@ -346,16 +346,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
GenericParamDefKind::Type { .. } => { GenericParamDefKind::Type { .. } => {
if param.index == 0 { if param.index == 0 {
self_ty.into() self_ty.into()
} else if let Some(input_types) = opt_input_types { } else if let Some(rhs_ty) = opt_rhs_ty {
input_types[param.index as usize - 1].into() assert_eq!(param.index, 1, "did not expect >1 param on operator trait");
rhs_ty.into()
} else { } else {
// FIXME: We should stop passing `None` for the failure case
// when probing for call exprs. I.e. `opt_rhs_ty` should always
// be set when it needs to be.
self.var_for_def(cause.span, param) 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(
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref); self.tcx,
cause,
self.param_env,
ty::TraitRef::new_from_args(self.tcx, trait_def_id, args),
);
// Now we want to know if this can be matched // Now we want to know if this can be matched
if !self.predicate_may_hold(&obligation) { if !self.predicate_may_hold(&obligation) {

View File

@ -895,7 +895,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let opname = Ident::with_dummy_span(opname); let opname = Ident::with_dummy_span(opname);
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip(); let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
let input_types = opt_rhs_ty.as_slice();
let cause = self.cause(span, ObligationCauseCode::BinOp { let cause = self.cause(span, ObligationCauseCode::BinOp {
lhs_hir_id: lhs_expr.hir_id, lhs_hir_id: lhs_expr.hir_id,
rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id), rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id),
@ -904,13 +903,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
output_ty: expected.only_has_type(self), output_ty: expected.only_has_type(self),
}); });
let method = self.lookup_method_in_trait( let method =
cause.clone(), self.lookup_method_in_trait(cause.clone(), opname, trait_did, lhs_ty, opt_rhs_ty);
opname,
trait_did,
lhs_ty,
Some(input_types),
);
match method { match method {
Some(ok) => { Some(ok) => {
let method = self.register_infer_ok_obligations(ok); let method = self.register_infer_ok_obligations(ok);
@ -942,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if param.index == 0 { if param.index == 0 {
lhs_ty.into() lhs_ty.into()
} else { } else {
input_types[param.index as usize - 1].into() opt_rhs_ty.expect("expected RHS for binop").into()
} }
} }
}); });

View File

@ -149,7 +149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// If some lookup succeeded, install method in table // If some lookup succeeded, install method in table
let input_ty = self.next_ty_var(base_expr.span); let input_ty = self.next_ty_var(base_expr.span);
let method = let method =
self.try_overloaded_place_op(expr.span, self_ty, &[input_ty], PlaceOp::Index); self.try_overloaded_place_op(expr.span, self_ty, Some(input_ty), PlaceOp::Index);
if let Some(result) = method { if let Some(result) = method {
debug!("try_index_step: success, using overloaded indexing"); debug!("try_index_step: success, using overloaded indexing");
@ -189,7 +189,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
span: Span, span: Span,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
arg_tys: &[Ty<'tcx>], opt_rhs_ty: Option<Ty<'tcx>>,
op: PlaceOp, op: PlaceOp,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> { ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
debug!("try_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op); debug!("try_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@ -207,7 +207,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ident::with_dummy_span(imm_op), Ident::with_dummy_span(imm_op),
imm_tr, imm_tr,
base_ty, base_ty,
Some(arg_tys), opt_rhs_ty,
) )
} }
@ -215,7 +215,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
span: Span, span: Span,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
arg_tys: &[Ty<'tcx>], opt_rhs_ty: Option<Ty<'tcx>>,
op: PlaceOp, op: PlaceOp,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> { ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
debug!("try_mutable_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op); debug!("try_mutable_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@ -233,7 +233,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ident::with_dummy_span(mut_op), Ident::with_dummy_span(mut_op),
mut_tr, mut_tr,
base_ty, base_ty,
Some(arg_tys), opt_rhs_ty,
) )
} }
@ -284,7 +284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& let Some(ok) = self.try_mutable_overloaded_place_op( && let Some(ok) = self.try_mutable_overloaded_place_op(
expr.span, expr.span,
source, source,
&[], None,
PlaceOp::Deref, PlaceOp::Deref,
) )
{ {
@ -359,8 +359,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some(self.typeck_results.borrow().node_args(expr.hir_id).type_at(1)) Some(self.typeck_results.borrow().node_args(expr.hir_id).type_at(1))
} }
}; };
let arg_tys = arg_ty.as_slice(); let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_ty, op);
let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_tys, op);
let method = match method { let method = match method {
Some(ok) => self.register_infer_ok_obligations(ok), Some(ok) => self.register_infer_ok_obligations(ok),
// Couldn't find the mutable variant of the place op, keep the // Couldn't find the mutable variant of the place op, keep the