Skip to content

Commit dab5c44

Browse files
committed
rustc_trait_selection: adopt let else in more places
1 parent b8c56fa commit dab5c44

File tree

10 files changed

+75
-117
lines changed

10 files changed

+75
-117
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/auto_trait.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,16 @@ impl<'tcx> AutoTraitFinder<'tcx> {
148148
// traits::project will see that 'T: SomeTrait' is in our ParamEnv, allowing
149149
// SelectionContext to return it back to us.
150150

151-
let (new_env, user_env) = match self.evaluate_predicates(
151+
let Some((new_env, user_env)) = self.evaluate_predicates(
152152
&infcx,
153153
trait_did,
154154
ty,
155155
orig_env,
156156
orig_env,
157157
&mut fresh_preds,
158158
false,
159-
) {
160-
Some(e) => e,
161-
None => return AutoTraitResult::NegativeImpl,
159+
) else {
160+
return AutoTraitResult::NegativeImpl;
162161
};
163162

164163
let (full_env, full_user_env) = self

Diff for: compiler/rustc_trait_selection/src/traits/coherence.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,15 @@ fn negative_impl<'cx, 'tcx>(
335335
impl_trait_ref_and_oblig(selcx, impl1_env, impl2_def_id, impl2_substs);
336336

337337
// do the impls unify? If not, not disjoint.
338-
let more_obligations = match infcx
338+
let Ok(InferOk { obligations: more_obligations, .. }) = infcx
339339
.at(&ObligationCause::dummy(), impl1_env)
340340
.eq(impl1_trait_ref, impl2_trait_ref)
341-
{
342-
Ok(InferOk { obligations, .. }) => obligations,
343-
Err(_) => {
344-
debug!(
345-
"explicit_disjoint: {:?} does not unify with {:?}",
346-
impl1_trait_ref, impl2_trait_ref
347-
);
348-
return false;
349-
}
341+
else {
342+
debug!(
343+
"explicit_disjoint: {:?} does not unify with {:?}",
344+
impl1_trait_ref, impl2_trait_ref
345+
);
346+
return false;
350347
};
351348

352349
let opt_failing_obligation = obligations

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
801801
return;
802802
}
803803

804-
let found_trait_ty = match found_trait_ref.self_ty().no_bound_vars() {
805-
Some(ty) => ty,
806-
None => return,
804+
let Some(found_trait_ty) = found_trait_ref.self_ty().no_bound_vars() else {
805+
return;
807806
};
808807

809808
let found_did = match *found_trait_ty.kind() {
@@ -2115,26 +2114,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21152114
err: &mut DiagnosticBuilder<'tcx>,
21162115
obligation: &PredicateObligation<'tcx>,
21172116
) {
2118-
let (pred, item_def_id, span) = match (
2117+
let (
2118+
ty::PredicateKind::Trait(pred),
2119+
&ObligationCauseCode::BindingObligation(item_def_id, span),
2120+
) = (
21192121
obligation.predicate.kind().skip_binder(),
21202122
obligation.cause.code().peel_derives(),
2121-
) {
2122-
(
2123-
ty::PredicateKind::Trait(pred),
2124-
&ObligationCauseCode::BindingObligation(item_def_id, span),
2125-
) => (pred, item_def_id, span),
2126-
_ => return,
2123+
) else {
2124+
return;
21272125
};
21282126
debug!(
21292127
"suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}",
21302128
pred, item_def_id, span
21312129
);
2132-
let node = match (
2130+
let (Some(node), true) = (
21332131
self.tcx.hir().get_if_local(item_def_id),
21342132
Some(pred.def_id()) == self.tcx.lang_items().sized_trait(),
2135-
) {
2136-
(Some(node), true) => node,
2137-
_ => return,
2133+
) else {
2134+
return;
21382135
};
21392136
self.maybe_suggest_unsized_generics(err, span, node);
21402137
}
@@ -2145,9 +2142,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21452142
span: Span,
21462143
node: Node<'hir>,
21472144
) {
2148-
let generics = match node.generics() {
2149-
Some(generics) => generics,
2150-
None => return,
2145+
let Some(generics) = node.generics() else {
2146+
return;
21512147
};
21522148
let sized_trait = self.tcx.lang_items().sized_trait();
21532149
debug!("maybe_suggest_unsized_generics: generics.params={:?}", generics.params);
@@ -2160,9 +2156,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21602156
.iter()
21612157
.all(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) != sized_trait)
21622158
});
2163-
let param = match param {
2164-
Some(param) => param,
2165-
_ => return,
2159+
let Some(param) = param else {
2160+
return;
21662161
};
21672162
let param_def_id = self.tcx.hir().local_def_id(param.hir_id).to_def_id();
21682163
let preds = generics.where_clause.predicates.iter();

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
513513
| ObligationCauseCode::BuiltinDerivedObligation(cause) => cause.parent_trait_pred,
514514
_ => trait_pred,
515515
};
516-
let real_ty = match real_trait_pred.self_ty().no_bound_vars() {
517-
Some(ty) => ty,
518-
None => return,
516+
let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else {
517+
return;
519518
};
520519

521520
if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() {
@@ -593,9 +592,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
593592
err: &mut DiagnosticBuilder<'_>,
594593
trait_pred: ty::PolyTraitPredicate<'tcx>,
595594
) {
596-
let self_ty = match trait_pred.self_ty().no_bound_vars() {
597-
None => return,
598-
Some(ty) => ty,
595+
let Some(self_ty) = trait_pred.self_ty().no_bound_vars() else {
596+
return;
599597
};
600598

601599
let (def_id, output_ty, callable) = match *self_ty.kind() {
@@ -607,9 +605,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
607605

608606
// `mk_trait_obligation_with_new_self_ty` only works for types with no escaping bound
609607
// variables, so bail out if we have any.
610-
let output_ty = match output_ty.no_bound_vars() {
611-
Some(ty) => ty,
612-
None => return,
608+
let Some(output_ty) = output_ty.no_bound_vars() else {
609+
return;
613610
};
614611

615612
let new_obligation =
@@ -631,9 +628,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
631628
..
632629
})) => {
633630
err.span_label(*span, "consider calling this closure");
634-
let name = match self.get_closure_name(def_id, err, &msg) {
635-
Some(name) => name,
636-
None => return,
631+
let Some(name) = self.get_closure_name(def_id, err, &msg) else {
632+
return;
637633
};
638634
let args = decl.inputs.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
639635
let sugg = format!("({})", args);
@@ -830,9 +826,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
830826
return;
831827
}
832828

833-
let mut suggested_ty = match trait_pred.self_ty().no_bound_vars() {
834-
Some(ty) => ty,
835-
None => return,
829+
let Some(mut suggested_ty) = trait_pred.self_ty().no_bound_vars() else {
830+
return;
836831
};
837832

838833
for refs_remaining in 0..refs_number {
@@ -1050,9 +1045,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10501045
fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
10511046
let hir = self.tcx.hir();
10521047
let parent_node = hir.get_parent_node(obligation.cause.body_id);
1053-
let sig = match hir.find(parent_node) {
1054-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) => sig,
1055-
_ => return None,
1048+
let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) = hir.find(parent_node) else {
1049+
return None;
10561050
};
10571051

10581052
if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
@@ -1502,11 +1496,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15021496

15031497
// Only continue if a generator was found.
15041498
debug!(?generator, ?trait_ref, ?target_ty, "maybe_note_obligation_cause_for_async_await");
1505-
let (generator_did, trait_ref, target_ty) = match (generator, trait_ref, target_ty) {
1506-
(Some(generator_did), Some(trait_ref), Some(target_ty)) => {
1507-
(generator_did, trait_ref, target_ty)
1508-
}
1509-
_ => return false,
1499+
let (Some(generator_did), Some(trait_ref), Some(target_ty)) = (generator, trait_ref, target_ty) else {
1500+
return false;
15101501
};
15111502

15121503
let span = self.tcx.def_span(generator_did);

Diff for: compiler/rustc_trait_selection/src/traits/mod.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,16 @@ pub fn normalize_param_env_or_error<'tcx>(
340340
"normalize_param_env_or_error: predicates=(non-outlives={:?}, outlives={:?})",
341341
predicates, outlives_predicates
342342
);
343-
let non_outlives_predicates = match do_normalize_predicates(
343+
let Ok(non_outlives_predicates) = do_normalize_predicates(
344344
tcx,
345345
region_context,
346346
cause.clone(),
347347
elaborated_env,
348348
predicates,
349-
) {
350-
Ok(predicates) => predicates,
349+
) else {
351350
// An unnormalized env is better than nothing.
352-
Err(ErrorReported) => {
353-
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
354-
return elaborated_env;
355-
}
351+
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
352+
return elaborated_env;
356353
};
357354

358355
debug!("normalize_param_env_or_error: non-outlives predicates={:?}", non_outlives_predicates);
@@ -367,19 +364,16 @@ pub fn normalize_param_env_or_error<'tcx>(
367364
unnormalized_env.reveal(),
368365
unnormalized_env.constness(),
369366
);
370-
let outlives_predicates = match do_normalize_predicates(
367+
let Ok(outlives_predicates) = do_normalize_predicates(
371368
tcx,
372369
region_context,
373370
cause,
374371
outlives_env,
375372
outlives_predicates,
376-
) {
377-
Ok(predicates) => predicates,
373+
) else {
378374
// An unnormalized env is better than nothing.
379-
Err(ErrorReported) => {
380-
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
381-
return elaborated_env;
382-
}
375+
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
376+
return elaborated_env;
383377
};
384378
debug!("normalize_param_env_or_error: outlives predicates={:?}", outlives_predicates);
385379

@@ -834,9 +828,8 @@ pub fn vtable_trait_upcasting_coercion_new_vptr_slot<'tcx>(
834828
selcx.select(&obligation).unwrap()
835829
});
836830

837-
let implsrc_traitcasting = match implsrc {
838-
Some(ImplSource::TraitUpcasting(data)) => data,
839-
_ => bug!(),
831+
let Some(ImplSource::TraitUpcasting(implsrc_traitcasting)) = implsrc else {
832+
bug!();
840833
};
841834

842835
implsrc_traitcasting.vtable_vptr_slot

Diff for: compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,8 @@ fn trait_has_sized_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
322322
}
323323

324324
fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
325-
let sized_def_id = match tcx.lang_items().sized_trait() {
326-
Some(def_id) => def_id,
327-
None => {
328-
return false; /* No Sized trait, can't require it! */
329-
}
325+
let Some(sized_def_id) = tcx.lang_items().sized_trait() else {
326+
return false; /* No Sized trait, can't require it! */
330327
};
331328

332329
// Search for a predicate like `Self : Sized` amongst the trait bounds.

Diff for: compiler/rustc_trait_selection/src/traits/project.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1832,9 +1832,8 @@ fn confirm_impl_candidate<'cx, 'tcx>(
18321832
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
18331833

18341834
let param_env = obligation.param_env;
1835-
let assoc_ty = match assoc_def(selcx, impl_def_id, assoc_item_id) {
1836-
Ok(assoc_ty) => assoc_ty,
1837-
Err(ErrorReported) => return Progress { term: tcx.ty_error().into(), obligations: nested },
1835+
let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1836+
return Progress { term: tcx.ty_error().into(), obligations: nested };
18381837
};
18391838

18401839
if !assoc_ty.item.defaultness.has_value() {

Diff for: compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
436436
obligation: &TraitObligation<'tcx>,
437437
candidates: &mut SelectionCandidateSet<'tcx>,
438438
) {
439-
let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) {
440-
Some(k) => k,
441-
None => {
442-
return;
443-
}
439+
let Some(kind) = self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) else {
440+
return;
444441
};
445442

446443
// Okay to skip binder because the substs on closure types never
@@ -763,12 +760,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
763760
// T: Trait
764761
// so it seems ok if we (conservatively) fail to accept that `Unsize`
765762
// obligation above. Should be possible to extend this in the future.
766-
let source = match obligation.self_ty().no_bound_vars() {
767-
Some(t) => t,
768-
None => {
769-
// Don't add any candidates if there are bound regions.
770-
return;
771-
}
763+
let Some(source) = obligation.self_ty().no_bound_vars() else {
764+
// Don't add any candidates if there are bound regions.
765+
return;
772766
};
773767
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
774768

Diff for: compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
272272
} else {
273273
bug!("unexpected builtin trait {:?}", trait_def)
274274
};
275-
let nested = match conditions {
276-
BuiltinImplConditions::Where(nested) => nested,
277-
_ => bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation),
275+
let BuiltinImplConditions::Where(nested) = conditions else {
276+
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
278277
};
279278

280279
let cause = obligation.derived_cause(BuiltinDerivedObligation);
@@ -421,9 +420,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
421420
let trait_predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate);
422421
let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
423422
let obligation_trait_ref = ty::Binder::dummy(trait_predicate.trait_ref);
424-
let data = match *self_ty.kind() {
425-
ty::Dynamic(data, ..) => data,
426-
_ => span_bug!(obligation.cause.span, "object candidate with non-object"),
423+
let ty::Dynamic(data, ..) = *self_ty.kind() else {
424+
span_bug!(obligation.cause.span, "object candidate with non-object");
427425
};
428426

429427
let object_trait_ref = data.principal().unwrap_or_else(|| {
@@ -608,9 +606,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
608606
// touch bound regions, they just capture the in-scope
609607
// type/region parameters.
610608
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
611-
let (generator_def_id, substs) = match *self_ty.kind() {
612-
ty::Generator(id, substs, _) => (id, substs),
613-
_ => bug!("closure candidate for non-closure {:?}", obligation),
609+
let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
610+
bug!("closure candidate for non-closure {:?}", obligation);
614611
};
615612

616613
debug!(?obligation, ?generator_def_id, ?substs, "confirm_generator_candidate");
@@ -652,9 +649,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
652649
// touch bound regions, they just capture the in-scope
653650
// type/region parameters.
654651
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
655-
let (closure_def_id, substs) = match *self_ty.kind() {
656-
ty::Closure(id, substs) => (id, substs),
657-
_ => bug!("closure candidate for non-closure {:?}", obligation),
652+
let ty::Closure(closure_def_id, substs) = *self_ty.kind() else {
653+
bug!("closure candidate for non-closure {:?}", obligation);
658654
};
659655

660656
let obligation_predicate = obligation.predicate;

Diff for: compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,15 @@ fn fulfill_implication<'a, 'tcx>(
192192
impl_trait_ref_and_oblig(selcx, param_env, target_impl, target_substs);
193193

194194
// do the impls unify? If not, no specialization.
195-
let more_obligations =
196-
match infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref)
197-
{
198-
Ok(InferOk { obligations, .. }) => obligations,
199-
Err(_) => {
200-
debug!(
201-
"fulfill_implication: {:?} does not unify with {:?}",
202-
source_trait_ref, target_trait_ref
203-
);
204-
return Err(());
205-
}
206-
};
195+
let Ok(InferOk { obligations: more_obligations, .. }) =
196+
infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref)
197+
else {
198+
debug!(
199+
"fulfill_implication: {:?} does not unify with {:?}",
200+
source_trait_ref, target_trait_ref
201+
);
202+
return Err(());
203+
};
207204

208205
// attempt to prove all of the predicates for impl2 given those for impl1
209206
// (which are packed up in penv)

0 commit comments

Comments
 (0)