Skip to content

Commit 3d80dd9

Browse files
Clean up some builtin operator typeck logic
1 parent 1c42cb4 commit 3d80dd9

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,9 @@ fn fatally_break_rust(sess: &Session) {
461461
));
462462
}
463463

464-
fn has_expected_num_generic_args(
465-
tcx: TyCtxt<'_>,
466-
trait_did: Option<DefId>,
467-
expected: usize,
468-
) -> bool {
469-
trait_did.map_or(true, |trait_did| {
470-
let generics = tcx.generics_of(trait_did);
471-
generics.count() == expected + if generics.has_self { 1 } else { 0 }
472-
})
464+
fn has_expected_num_generic_args(tcx: TyCtxt<'_>, trait_did: DefId, expected: usize) -> bool {
465+
let generics = tcx.generics_of(trait_did);
466+
generics.count() == expected + if generics.has_self { 1 } else { 0 }
473467
}
474468

475469
pub fn provide(providers: &mut Providers) {

compiler/rustc_hir_typeck/src/op.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
719719
Op::Binary(op, _) => op.span,
720720
Op::Unary(_, span) => span,
721721
};
722-
let (opname, trait_did) = lang_item_for_op(self.tcx, op, span);
722+
let (opname, Some(trait_did)) = lang_item_for_op(self.tcx, op, span) else {
723+
// Bail if the operator trait is not defined.
724+
return Err(vec![]);
725+
};
723726

724727
debug!(
725728
"lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})",
@@ -759,18 +762,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
759762
},
760763
);
761764

762-
let method = trait_did.and_then(|trait_did| {
763-
self.lookup_method_in_trait(cause.clone(), opname, trait_did, lhs_ty, Some(input_types))
764-
});
765-
766-
match (method, trait_did) {
767-
(Some(ok), _) => {
765+
let method = self.lookup_method_in_trait(
766+
cause.clone(),
767+
opname,
768+
trait_did,
769+
lhs_ty,
770+
Some(input_types),
771+
);
772+
match method {
773+
Some(ok) => {
768774
let method = self.register_infer_ok_obligations(ok);
769775
self.select_obligations_where_possible(|_| {});
770776
Ok(method)
771777
}
772-
(None, None) => Err(vec![]),
773-
(None, Some(trait_did)) => {
778+
None => {
774779
let (obligation, _) =
775780
self.obligation_for_method(cause, trait_did, lhs_ty, Some(input_types));
776781
// FIXME: This should potentially just add the obligation to the `FnCtxt`

compiler/rustc_hir_typeck/src/place_op.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200200
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
201201
debug!("try_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
202202

203-
let (imm_tr, imm_op) = match op {
203+
let (Some(imm_tr), imm_op) = (match op {
204204
PlaceOp::Deref => (self.tcx.lang_items().deref_trait(), sym::deref),
205205
PlaceOp::Index => (self.tcx.lang_items().index_trait(), sym::index),
206+
}) else {
207+
// Bail if `Deref` or `Index` isn't defined.
208+
return None;
206209
};
207210

208211
// If the lang item was declared incorrectly, stop here so that we don't
@@ -219,15 +222,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
219222
return None;
220223
}
221224

222-
imm_tr.and_then(|trait_did| {
223-
self.lookup_method_in_trait(
224-
self.misc(span),
225-
Ident::with_dummy_span(imm_op),
226-
trait_did,
227-
base_ty,
228-
Some(arg_tys),
229-
)
230-
})
225+
self.lookup_method_in_trait(
226+
self.misc(span),
227+
Ident::with_dummy_span(imm_op),
228+
imm_tr,
229+
base_ty,
230+
Some(arg_tys),
231+
)
231232
}
232233

233234
fn try_mutable_overloaded_place_op(
@@ -239,9 +240,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
239240
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
240241
debug!("try_mutable_overloaded_place_op({:?},{:?},{:?})", span, base_ty, op);
241242

242-
let (mut_tr, mut_op) = match op {
243+
let (Some(mut_tr), mut_op) = (match op {
243244
PlaceOp::Deref => (self.tcx.lang_items().deref_mut_trait(), sym::deref_mut),
244245
PlaceOp::Index => (self.tcx.lang_items().index_mut_trait(), sym::index_mut),
246+
}) else {
247+
// Bail if `DerefMut` or `IndexMut` isn't defined.
248+
return None;
245249
};
246250

247251
// If the lang item was declared incorrectly, stop here so that we don't
@@ -258,15 +262,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
258262
return None;
259263
}
260264

261-
mut_tr.and_then(|trait_did| {
262-
self.lookup_method_in_trait(
263-
self.misc(span),
264-
Ident::with_dummy_span(mut_op),
265-
trait_did,
266-
base_ty,
267-
Some(arg_tys),
268-
)
269-
})
265+
self.lookup_method_in_trait(
266+
self.misc(span),
267+
Ident::with_dummy_span(mut_op),
268+
mut_tr,
269+
base_ty,
270+
Some(arg_tys),
271+
)
270272
}
271273

272274
/// Convert auto-derefs, indices, etc of an expression from `Deref` and `Index`

0 commit comments

Comments
 (0)