Skip to content

Commit dbf0372

Browse files
authored
Rollup merge of #94484 - c410-f3r:more-let-chains, r=jackh726
8 - Make more use of `let_chains` Continuation of #94376. cc #53667
2 parents 850511d + 658ff94 commit dbf0372

File tree

10 files changed

+145
-185
lines changed

10 files changed

+145
-185
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+35-42
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
606606
// don't show type `_`
607607
err.span_label(span, format!("this expression has type `{}`", ty));
608608
}
609-
if let Some(ty::error::ExpectedFound { found, .. }) = exp_found {
610-
if ty.is_box() && ty.boxed_ty() == found {
611-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
612-
err.span_suggestion(
613-
span,
614-
"consider dereferencing the boxed value",
615-
format!("*{}", snippet),
616-
Applicability::MachineApplicable,
617-
);
618-
}
619-
}
609+
if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
610+
&& ty.is_box() && ty.boxed_ty() == found
611+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
612+
{
613+
err.span_suggestion(
614+
span,
615+
"consider dereferencing the boxed value",
616+
format!("*{}", snippet),
617+
Applicability::MachineApplicable,
618+
);
620619
}
621620
}
622621
ObligationCauseCode::Pattern { origin_expr: false, span: Some(span), .. } => {
@@ -1748,13 +1747,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17481747
self.check_and_note_conflicting_crates(diag, terr);
17491748
self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id());
17501749

1751-
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values {
1752-
if let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() {
1753-
if let Some(def_id) = def_id.as_local() {
1754-
let span = self.tcx.def_span(def_id);
1755-
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1756-
}
1757-
}
1750+
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
1751+
&& let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()
1752+
&& let Some(def_id) = def_id.as_local()
1753+
{
1754+
let span = self.tcx.def_span(def_id);
1755+
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
17581756
}
17591757

17601758
// It reads better to have the error origin as the final
@@ -2046,19 +2044,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20462044
// containing a single character, perhaps the user meant to write `'c'` to
20472045
// specify a character literal (issue #92479)
20482046
(ty::Char, ty::Ref(_, r, _)) if r.is_str() => {
2049-
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2050-
if let Some(code) =
2051-
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
2052-
{
2053-
if code.chars().count() == 1 {
2054-
err.span_suggestion(
2055-
span,
2056-
"if you meant to write a `char` literal, use single quotes",
2057-
format!("'{}'", code),
2058-
Applicability::MachineApplicable,
2059-
);
2060-
}
2061-
}
2047+
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
2048+
&& let Some(code) = code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
2049+
&& code.chars().count() == 1
2050+
{
2051+
err.span_suggestion(
2052+
span,
2053+
"if you meant to write a `char` literal, use single quotes",
2054+
format!("'{}'", code),
2055+
Applicability::MachineApplicable,
2056+
);
20622057
}
20632058
}
20642059
// If a string was expected and the found expression is a character literal,
@@ -2080,18 +2075,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20802075
_ => {}
20812076
}
20822077
}
2083-
if let MatchExpressionArm(box MatchExpressionArmCause { source, .. }) =
2084-
*trace.cause.code()
2078+
let code = trace.cause.code();
2079+
if let &MatchExpressionArm(box MatchExpressionArmCause { source, .. }) = code
2080+
&& let hir::MatchSource::TryDesugar = source
2081+
&& let Some((expected_ty, found_ty)) = self.values_str(trace.values)
20852082
{
2086-
if let hir::MatchSource::TryDesugar = source {
2087-
if let Some((expected_ty, found_ty)) = self.values_str(trace.values) {
2088-
err.note(&format!(
2089-
"`?` operator cannot convert from `{}` to `{}`",
2090-
found_ty.content(),
2091-
expected_ty.content(),
2092-
));
2093-
}
2094-
}
2083+
err.note(&format!(
2084+
"`?` operator cannot convert from `{}` to `{}`",
2085+
found_ty.content(),
2086+
expected_ty.content(),
2087+
));
20952088
}
20962089
err
20972090
}

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+41-48
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,25 @@ impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
114114
}
115115

116116
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
117-
if let ExprKind::Match(scrutinee, [_, arm], MatchSource::ForLoopDesugar) = expr.kind {
118-
if let Some(pat) = arm.pat.for_loop_some() {
119-
if let Some(ty) = self.node_ty_contains_target(pat.hir_id) {
120-
self.found_for_loop_iter = Some(scrutinee);
121-
self.found_node_ty = Some(ty);
122-
return;
123-
}
124-
}
117+
if let ExprKind::Match(scrutinee, [_, arm], MatchSource::ForLoopDesugar) = expr.kind
118+
&& let Some(pat) = arm.pat.for_loop_some()
119+
&& let Some(ty) = self.node_ty_contains_target(pat.hir_id)
120+
{
121+
self.found_for_loop_iter = Some(scrutinee);
122+
self.found_node_ty = Some(ty);
123+
return;
125124
}
126-
if let ExprKind::MethodCall(segment, exprs, _) = expr.kind {
127-
if segment.ident.span == self.target_span
128-
&& Some(self.target)
129-
== self.infcx.in_progress_typeck_results.and_then(|typeck_results| {
130-
typeck_results
131-
.borrow()
132-
.node_type_opt(exprs.first().unwrap().hir_id)
133-
.map(Into::into)
134-
})
135-
{
136-
self.found_exact_method_call = Some(&expr);
137-
return;
138-
}
125+
if let ExprKind::MethodCall(segment, exprs, _) = expr.kind
126+
&& segment.ident.span == self.target_span
127+
&& Some(self.target) == self.infcx.in_progress_typeck_results.and_then(|typeck_results| {
128+
typeck_results
129+
.borrow()
130+
.node_type_opt(exprs.first().unwrap().hir_id)
131+
.map(Into::into)
132+
})
133+
{
134+
self.found_exact_method_call = Some(&expr);
135+
return;
139136
}
140137

141138
// FIXME(const_generics): Currently, any uninferred `const` generics arguments
@@ -602,10 +599,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
602599
);
603600

604601
let use_diag = local_visitor.found_use_diagnostic.as_ref();
605-
if let Some(use_diag) = use_diag {
606-
if use_diag.applies_to(err_span) {
607-
use_diag.attach_note(&mut err);
608-
}
602+
if let Some(use_diag) = use_diag && use_diag.applies_to(err_span) {
603+
use_diag.attach_note(&mut err);
609604
}
610605

611606
let param_type = arg_data.kind.descr();
@@ -736,29 +731,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
736731
// | help: specify type like: `<Impl as Into<u32>>::into(foo_impl)`
737732
// |
738733
// = note: cannot satisfy `Impl: Into<_>`
739-
if !impl_candidates.is_empty() && e.span.contains(span) {
740-
if let Some(expr) = exprs.first() {
741-
if let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind {
742-
if let [path_segment] = path.segments {
743-
let candidate_len = impl_candidates.len();
744-
let suggestions = impl_candidates.iter().map(|candidate| {
745-
format!(
746-
"{}::{}({})",
747-
candidate, segment.ident, path_segment.ident
748-
)
749-
});
750-
err.span_suggestions(
751-
e.span,
752-
&format!(
753-
"use the fully qualified path for the potential candidate{}",
754-
pluralize!(candidate_len),
755-
),
756-
suggestions,
757-
Applicability::MaybeIncorrect,
758-
);
759-
}
760-
}
761-
};
734+
if !impl_candidates.is_empty() && e.span.contains(span)
735+
&& let Some(expr) = exprs.first()
736+
&& let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
737+
&& let [path_segment] = path.segments
738+
{
739+
let candidate_len = impl_candidates.len();
740+
let suggestions = impl_candidates.iter().map(|candidate| {
741+
format!(
742+
"{}::{}({})",
743+
candidate, segment.ident, path_segment.ident
744+
)
745+
});
746+
err.span_suggestions(
747+
e.span,
748+
&format!(
749+
"use the fully qualified path for the potential candidate{}",
750+
pluralize!(candidate_len),
751+
),
752+
suggestions,
753+
Applicability::MaybeIncorrect,
754+
);
762755
}
763756
// Suggest specifying type params or point out the return type of the call:
764757
//

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -223,35 +223,32 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
223223
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.def_id);
224224

225225
let mut override_error_code = None;
226-
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin {
227-
if let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code() {
228-
// Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
229-
// `'static` lifetime when called as a method on a binding: `bar.qux()`.
230-
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
231-
override_error_code = Some(ctxt.assoc_item.name);
232-
}
233-
}
226+
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin
227+
&& let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code()
228+
// Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
229+
// `'static` lifetime when called as a method on a binding: `bar.qux()`.
230+
&& self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt)
231+
{
232+
override_error_code = Some(ctxt.assoc_item.name);
234233
}
235-
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin {
236-
let code = match cause.code() {
234+
235+
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin
236+
&& let code = match cause.code() {
237237
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
238238
_ => cause.code(),
239-
};
240-
if let (ObligationCauseCode::ItemObligation(item_def_id), None) =
241-
(code, override_error_code)
239+
}
240+
&& let (ObligationCauseCode::ItemObligation(item_def_id), None) = (code, override_error_code)
241+
{
242+
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
243+
// lifetime as above, but called using a fully-qualified path to the method:
244+
// `Foo::qux(bar)`.
245+
let mut v = TraitObjectVisitor(FxHashSet::default());
246+
v.visit_ty(param.param_ty);
247+
if let Some((ident, self_ty)) =
248+
self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0)
249+
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
242250
{
243-
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
244-
// lifetime as above, but called using a fully-qualified path to the method:
245-
// `Foo::qux(bar)`.
246-
let mut v = TraitObjectVisitor(FxHashSet::default());
247-
v.visit_ty(param.param_ty);
248-
if let Some((ident, self_ty)) =
249-
self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0)
250-
{
251-
if self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty) {
252-
override_error_code = Some(ident.name);
253-
}
254-
}
251+
override_error_code = Some(ident.name);
255252
}
256253
}
257254
if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+26-38
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,39 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2323
let error = self.error.as_ref()?;
2424
debug!("try_report_impl_not_conforming_to_trait {:?}", error);
2525
if let RegionResolutionError::SubSupConflict(
26-
_,
27-
var_origin,
28-
sub_origin,
29-
_sub,
30-
sup_origin,
31-
_sup,
32-
_,
33-
) = error.clone()
26+
_, var_origin, sub_origin, _sub, sup_origin, _sup, _,
27+
) = error.clone()
28+
&& let (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) = (&sup_origin, &sub_origin)
29+
&& let (
30+
sub_expected_found @ Some((sub_expected, sub_found)),
31+
sup_expected_found @ Some(_),
32+
CompareImplMethodObligation { trait_item_def_id, .. },
33+
) = (sub_trace.values.ty(), sup_trace.values.ty(), sub_trace.cause.code())
34+
&& sup_expected_found == sub_expected_found
3435
{
35-
if let (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) = (&sup_origin, &sub_origin) {
36-
if let (
37-
sub_expected_found @ Some((sub_expected, sub_found)),
38-
sup_expected_found @ Some(_),
39-
CompareImplMethodObligation { trait_item_def_id, .. },
40-
) = (&sub_trace.values.ty(), &sup_trace.values.ty(), sub_trace.cause.code())
41-
{
42-
if sup_expected_found == sub_expected_found {
43-
self.emit_err(
44-
var_origin.span(),
45-
*sub_expected,
46-
*sub_found,
47-
*trait_item_def_id,
48-
);
49-
return Some(ErrorGuaranteed);
50-
}
51-
}
52-
}
36+
self.emit_err(
37+
var_origin.span(),
38+
sub_expected,
39+
sub_found,
40+
*trait_item_def_id,
41+
);
42+
return Some(ErrorGuaranteed);
5343
}
5444
if let RegionResolutionError::ConcreteFailure(origin, _, _)
55-
| RegionResolutionError::GenericBoundFailure(origin, _, _) = error.clone()
56-
{
57-
if let SubregionOrigin::CompareImplTypeObligation {
45+
| RegionResolutionError::GenericBoundFailure(origin, _, _) = error.clone()
46+
&& let SubregionOrigin::CompareImplTypeObligation {
5847
span,
5948
impl_item_def_id,
6049
trait_item_def_id,
6150
} = origin
62-
{
63-
self.emit_associated_type_err(
64-
span,
65-
self.infcx.tcx.item_name(impl_item_def_id),
66-
impl_item_def_id,
67-
trait_item_def_id,
68-
);
69-
return Some(ErrorGuaranteed);
70-
}
51+
{
52+
self.emit_associated_type_err(
53+
span,
54+
self.infcx.tcx.item_name(impl_item_def_id),
55+
impl_item_def_id,
56+
trait_item_def_id,
57+
);
58+
return Some(ErrorGuaranteed);
7159
}
7260
None
7361
}

compiler/rustc_infer/src/infer/fudge.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
220220
}
221221

222222
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
223-
if let ty::ReVar(vid) = *r {
224-
if self.region_vars.0.contains(&vid) {
225-
let idx = vid.index() - self.region_vars.0.start.index();
226-
let origin = self.region_vars.1[idx];
227-
return self.infcx.next_region_var(origin);
228-
}
223+
if let ty::ReVar(vid) = *r && self.region_vars.0.contains(&vid) {
224+
let idx = vid.index() - self.region_vars.0.start.index();
225+
let origin = self.region_vars.1[idx];
226+
return self.infcx.next_region_var(origin);
229227
}
230228
r
231229
}

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
304304
// check below for a common case, here purely as an
305305
// optimization.
306306
let b_universe = self.var_infos[b_vid].universe;
307-
if let ReEmpty(a_universe) = *a_region {
308-
if a_universe == b_universe {
309-
return false;
310-
}
307+
if let ReEmpty(a_universe) = *a_region && a_universe == b_universe {
308+
return false;
311309
}
312310

313311
let mut lub = self.lub_concrete_regions(a_region, cur_region);
@@ -324,10 +322,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
324322
// tighter bound than `'static`.
325323
//
326324
// (This might e.g. arise from being asked to prove `for<'a> { 'b: 'a }`.)
327-
if let ty::RePlaceholder(p) = *lub {
328-
if b_universe.cannot_name(p.universe) {
329-
lub = self.tcx().lifetimes.re_static;
330-
}
325+
if let ty::RePlaceholder(p) = *lub && b_universe.cannot_name(p.universe) {
326+
lub = self.tcx().lifetimes.re_static;
331327
}
332328

333329
debug!("Expanding value of {:?} from {:?} to {:?}", b_vid, cur_region, lub);

0 commit comments

Comments
 (0)