Skip to content

Commit 41706ff

Browse files
authored
Rollup merge of #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

13 files changed

+59
-89
lines changed

13 files changed

+59
-89
lines changed

src/librustc/infer/canonical/query_result.rs

Lines changed: 28 additions & 36 deletions
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() {
313+
output_query_region_constraints.extend(
314+
query_result.value.region_constraints.iter().filter_map(|r_c| {
321315
let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder(); // reconstructed below
322316
let k1 = substitute_value(self.tcx, &result_subst, &k1);
323317
let r2 = substitute_value(self.tcx, &result_subst, &r2);
324318
if k1 != r2.into() {
325-
output_query_region_constraints
326-
.push(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)));
327-
}
319+
Some(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)))
320+
} else {
321+
None
328322
}
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),
513-
)),
514-
),
515502

516-
UnpackedKind::Type(t1) => Obligation::new(
503+
Obligation::new(
517504
cause.clone(),
518505
param_env,
519-
ty::Predicate::TypeOutlives(ty::Binder::dummy(ty::OutlivesPredicate(
520-
t1, r2,
521-
))),
522-
),
506+
match k1.unpack() {
507+
UnpackedKind::Lifetime(r1) => ty::Predicate::RegionOutlives(
508+
ty::Binder::dummy(
509+
ty::OutlivesPredicate(r1, r2)
510+
)),
511+
UnpackedKind::Type(t1) => ty::Predicate::TypeOutlives(
512+
ty::Binder::dummy(ty::OutlivesPredicate(
513+
t1, r2
514+
)))
523515
}
524-
}),
516+
)
517+
})
525518
) as Box<dyn Iterator<Item = _>>
526519
}
527520

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

586-
let mut outlives: Vec<_> = constraints
579+
let outlives: Vec<_> = constraints
587580
.into_iter()
588581
.map(|(k, _)| match *k {
589582
// Swap regions because we are going from sub (<=) to outlives
@@ -601,13 +594,12 @@ pub fn make_query_outlives<'tcx>(
601594
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
602595
})
603596
.map(ty::Binder::dummy) // no bound regions in the code above
604-
.collect();
605-
606-
outlives.extend(
597+
.chain(
607598
outlives_obligations
608599
.map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
609600
.map(ty::Binder::dummy), // no bound regions in the code above
610-
);
601+
)
602+
.collect();
611603

612604
outlives
613605
}

src/librustc/infer/equate.rs

Lines changed: 2 additions & 4 deletions
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>)

src/librustc/infer/error_reporting/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,11 @@ 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, _)) => {
458+
if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _))
459+
= (&exp_found.expected.sty, &exp_found.found.sty)
460+
{
460461
report_path_match(err, exp_adt.did, found_adt.did);
461462
}
462-
_ => (),
463-
}
464463
}
465464
TypeError::Traits(ref exp_found) => {
466465
report_path_match(err, exp_found.expected, exp_found.found);

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 4 additions & 4 deletions
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

src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs

Lines changed: 5 additions & 5 deletions
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
),

src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ 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 {
61+
if let Node::Expr(Expr {
6362
node: Closure(_, _, _, closure_span, None),
6463
..
65-
}) => {
64+
}) = hir.get(node_id) {
6665
let sup_sp = sup_origin.span();
6766
let origin_sp = origin.span();
6867
let mut err = self.tcx.sess.struct_span_err(
@@ -113,8 +112,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
113112
err.emit();
114113
return Some(ErrorReported);
115114
}
116-
_ => {}
117-
}
118115
}
119116
}
120117
}

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2020
/// Print the error message for lifetime errors when the return type is a static impl Trait.
2121
pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
2222
if let Some(ref error) = self.error {
23-
match error.clone() {
24-
RegionResolutionError::SubSupConflict(
23+
if let RegionResolutionError::SubSupConflict(
2524
var_origin,
2625
sub_origin,
2726
sub_r,
2827
sup_origin,
2928
sup_r,
30-
) => {
29+
) = error.clone()
30+
{
3131
let anon_reg_sup = self.tcx.is_suitable_region(sup_r)?;
3232
if sub_r == &RegionKind::ReStatic &&
3333
self.tcx.return_type_impl_trait(anon_reg_sup.def_id).is_some()
@@ -77,8 +77,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
7777
return Some(ErrorReported);
7878
}
7979
}
80-
_ => {}
81-
}
8280
}
8381
None
8482
}

src/librustc/infer/error_reporting/nice_region_error/util.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,14 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
119119
decl: &hir::FnDecl,
120120
) -> Option<Span> {
121121
let ret_ty = self.tcx.type_of(scope_def_id);
122-
match ret_ty.sty {
123-
ty::FnDef(_, _) => {
122+
if let ty::FnDef(_, _) = ret_ty.sty {
124123
let sig = ret_ty.fn_sig(self.tcx);
125124
let late_bound_regions = self.tcx
126125
.collect_referenced_late_bound_regions(&sig.output());
127126
if late_bound_regions.iter().any(|r| *r == br) {
128127
return Some(decl.output.span());
129128
}
130129
}
131-
_ => {}
132-
}
133130
None
134131
}
135132

src/librustc/infer/lexical_region_resolve/graphviz.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,11 @@ pub fn maybe_print_constraints_for<'a, 'gcx, 'tcx>(
112112
}
113113
};
114114

115-
match dump_region_data_to(region_rels, &region_data.constraints, &output_path) {
116-
Ok(()) => {}
117-
Err(e) => {
115+
if let Err(e) = dump_region_data_to(region_rels, &region_data.constraints, &output_path) {
118116
let msg = format!("io error dumping region constraints: {}", e);
119117
tcx.sess.err(&msg)
120118
}
121119
}
122-
}
123120

124121
struct ConstraintGraph<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
125122
graph_name: String,
@@ -187,12 +184,9 @@ impl<'a, 'gcx, 'tcx> dot::Labeller<'a> for ConstraintGraph<'a, 'gcx, 'tcx> {
187184
None => bug!("no node_id found for node: {:?}", n),
188185
};
189186
let name = || format!("node_{}", node_id);
190-
match dot::Id::new(name()) {
191-
Ok(id) => id,
192-
Err(_) => {
193-
bug!("failed to create graphviz node identified by {}", name());
194-
}
195-
}
187+
188+
dot::Id::new(name()).unwrap_or_else(|_|
189+
bug!("failed to create graphviz node identified by {}", name()))
196190
}
197191
fn node_label(&self, n: &Node) -> dot::LabelText<'_> {
198192
match *n {
@@ -204,7 +198,7 @@ impl<'a, 'gcx, 'tcx> dot::Labeller<'a> for ConstraintGraph<'a, 'gcx, 'tcx> {
204198
match *e {
205199
Edge::Constraint(ref c) =>
206200
dot::LabelText::label(format!("{:?}", self.map.get(c).unwrap())),
207-
Edge::EnclScope(..) => dot::LabelText::label("(enclosed)".to_string()),
201+
Edge::EnclScope(..) => dot::LabelText::label("(enclosed)".to_owned()),
208202
}
209203
}
210204
}

src/librustc/infer/lexical_region_resolve/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
147147
fn construct_var_data(&self, tcx: TyCtxt<'_, '_, 'tcx>) -> LexicalRegionResolutions<'tcx> {
148148
LexicalRegionResolutions {
149149
error_region: tcx.types.re_static,
150-
values: (0..self.num_vars())
151-
.map(|_| VarValue::Value(tcx.types.re_empty))
152-
.collect(),
150+
values: IndexVec::from_elem_n(VarValue::Value(tcx.types.re_empty), self.num_vars())
153151
}
154152
}
155153

src/librustc/infer/opaque_types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
803803
);
804804
debug!("instantiate_opaque_types: ty_var={:?}", ty_var);
805805

806+
self.obligations.reserve(bounds.predicates.len());
806807
for predicate in bounds.predicates {
807808
// Change the predicate to refer to the type variable,
808809
// which will be the concrete type instead of the opaque type.

src/librustc/infer/region_constraints/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
443443
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
444444

445445
if snapshot.length == 0 {
446-
self.undo_log.truncate(0);
446+
self.undo_log.clear();
447447
} else {
448448
(*self.undo_log)[snapshot.length] = CommitedSnapshot;
449449
}
@@ -661,11 +661,10 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
661661
debug!("RegionConstraintCollector: add_verify({:?})", verify);
662662

663663
// skip no-op cases known to be satisfied
664-
match verify.bound {
665-
VerifyBound::AllBounds(ref bs) if bs.len() == 0 => {
664+
if let VerifyBound::AllBounds(ref bs) = verify.bound {
665+
if bs.len() == 0 {
666666
return;
667667
}
668-
_ => {}
669668
}
670669

671670
let index = self.data.verifys.len();

src/librustc/infer/resolve.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)