Skip to content

Commit 2d0262e

Browse files
authored
Rollup merge of rust-lang#54295 - ljedrz:cleanups_rustc_traits, r=nikomatsakis
A few cleanups and minor improvements to rustc/traits It's a little bigger than usual, so bear with me ^^: - introduce `TyCtxt::all_impls` and use it to avoid inefficiently allocating push loops - modify `ArgKind::from_expected_ty` to take an `Option<Span>` argument to make it more versatile - replace `ArgKind::Arg("_".to_owned(), "_".to_owned())` with `ArgKind::empty` - move early `return`s earlier where possible - if all branches of a `match` end with the same expression, move it after it - change a hacky `match` expression to an `if else` chain - move the `push` out from a push loop closure to reduce the number of allocations - correct the vector size for `pretty_predicates` (under `specialize`) - take advantage of the fact that `if else` is an expression - prefer `cloned()` to `map(|&x| x)` and `map(|x| *x)` - prefer `vec![x; y.len()]` to `y.map(|_| x).collect()` - use `unwrap_or_else` instead of `match` where applicable - use `if let` instead of `match` when only one branch matters - prefer `to_owned` to `to_string` for string literals - remove explicit `return`s - remove superfluous braces - whitespace fixes - several other minor improvements
2 parents cf06e03 + a8ec8e5 commit 2d0262e

18 files changed

+414
-466
lines changed

src/librustc/traits/auto_trait.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,18 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
112112
orig_params,
113113
trait_pred.to_poly_trait_predicate(),
114114
));
115+
115116
match result {
116117
Ok(Some(Vtable::VtableImpl(_))) => {
117118
debug!(
118119
"find_auto_trait_generics(did={:?}, trait_did={:?}, generics={:?}): \
119120
manual impl found, bailing out",
120121
did, trait_did, generics
121122
);
122-
return true;
123+
true
123124
}
124-
_ => return false,
125-
};
125+
_ => false
126+
}
126127
});
127128

128129
// If an explicit impl exists, it always takes priority over an auto impl
@@ -426,6 +427,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
426427
if new_trait.def_id() == old_trait.def_id() {
427428
let new_substs = new_trait.skip_binder().trait_ref.substs;
428429
let old_substs = old_trait.skip_binder().trait_ref.substs;
430+
429431
if !new_substs.types().eq(old_substs.types()) {
430432
// We can't compare lifetimes if the types are different,
431433
// so skip checking old_pred
@@ -489,12 +491,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
489491

490492
pub fn get_lifetime(&self, region: Region, names_map: &FxHashMap<String, String>) -> String {
491493
self.region_name(region)
492-
.map(|name| {
493-
names_map.get(&name).unwrap_or_else(|| {
494+
.map(|name|
495+
names_map.get(&name).unwrap_or_else(||
494496
panic!("Missing lifetime with name {:?} for {:?}", name, region)
495-
})
496-
})
497-
.unwrap_or(&"'static".to_string())
497+
)
498+
)
499+
.unwrap_or(&"'static".to_owned())
498500
.clone()
499501
}
500502

src/librustc/traits/codegen/mod.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn codegen_fulfill_obligation<'a, 'tcx>(ty: TyCtxt<'a, 'tcx, 'tcx>,
3939
let trait_ref = ty.erase_regions(&trait_ref);
4040

4141
debug!("codegen_fulfill_obligation(trait_ref={:?}, def_id={:?})",
42-
(param_env, trait_ref), trait_ref.def_id());
42+
(param_env, trait_ref), trait_ref.def_id());
4343

4444
// Do the initial selection for the obligation. This yields the
4545
// shallow result we are looking for -- that is, what specific impl.
@@ -48,8 +48,8 @@ pub fn codegen_fulfill_obligation<'a, 'tcx>(ty: TyCtxt<'a, 'tcx, 'tcx>,
4848

4949
let obligation_cause = ObligationCause::dummy();
5050
let obligation = Obligation::new(obligation_cause,
51-
param_env,
52-
trait_ref.to_poly_trait_predicate());
51+
param_env,
52+
trait_ref.to_poly_trait_predicate());
5353

5454
let selection = match selcx.select(&obligation) {
5555
Ok(Some(selection)) => selection,
@@ -61,12 +61,11 @@ pub fn codegen_fulfill_obligation<'a, 'tcx>(ty: TyCtxt<'a, 'tcx, 'tcx>,
6161
// overflow bug, since I believe this is the only case
6262
// where ambiguity can result.
6363
bug!("Encountered ambiguity selecting `{:?}` during codegen, \
64-
presuming due to overflow",
65-
trait_ref)
64+
presuming due to overflow",
65+
trait_ref)
6666
}
6767
Err(e) => {
68-
bug!("Encountered error `{:?}` selecting `{:?}` during codegen",
69-
e, trait_ref)
68+
bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
7069
}
7170
};
7271

@@ -163,22 +162,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
163162
// In principle, we only need to do this so long as `result`
164163
// contains unbound type parameters. It could be a slight
165164
// optimization to stop iterating early.
166-
match fulfill_cx.select_all_or_error(self) {
167-
Ok(()) => { }
168-
Err(errors) => {
169-
span_bug!(span, "Encountered errors `{:?}` resolving bounds after type-checking",
170-
errors);
171-
}
165+
if let Err(errors) = fulfill_cx.select_all_or_error(self) {
166+
span_bug!(span, "Encountered errors `{:?}` resolving bounds after type-checking",
167+
errors);
172168
}
173169

174170
let result = self.resolve_type_vars_if_possible(result);
175171
let result = self.tcx.erase_regions(&result);
176172

177-
match self.tcx.lift_to_global(&result) {
178-
Some(result) => result,
179-
None => {
180-
span_bug!(span, "Uninferred types/regions in `{:?}`", result);
181-
}
182-
}
173+
self.tcx.lift_to_global(&result).unwrap_or_else(||
174+
span_bug!(span, "Uninferred types/regions in `{:?}`", result)
175+
)
183176
}
184177
}

src/librustc/traits/coherence.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ fn overlap<'cx, 'gcx, 'tcx>(selcx: &mut SelectionContext<'cx, 'gcx, 'tcx>,
115115
b_def_id: DefId)
116116
-> Option<OverlapResult<'tcx>>
117117
{
118-
debug!("overlap(a_def_id={:?}, b_def_id={:?})",
119-
a_def_id,
120-
b_def_id);
118+
debug!("overlap(a_def_id={:?}, b_def_id={:?})", a_def_id, b_def_id);
121119

122120
// For the purposes of this check, we don't bring any skolemized
123121
// types into scope; instead, we replace the generic types with
@@ -133,10 +131,9 @@ fn overlap<'cx, 'gcx, 'tcx>(selcx: &mut SelectionContext<'cx, 'gcx, 'tcx>,
133131

134132
// Do `a` and `b` unify? If not, no overlap.
135133
let obligations = match selcx.infcx().at(&ObligationCause::dummy(), param_env)
136-
.eq_impl_headers(&a_impl_header, &b_impl_header) {
137-
Ok(InferOk { obligations, value: () }) => {
138-
obligations
139-
}
134+
.eq_impl_headers(&a_impl_header, &b_impl_header)
135+
{
136+
Ok(InferOk { obligations, value: () }) => obligations,
140137
Err(_) => return None
141138
};
142139

@@ -164,7 +161,7 @@ fn overlap<'cx, 'gcx, 'tcx>(selcx: &mut SelectionContext<'cx, 'gcx, 'tcx>,
164161
return None
165162
}
166163

167-
let impl_header = selcx.infcx().resolve_type_vars_if_possible(&a_impl_header);
164+
let impl_header = selcx.infcx().resolve_type_vars_if_possible(&a_impl_header);
168165
let intercrate_ambiguity_causes = selcx.take_intercrate_ambiguity_causes();
169166
debug!("overlap: intercrate_ambiguity_causes={:#?}", intercrate_ambiguity_causes);
170167
Some(OverlapResult { impl_header, intercrate_ambiguity_causes })
@@ -471,14 +468,12 @@ fn ty_is_local_constructor(ty: Ty, in_crate: InCrate) -> bool {
471468
ty::Foreign(did) => def_id_is_local(did, in_crate),
472469

473470
ty::Dynamic(ref tt, ..) => {
474-
tt.principal().map_or(false, |p| {
471+
tt.principal().map_or(false, |p|
475472
def_id_is_local(p.def_id(), in_crate)
476-
})
473+
)
477474
}
478475

479-
ty::Error => {
480-
true
481-
}
476+
ty::Error => true,
482477

483478
ty::Closure(..) |
484479
ty::Generator(..) |

0 commit comments

Comments
 (0)