Skip to content

Commit 41706ff

Browse files
authored
Rollup merge of rust-lang#54641 - ljedrz:cleanup_rustc_infer, r=estebank
A few cleanups and minor improvements to rustc/infer - use unwrap_or(_else) where applicable - convert single-branch matches to if-let - use to_owned instead of to_string with string literals - improve vector allocations - readability improvements - miscellaneous minor code improvements
2 parents 5bfd085 + 52da886 commit 41706ff

File tree

14 files changed

+170
-200
lines changed

14 files changed

+170
-200
lines changed

Diff for: src/librustc/infer/canonical/query_result.rs

+56-64
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
135135
);
136136

137137
// Select everything, returning errors.
138-
let true_errors = match fulfill_cx.select_where_possible(self) {
139-
Ok(()) => vec![],
140-
Err(errors) => errors,
141-
};
138+
let true_errors = fulfill_cx.select_where_possible(self).err().unwrap_or_else(Vec::new);
142139
debug!("true_errors = {:#?}", true_errors);
143140

144141
if !true_errors.is_empty() {
@@ -148,10 +145,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
148145
}
149146

150147
// Anything left unselected *now* must be an ambiguity.
151-
let ambig_errors = match fulfill_cx.select_all_or_error(self) {
152-
Ok(()) => vec![],
153-
Err(errors) => errors,
154-
};
148+
let ambig_errors = fulfill_cx.select_all_or_error(self).err().unwrap_or_else(Vec::new);
155149
debug!("ambig_errors = {:#?}", ambig_errors);
156150

157151
let region_obligations = self.take_registered_region_obligations();
@@ -316,16 +310,18 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
316310
}
317311

318312
// ...also include the other query region constraints from the query.
319-
output_query_region_constraints.reserve(query_result.value.region_constraints.len());
320-
for r_c in query_result.value.region_constraints.iter() {
321-
let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder(); // reconstructed below
322-
let k1 = substitute_value(self.tcx, &result_subst, &k1);
323-
let r2 = substitute_value(self.tcx, &result_subst, &r2);
324-
if k1 != r2.into() {
325-
output_query_region_constraints
326-
.push(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)));
327-
}
328-
}
313+
output_query_region_constraints.extend(
314+
query_result.value.region_constraints.iter().filter_map(|r_c| {
315+
let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder(); // reconstructed below
316+
let k1 = substitute_value(self.tcx, &result_subst, &k1);
317+
let r2 = substitute_value(self.tcx, &result_subst, &r2);
318+
if k1 != r2.into() {
319+
Some(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)))
320+
} else {
321+
None
322+
}
323+
})
324+
);
329325

330326
let user_result: R =
331327
query_result.substitute_projected(self.tcx, &result_subst, |q_r| &q_r.value);
@@ -448,10 +444,9 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
448444
.variables
449445
.iter()
450446
.enumerate()
451-
.map(|(index, info)| match opt_values[CanonicalVar::new(index)] {
452-
Some(k) => k,
453-
None => self.fresh_inference_var_for_canonical_var(cause.span, *info),
454-
})
447+
.map(|(index, info)| opt_values[CanonicalVar::new(index)].unwrap_or_else(||
448+
self.fresh_inference_var_for_canonical_var(cause.span, *info)
449+
))
455450
.collect(),
456451
};
457452

@@ -504,24 +499,22 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
504499
let ty::OutlivesPredicate(k1, r2) = constraint.skip_binder(); // restored below
505500
let k1 = substitute_value(self.tcx, result_subst, k1);
506501
let r2 = substitute_value(self.tcx, result_subst, r2);
507-
match k1.unpack() {
508-
UnpackedKind::Lifetime(r1) => Obligation::new(
509-
cause.clone(),
510-
param_env,
511-
ty::Predicate::RegionOutlives(ty::Binder::dummy(
512-
ty::OutlivesPredicate(r1, r2),
502+
503+
Obligation::new(
504+
cause.clone(),
505+
param_env,
506+
match k1.unpack() {
507+
UnpackedKind::Lifetime(r1) => ty::Predicate::RegionOutlives(
508+
ty::Binder::dummy(
509+
ty::OutlivesPredicate(r1, r2)
513510
)),
514-
),
515-
516-
UnpackedKind::Type(t1) => Obligation::new(
517-
cause.clone(),
518-
param_env,
519-
ty::Predicate::TypeOutlives(ty::Binder::dummy(ty::OutlivesPredicate(
520-
t1, r2,
521-
))),
522-
),
523-
}
524-
}),
511+
UnpackedKind::Type(t1) => ty::Predicate::TypeOutlives(
512+
ty::Binder::dummy(ty::OutlivesPredicate(
513+
t1, r2
514+
)))
515+
}
516+
)
517+
})
525518
) as Box<dyn Iterator<Item = _>>
526519
}
527520

@@ -583,31 +576,30 @@ pub fn make_query_outlives<'tcx>(
583576
assert!(verifys.is_empty());
584577
assert!(givens.is_empty());
585578

586-
let mut outlives: Vec<_> = constraints
587-
.into_iter()
588-
.map(|(k, _)| match *k {
589-
// Swap regions because we are going from sub (<=) to outlives
590-
// (>=).
591-
Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
592-
tcx.mk_region(ty::ReVar(v2)).into(),
593-
tcx.mk_region(ty::ReVar(v1)),
594-
),
595-
Constraint::VarSubReg(v1, r2) => {
596-
ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
597-
}
598-
Constraint::RegSubVar(r1, v2) => {
599-
ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
600-
}
601-
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
602-
})
603-
.map(ty::Binder::dummy) // no bound regions in the code above
604-
.collect();
605-
606-
outlives.extend(
607-
outlives_obligations
608-
.map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
609-
.map(ty::Binder::dummy), // no bound regions in the code above
610-
);
579+
let outlives: Vec<_> = constraints
580+
.into_iter()
581+
.map(|(k, _)| match *k {
582+
// Swap regions because we are going from sub (<=) to outlives
583+
// (>=).
584+
Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
585+
tcx.mk_region(ty::ReVar(v2)).into(),
586+
tcx.mk_region(ty::ReVar(v1)),
587+
),
588+
Constraint::VarSubReg(v1, r2) => {
589+
ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
590+
}
591+
Constraint::RegSubVar(r1, v2) => {
592+
ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
593+
}
594+
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
595+
})
596+
.map(ty::Binder::dummy) // no bound regions in the code above
597+
.chain(
598+
outlives_obligations
599+
.map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
600+
.map(ty::Binder::dummy), // no bound regions in the code above
601+
)
602+
.collect();
611603

612604
outlives
613605
}

Diff for: src/librustc/infer/equate.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,22 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
7777
match (&a.sty, &b.sty) {
7878
(&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
7979
infcx.type_variables.borrow_mut().equate(a_id, b_id);
80-
Ok(a)
8180
}
8281

8382
(&ty::Infer(TyVar(a_id)), _) => {
8483
self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
85-
Ok(a)
8684
}
8785

8886
(_, &ty::Infer(TyVar(b_id))) => {
8987
self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
90-
Ok(a)
9188
}
9289

9390
_ => {
9491
self.fields.infcx.super_combine_tys(self, a, b)?;
95-
Ok(a)
9692
}
9793
}
94+
95+
Ok(a)
9896
}
9997

10098
fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)

Diff for: src/librustc/infer/error_reporting/mod.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
406406
errors.clone()
407407
} else {
408408
errors
409-
.iter()
410-
.filter(|&e| !is_bound_failure(e))
411-
.cloned()
412-
.collect()
409+
.iter()
410+
.filter(|&e| !is_bound_failure(e))
411+
.cloned()
412+
.collect()
413413
};
414414

415415
// sort the errors by span, for better error message stability.
@@ -455,11 +455,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
455455
TypeError::Sorts(ref exp_found) => {
456456
// if they are both "path types", there's a chance of ambiguity
457457
// due to different versions of the same crate
458-
match (&exp_found.expected.sty, &exp_found.found.sty) {
459-
(&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _)) => {
460-
report_path_match(err, exp_adt.did, found_adt.did);
461-
}
462-
_ => (),
458+
if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _))
459+
= (&exp_found.expected.sty, &exp_found.found.sty)
460+
{
461+
report_path_match(err, exp_adt.did, found_adt.did);
463462
}
464463
}
465464
TypeError::Traits(ref exp_found) => {

Diff for: src/librustc/infer/error_reporting/need_type_info.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
100100
let mut labels = vec![(
101101
span,
102102
if &name == "_" {
103-
"cannot infer type".to_string()
103+
"cannot infer type".to_owned()
104104
} else {
105105
format!("cannot infer type for `{}`", name)
106106
},
@@ -138,20 +138,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
138138
// ```
139139
labels.clear();
140140
labels.push(
141-
(pattern.span, "consider giving this closure parameter a type".to_string()));
141+
(pattern.span, "consider giving this closure parameter a type".to_owned()));
142142
} else if let Some(pattern) = local_visitor.found_local_pattern {
143143
if let Some(simple_ident) = pattern.simple_ident() {
144144
match pattern.span.compiler_desugaring_kind() {
145145
None => labels.push((pattern.span,
146146
format!("consider giving `{}` a type", simple_ident))),
147147
Some(CompilerDesugaringKind::ForLoop) => labels.push((
148148
pattern.span,
149-
"the element type for this iterator is not specified".to_string(),
149+
"the element type for this iterator is not specified".to_owned(),
150150
)),
151151
_ => {}
152152
}
153153
} else {
154-
labels.push((pattern.span, "consider giving the pattern a type".to_string()));
154+
labels.push((pattern.span, "consider giving the pattern a type".to_owned()));
155155
}
156156
}
157157

Diff for: src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
113113
(None, None) => {
114114
let (main_label_1, span_label_1) = if ty_sup.id == ty_sub.id {
115115
(
116-
"this type is declared with multiple lifetimes...".to_string(),
117-
"...but data with one lifetime flows into the other here".to_string()
116+
"this type is declared with multiple lifetimes...".to_owned(),
117+
"...but data with one lifetime flows into the other here".to_owned()
118118
)
119119
} else {
120120
(
121-
"these two types are declared with different lifetimes...".to_string(),
121+
"these two types are declared with different lifetimes...".to_owned(),
122122
format!(
123123
"...but data{} flows{} here",
124124
span_label_var1,
@@ -133,15 +133,15 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
133133
ty_sub.span,
134134
ret_span,
135135
"this parameter and the return type are declared \
136-
with different lifetimes...".to_string()
136+
with different lifetimes...".to_owned()
137137
,
138138
format!("...but data{} is returned here", span_label_var1),
139139
),
140140
(_, Some(ret_span)) => (
141141
ty_sup.span,
142142
ret_span,
143143
"this parameter and the return type are declared \
144-
with different lifetimes...".to_string()
144+
with different lifetimes...".to_owned()
145145
,
146146
format!("...but data{} is returned here", span_label_var1),
147147
),

Diff for: src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,17 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5858
&RegionKind::ReFree(ref free_region)) = (&sub_origin, sup_region) {
5959
let hir = &self.tcx.hir;
6060
if let Some(node_id) = hir.as_local_node_id(free_region.scope) {
61-
match hir.get(node_id) {
62-
Node::Expr(Expr {
63-
node: Closure(_, _, _, closure_span, None),
64-
..
65-
}) => {
66-
let sup_sp = sup_origin.span();
67-
let origin_sp = origin.span();
68-
let mut err = self.tcx.sess.struct_span_err(
69-
sup_sp,
70-
"borrowed data cannot be stored outside of its closure");
71-
err.span_label(sup_sp, "cannot be stored outside of its closure");
72-
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
61+
if let Node::Expr(Expr {
62+
node: Closure(_, _, _, closure_span, None),
63+
..
64+
}) = hir.get(node_id) {
65+
let sup_sp = sup_origin.span();
66+
let origin_sp = origin.span();
67+
let mut err = self.tcx.sess.struct_span_err(
68+
sup_sp,
69+
"borrowed data cannot be stored outside of its closure");
70+
err.span_label(sup_sp, "cannot be stored outside of its closure");
71+
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
7372
// // sup_sp == origin.span():
7473
//
7574
// let mut x = None;
@@ -87,11 +86,11 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
8786
// ------------ ... because it cannot outlive this closure
8887
// f = Some(x);
8988
// ^ cannot be stored outside of its closure
90-
err.span_label(*external_span,
91-
"borrowed data cannot be stored into here...");
92-
err.span_label(*closure_span,
93-
"...because it cannot outlive this closure");
94-
} else {
89+
err.span_label(*external_span,
90+
"borrowed data cannot be stored into here...");
91+
err.span_label(*closure_span,
92+
"...because it cannot outlive this closure");
93+
} else {
9594
// FIXME: the wording for this case could be much improved
9695
//
9796
// let mut lines_to_use: Vec<&CrateId> = Vec::new();
@@ -102,18 +101,16 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
102101
// ...so that variable is valid at time of its declaration
103102
// lines_to_use.push(installed_id);
104103
// ^^^^^^^^^^^^ cannot be stored outside of its closure
105-
err.span_label(origin_sp,
106-
"cannot infer an appropriate lifetime...");
107-
err.span_label(*external_span,
108-
"...so that variable is valid at time of its \
109-
declaration");
110-
err.span_label(*closure_span,
111-
"borrowed data cannot outlive this closure");
112-
}
113-
err.emit();
114-
return Some(ErrorReported);
104+
err.span_label(origin_sp,
105+
"cannot infer an appropriate lifetime...");
106+
err.span_label(*external_span,
107+
"...so that variable is valid at time of its \
108+
declaration");
109+
err.span_label(*closure_span,
110+
"borrowed data cannot outlive this closure");
115111
}
116-
_ => {}
112+
err.emit();
113+
return Some(ErrorReported);
117114
}
118115
}
119116
}

0 commit comments

Comments
 (0)