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 @@ pub(crate) fn try_overloaded_deref(
span: Span,
base_ty: Ty<'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.

View File

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

View File

@ -336,7 +336,7 @@ pub(super) fn lookup_method_in_trait(
m_name: Ident,
trait_def_id: DefId,
self_ty: Ty<'tcx>,
opt_input_types: Option<&[Ty<'tcx>]>,
opt_rhs_ty: Option<Ty<'tcx>>,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
// Construct a trait-reference `self_ty : Trait<input_tys>`
let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind {
@ -346,16 +346,24 @@ pub(super) fn lookup_method_in_trait(
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 if let Some(rhs_ty) = opt_rhs_ty {
assert_eq!(param.index, 1, "did not expect >1 param on operator trait");
rhs_ty.into()
} 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)
}
}
});
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);
let obligation = traits::Obligation::new(
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
if !self.predicate_may_hold(&obligation) {

View File

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

View File

@ -149,7 +149,7 @@ fn try_index_step(
// If some lookup succeeded, install method in table
let input_ty = self.next_ty_var(base_expr.span);
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 {
debug!("try_index_step: success, using overloaded indexing");
@ -189,7 +189,7 @@ pub(super) fn try_overloaded_place_op(
&self,
span: Span,
base_ty: Ty<'tcx>,
arg_tys: &[Ty<'tcx>],
opt_rhs_ty: Option<Ty<'tcx>>,
op: PlaceOp,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
debug!("try_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@ -207,7 +207,7 @@ pub(super) fn try_overloaded_place_op(
Ident::with_dummy_span(imm_op),
imm_tr,
base_ty,
Some(arg_tys),
opt_rhs_ty,
)
}
@ -215,7 +215,7 @@ fn try_mutable_overloaded_place_op(
&self,
span: Span,
base_ty: Ty<'tcx>,
arg_tys: &[Ty<'tcx>],
opt_rhs_ty: Option<Ty<'tcx>>,
op: PlaceOp,
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
debug!("try_mutable_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@ -233,7 +233,7 @@ fn try_mutable_overloaded_place_op(
Ident::with_dummy_span(mut_op),
mut_tr,
base_ty,
Some(arg_tys),
opt_rhs_ty,
)
}
@ -284,7 +284,7 @@ pub(crate) fn convert_place_derefs_to_mutable(&self, expr: &hir::Expr<'_>) {
&& let Some(ok) = self.try_mutable_overloaded_place_op(
expr.span,
source,
&[],
None,
PlaceOp::Deref,
)
{
@ -359,8 +359,7 @@ fn convert_place_op_to_mutable(
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_tys, op);
let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_ty, op);
let method = match method {
Some(ok) => self.register_infer_ok_obligations(ok),
// Couldn't find the mutable variant of the place op, keep the