Skip to content

Commit 3240fe2

Browse files
Remove some goofy slice logic from the operator path
1 parent f202abd commit 3240fe2

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2323
span: Span,
2424
base_ty: Ty<'tcx>,
2525
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
26-
self.try_overloaded_place_op(span, base_ty, &[], PlaceOp::Deref)
26+
self.try_overloaded_place_op(span, base_ty, None, PlaceOp::Deref)
2727
}
2828

2929
/// Returns the adjustment steps.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{iter, slice};
1+
use std::iter;
22

33
use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
44
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
@@ -300,7 +300,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
300300
Ident::with_dummy_span(method_name),
301301
trait_def_id,
302302
adjusted_ty,
303-
opt_input_type.as_ref().map(slice::from_ref),
303+
opt_input_type,
304304
) {
305305
let method = self.register_infer_ok_obligations(ok);
306306
let mut autoref = None;

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
336336
m_name: Ident,
337337
trait_def_id: DefId,
338338
self_ty: Ty<'tcx>,
339-
opt_input_types: Option<&[Ty<'tcx>]>,
339+
opt_rhs_ty: Option<Ty<'tcx>>,
340340
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
341341
// Construct a trait-reference `self_ty : Trait<input_tys>`
342342
let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind {
@@ -346,16 +346,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
346346
GenericParamDefKind::Type { .. } => {
347347
if param.index == 0 {
348348
self_ty.into()
349-
} else if let Some(input_types) = opt_input_types {
350-
input_types[param.index as usize - 1].into()
349+
} else if let Some(rhs_ty) = opt_rhs_ty {
350+
assert_eq!(param.index, 1, "did not expect >1 param on operator trait");
351+
rhs_ty.into()
351352
} else {
353+
// FIXME: We should stop passing `None` for the failure case
354+
// when probing for call exprs. I.e. `opt_rhs_ty` should always
355+
// be set when it needs to be.
352356
self.var_for_def(cause.span, param)
353357
}
354358
}
355359
});
356360

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);
361+
let obligation = traits::Obligation::new(
362+
self.tcx,
363+
cause,
364+
self.param_env,
365+
ty::TraitRef::new_from_args(self.tcx, trait_def_id, args),
366+
);
359367

360368
// Now we want to know if this can be matched
361369
if !self.predicate_may_hold(&obligation) {

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
895895

896896
let opname = Ident::with_dummy_span(opname);
897897
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
898-
let input_types = opt_rhs_ty.as_slice();
899898
let cause = self.cause(span, ObligationCauseCode::BinOp {
900899
lhs_hir_id: lhs_expr.hir_id,
901900
rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id),
@@ -904,13 +903,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
904903
output_ty: expected.only_has_type(self),
905904
});
906905

907-
let method = self.lookup_method_in_trait(
908-
cause.clone(),
909-
opname,
910-
trait_did,
911-
lhs_ty,
912-
Some(input_types),
913-
);
906+
let method =
907+
self.lookup_method_in_trait(cause.clone(), opname, trait_did, lhs_ty, opt_rhs_ty);
914908
match method {
915909
Some(ok) => {
916910
let method = self.register_infer_ok_obligations(ok);
@@ -942,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
942936
if param.index == 0 {
943937
lhs_ty.into()
944938
} else {
945-
input_types[param.index as usize - 1].into()
939+
opt_rhs_ty.expect("expected RHS for binop").into()
946940
}
947941
}
948942
});

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
149149
// If some lookup succeeded, install method in table
150150
let input_ty = self.next_ty_var(base_expr.span);
151151
let method =
152-
self.try_overloaded_place_op(expr.span, self_ty, &[input_ty], PlaceOp::Index);
152+
self.try_overloaded_place_op(expr.span, self_ty, Some(input_ty), PlaceOp::Index);
153153

154154
if let Some(result) = method {
155155
debug!("try_index_step: success, using overloaded indexing");
@@ -189,7 +189,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
189189
&self,
190190
span: Span,
191191
base_ty: Ty<'tcx>,
192-
arg_tys: &[Ty<'tcx>],
192+
opt_rhs_ty: Option<Ty<'tcx>>,
193193
op: PlaceOp,
194194
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
195195
debug!("try_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@@ -207,15 +207,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207207
Ident::with_dummy_span(imm_op),
208208
imm_tr,
209209
base_ty,
210-
Some(arg_tys),
210+
opt_rhs_ty,
211211
)
212212
}
213213

214214
fn try_mutable_overloaded_place_op(
215215
&self,
216216
span: Span,
217217
base_ty: Ty<'tcx>,
218-
arg_tys: &[Ty<'tcx>],
218+
opt_rhs_ty: Option<Ty<'tcx>>,
219219
op: PlaceOp,
220220
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
221221
debug!("try_mutable_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
@@ -233,7 +233,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
233233
Ident::with_dummy_span(mut_op),
234234
mut_tr,
235235
base_ty,
236-
Some(arg_tys),
236+
opt_rhs_ty,
237237
)
238238
}
239239

@@ -284,7 +284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
284284
&& let Some(ok) = self.try_mutable_overloaded_place_op(
285285
expr.span,
286286
source,
287-
&[],
287+
None,
288288
PlaceOp::Deref,
289289
)
290290
{
@@ -359,8 +359,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
359359
Some(self.typeck_results.borrow().node_args(expr.hir_id).type_at(1))
360360
}
361361
};
362-
let arg_tys = arg_ty.as_slice();
363-
let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_tys, op);
362+
let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_ty, op);
364363
let method = match method {
365364
Some(ok) => self.register_infer_ok_obligations(ok),
366365
// Couldn't find the mutable variant of the place op, keep the

0 commit comments

Comments
 (0)